title
stringlengths
0
82
url
stringlengths
46
131
markdown
stringlengths
193
178k
html
stringlengths
243
3.7M
crawlDate
stringlengths
24
24
Introduction - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter1/1?fw=pt
## [](#introduction)Introduction [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-1-questions) ## [](#welcome-to-the-course)Welcome to the 🤗 Course! This course will teach you about natural language processing (NLP) using libraries from the [Hugging Face](https://huggingface.co/) ecosystem — [🤗 Transformers](https://github.com/huggingface/transformers), [🤗 Datasets](https://github.com/huggingface/datasets), [🤗 Tokenizers](https://github.com/huggingface/tokenizers), and [🤗 Accelerate](https://github.com/huggingface/accelerate) — as well as the [Hugging Face Hub](https://huggingface.co/models). It’s completely free and without ads. ## [](#what-to-expect)What to expect? Here is a brief overview of the course: ![Brief overview of the chapters of the course.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/summary.svg) ![Brief overview of the chapters of the course.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/summary-dark.svg) - Chapters 1 to 4 provide an introduction to the main concepts of the 🤗 Transformers library. By the end of this part of the course, you will be familiar with how Transformer models work and will know how to use a model from the [Hugging Face Hub](https://huggingface.co/models), fine-tune it on a dataset, and share your results on the Hub! - Chapters 5 to 8 teach the basics of 🤗 Datasets and 🤗 Tokenizers before diving into classic NLP tasks. By the end of this part, you will be able to tackle the most common NLP problems by yourself. - Chapters 9 to 12 go beyond NLP, and explore how Transformer models can be used to tackle tasks in speech processing and computer vision. Along the way, you’ll learn how to build and share demos of your models, and optimize them for production environments. By the end of this part, you will be ready to apply 🤗 Transformers to (almost) any machine learning problem! This course: - Requires a good knowledge of Python - Is better taken after an introductory deep learning course, such as [fast.ai’s](https://www.fast.ai/) [Practical Deep Learning for Coders](https://course.fast.ai/) or one of the programs developed by [DeepLearning.AI](https://www.deeplearning.ai/) - Does not expect prior [PyTorch](https://pytorch.org/) or [TensorFlow](https://www.tensorflow.org/) knowledge, though some familiarity with either of those will help After you’ve completed this course, we recommend checking out DeepLearning.AI’s [Natural Language Processing Specialization](https://www.coursera.org/specializations/natural-language-processing?utm_source=deeplearning-ai&utm_medium=institutions&utm_campaign=20211011-nlp-2-hugging_face-page-nlp-refresh), which covers a wide range of traditional NLP models like naive Bayes and LSTMs that are well worth knowing about! ## [](#who-are-we)Who are we? About the authors: [**Abubakar Abid**](https://huggingface.co/abidlabs) completed his PhD at Stanford in applied machine learning. During his PhD, he founded [Gradio](https://github.com/gradio-app/gradio), an open-source Python library that has been used to build over 600,000 machine learning demos. Gradio was acquired by Hugging Face, which is where Abubakar now serves as a machine learning team lead. [**Matthew Carrigan**](https://huggingface.co/Rocketknight1) is a Machine Learning Engineer at Hugging Face. He lives in Dublin, Ireland and previously worked as an ML engineer at Parse.ly and before that as a post-doctoral researcher at Trinity College Dublin. He does not believe we’re going to get to AGI by scaling existing architectures, but has high hopes for robot immortality regardless. [**Lysandre Debut**](https://huggingface.co/lysandre) is a Machine Learning Engineer at Hugging Face and has been working on the 🤗 Transformers library since the very early development stages. His aim is to make NLP accessible for everyone by developing tools with a very simple API. [**Sylvain Gugger**](https://huggingface.co/sgugger) is a Research Engineer at Hugging Face and one of the core maintainers of the 🤗 Transformers library. Previously he was a Research Scientist at fast.ai, and he co-wrote _[Deep Learning for Coders with fastai and PyTorch](https://learning.oreilly.com/library/view/deep-learning-for/9781492045519/)_ with Jeremy Howard. The main focus of his research is on making deep learning more accessible, by designing and improving techniques that allow models to train fast on limited resources. [**Dawood Khan**](https://huggingface.co/dawoodkhan82) is a Machine Learning Engineer at Hugging Face. He’s from NYC and graduated from New York University studying Computer Science. After working as an iOS Engineer for a few years, Dawood quit to start Gradio with his fellow co-founders. Gradio was eventually acquired by Hugging Face. [**Merve Noyan**](https://huggingface.co/merve) is a developer advocate at Hugging Face, working on developing tools and building content around them to democratize machine learning for everyone. [**Lucile Saulnier**](https://huggingface.co/SaulLu) is a machine learning engineer at Hugging Face, developing and supporting the use of open source tools. She is also actively involved in many research projects in the field of Natural Language Processing such as collaborative training and BigScience. [**Lewis Tunstall**](https://huggingface.co/lewtun) is a machine learning engineer at Hugging Face, focused on developing open-source tools and making them accessible to the wider community. He is also a co-author of the O’Reilly book [Natural Language Processing with Transformers](https://www.oreilly.com/library/view/natural-language-processing/9781098136789/). [**Leandro von Werra**](https://huggingface.co/lvwerra) is a machine learning engineer in the open-source team at Hugging Face and also a co-author of the O’Reilly book [Natural Language Processing with Transformers](https://www.oreilly.com/library/view/natural-language-processing/9781098136789/). He has several years of industry experience bringing NLP projects to production by working across the whole machine learning stack.. ## [](#faq)FAQ Here are some answers to frequently asked questions: - **Does taking this course lead to a certification?** Currently we do not have any certification for this course. However, we are working on a certification program for the Hugging Face ecosystem — stay tuned! - **How much time should I spend on this course?** Each chapter in this course is designed to be completed in 1 week, with approximately 6-8 hours of work per week. However, you can take as much time as you need to complete the course. - **Where can I ask a question if I have one?** If you have a question about any section of the course, just click on the ”_Ask a question_” banner at the top of the page to be automatically redirected to the right section of the [Hugging Face forums](https://discuss.huggingface.co/): ![Link to the Hugging Face forums](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/forum-button.png) Note that a list of [project ideas](https://discuss.huggingface.co/c/course/course-event/25) is also available on the forums if you wish to practice more once you have completed the course. - **Where can I get the code for the course?** For each section, click on the banner at the top of the page to run the code in either Google Colab or Amazon SageMaker Studio Lab: ![Link to the Hugging Face course notebooks](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/notebook-buttons.png) The Jupyter notebooks containing all the code from the course are hosted on the [`huggingface/notebooks`](https://github.com/huggingface/notebooks) repo. If you wish to generate them locally, check out the instructions in the [`course`](https://github.com/huggingface/course#-jupyter-notebooks) repo on GitHub. - **How can I contribute to the course?** There are many ways to contribute to the course! If you find a typo or a bug, please open an issue on the [`course`](https://github.com/huggingface/course) repo. If you would like to help translate the course into your native language, check out the instructions [here](https://github.com/huggingface/course#translating-the-course-into-your-language). - **What were the choices made for each translation?** Each translation has a glossary and `TRANSLATING.txt` file that details the choices that were made for machine learning jargon etc. You can find an example for German [here](https://github.com/huggingface/course/blob/main/chapters/de/TRANSLATING.txt). - **Can I reuse this course?** Of course! The course is released under the permissive [Apache 2 license](https://www.apache.org/licenses/LICENSE-2.0.html). This means that you must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. If you would like to cite the course, please use the following BibTeX: ``` @misc{huggingfacecourse, author = {Hugging Face}, title = {The Hugging Face Course, 2022}, howpublished = "\url{https://huggingface.co/course}", year = {2022}, note = "[Online; accessed <today>]" }``` ## [](#lets-go)Let's Go Are you ready to roll? In this chapter, you will learn: - How to use the `pipeline()` function to solve NLP tasks such as text generation and classification - About the Transformer architecture - How to distinguish between encoder, decoder, and encoder-decoder architectures and use cases
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Introduction - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter1/1"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Introduction - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;introduction&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;welcome-to-the-course&quot;,&quot;title&quot;:&quot;Welcome to the 🤗 Course!&quot;},{&quot;local&quot;:&quot;what-to-expect&quot;,&quot;title&quot;:&quot;What to expect?&quot;},{&quot;local&quot;:&quot;who-are-we&quot;,&quot;title&quot;:&quot;Who are we?&quot;},{&quot;local&quot;:&quot;faq&quot;,&quot;title&quot;:&quot;FAQ&quot;},{&quot;local&quot;:&quot;lets-go&quot;,&quot;title&quot;:&quot;Let's Go&quot;}],&quot;title&quot;:&quot;Introduction&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter1/1&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Introduction&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Introduction</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> </a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter1/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/2?fw=pt">Natural Language Processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/3?fw=pt">Transformers, what can they do? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/4?fw=pt">How do Transformers work? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/5?fw=pt">Encoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/6?fw=pt">Decoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/7?fw=pt">Sequence-to-sequence models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/8?fw=pt">Bias and limitations </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/9?fw=pt">Summary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="introduction" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#introduction"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Introduction</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-1-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <h2 class="relative group"><a id="welcome-to-the-course" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#welcome-to-the-course"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Welcome to the 🤗 Course!</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/00GKzGyWFEs" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>This course will teach you about natural language processing (NLP) using libraries from the <a href="https://huggingface.co/" rel="nofollow">Hugging Face</a> ecosystem — <a href="https://github.com/huggingface/transformers" rel="nofollow">🤗 Transformers</a>, <a href="https://github.com/huggingface/datasets" rel="nofollow">🤗 Datasets</a>, <a href="https://github.com/huggingface/tokenizers" rel="nofollow">🤗 Tokenizers</a>, and <a href="https://github.com/huggingface/accelerate" rel="nofollow">🤗 Accelerate</a> — as well as the <a href="https://huggingface.co/models" rel="nofollow">Hugging Face Hub</a>. It’s completely free and without ads.</p> <h2 class="relative group"><a id="what-to-expect" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#what-to-expect"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>What to expect?</span></h2> <p>Here is a brief overview of the course:</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/summary.svg" alt="Brief overview of the chapters of the course."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/summary-dark.svg" alt="Brief overview of the chapters of the course."></div> <ul><li>Chapters 1 to 4 provide an introduction to the main concepts of the 🤗 Transformers library. By the end of this part of the course, you will be familiar with how Transformer models work and will know how to use a model from the <a href="https://huggingface.co/models" rel="nofollow">Hugging Face Hub</a>, fine-tune it on a dataset, and share your results on the Hub!</li> <li>Chapters 5 to 8 teach the basics of 🤗 Datasets and 🤗 Tokenizers before diving into classic NLP tasks. By the end of this part, you will be able to tackle the most common NLP problems by yourself.</li> <li>Chapters 9 to 12 go beyond NLP, and explore how Transformer models can be used to tackle tasks in speech processing and computer vision. Along the way, you’ll learn how to build and share demos of your models, and optimize them for production environments. By the end of this part, you will be ready to apply 🤗 Transformers to (almost) any machine learning problem!</li></ul> <p>This course:</p> <ul><li>Requires a good knowledge of Python</li> <li>Is better taken after an introductory deep learning course, such as <a href="https://www.fast.ai/" rel="nofollow">fast.ai’s</a> <a href="https://course.fast.ai/" rel="nofollow">Practical Deep Learning for Coders</a> or one of the programs developed by <a href="https://www.deeplearning.ai/" rel="nofollow">DeepLearning.AI</a></li> <li>Does not expect prior <a href="https://pytorch.org/" rel="nofollow">PyTorch</a> or <a href="https://www.tensorflow.org/" rel="nofollow">TensorFlow</a> knowledge, though some familiarity with either of those will help</li></ul> <p>After you’ve completed this course, we recommend checking out DeepLearning.AI’s <a href="https://www.coursera.org/specializations/natural-language-processing?utm_source=deeplearning-ai&amp;utm_medium=institutions&amp;utm_campaign=20211011-nlp-2-hugging_face-page-nlp-refresh" rel="nofollow">Natural Language Processing Specialization</a>, which covers a wide range of traditional NLP models like naive Bayes and LSTMs that are well worth knowing about!</p> <h2 class="relative group"><a id="who-are-we" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#who-are-we"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Who are we?</span></h2> <p>About the authors:</p> <p><a href="https://huggingface.co/abidlabs" rel="nofollow"><strong>Abubakar Abid</strong></a> completed his PhD at Stanford in applied machine learning. During his PhD, he founded <a href="https://github.com/gradio-app/gradio" rel="nofollow">Gradio</a>, an open-source Python library that has been used to build over 600,000 machine learning demos. Gradio was acquired by Hugging Face, which is where Abubakar now serves as a machine learning team lead.</p> <p><a href="https://huggingface.co/Rocketknight1" rel="nofollow"><strong>Matthew Carrigan</strong></a> is a Machine Learning Engineer at Hugging Face. He lives in Dublin, Ireland and previously worked as an ML engineer at Parse.ly and before that as a post-doctoral researcher at Trinity College Dublin. He does not believe we’re going to get to AGI by scaling existing architectures, but has high hopes for robot immortality regardless.</p> <p><a href="https://huggingface.co/lysandre" rel="nofollow"><strong>Lysandre Debut</strong></a> is a Machine Learning Engineer at Hugging Face and has been working on the 🤗 Transformers library since the very early development stages. His aim is to make NLP accessible for everyone by developing tools with a very simple API.</p> <p><a href="https://huggingface.co/sgugger" rel="nofollow"><strong>Sylvain Gugger</strong></a> is a Research Engineer at Hugging Face and one of the core maintainers of the 🤗 Transformers library. Previously he was a Research Scientist at fast.ai, and he co-wrote <em><a href="https://learning.oreilly.com/library/view/deep-learning-for/9781492045519/" rel="nofollow">Deep Learning for Coders with fastai and PyTorch</a></em> with Jeremy Howard. The main focus of his research is on making deep learning more accessible, by designing and improving techniques that allow models to train fast on limited resources.</p> <p><a href="https://huggingface.co/dawoodkhan82" rel="nofollow"><strong>Dawood Khan</strong></a> is a Machine Learning Engineer at Hugging Face. He’s from NYC and graduated from New York University studying Computer Science. After working as an iOS Engineer for a few years, Dawood quit to start Gradio with his fellow co-founders. Gradio was eventually acquired by Hugging Face.</p> <p><a href="https://huggingface.co/merve" rel="nofollow"><strong>Merve Noyan</strong></a> is a developer advocate at Hugging Face, working on developing tools and building content around them to democratize machine learning for everyone.</p> <p><a href="https://huggingface.co/SaulLu" rel="nofollow"><strong>Lucile Saulnier</strong></a> is a machine learning engineer at Hugging Face, developing and supporting the use of open source tools. She is also actively involved in many research projects in the field of Natural Language Processing such as collaborative training and BigScience.</p> <p><a href="https://huggingface.co/lewtun" rel="nofollow"><strong>Lewis Tunstall</strong></a> is a machine learning engineer at Hugging Face, focused on developing open-source tools and making them accessible to the wider community. He is also a co-author of the O’Reilly book <a href="https://www.oreilly.com/library/view/natural-language-processing/9781098136789/" rel="nofollow">Natural Language Processing with Transformers</a>.</p> <p><a href="https://huggingface.co/lvwerra" rel="nofollow"><strong>Leandro von Werra</strong></a> is a machine learning engineer in the open-source team at Hugging Face and also a co-author of the O’Reilly book <a href="https://www.oreilly.com/library/view/natural-language-processing/9781098136789/" rel="nofollow">Natural Language Processing with Transformers</a>. He has several years of industry experience bringing NLP projects to production by working across the whole machine learning stack..</p> <h2 class="relative group"><a id="faq" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#faq"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>FAQ</span></h2> <p>Here are some answers to frequently asked questions:</p> <ul><li><p><strong>Does taking this course lead to a certification?</strong> Currently we do not have any certification for this course. However, we are working on a certification program for the Hugging Face ecosystem — stay tuned!</p></li> <li><p><strong>How much time should I spend on this course?</strong> Each chapter in this course is designed to be completed in 1 week, with approximately 6-8 hours of work per week. However, you can take as much time as you need to complete the course.</p></li> <li><p><strong>Where can I ask a question if I have one?</strong> If you have a question about any section of the course, just click on the ”<em>Ask a question</em>” banner at the top of the page to be automatically redirected to the right section of the <a href="https://discuss.huggingface.co/" rel="nofollow">Hugging Face forums</a>:</p></li></ul> <img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/forum-button.png" alt="Link to the Hugging Face forums" width="75%"> <p>Note that a list of <a href="https://discuss.huggingface.co/c/course/course-event/25" rel="nofollow">project ideas</a> is also available on the forums if you wish to practice more once you have completed the course.</p> <ul><li><strong>Where can I get the code for the course?</strong> For each section, click on the banner at the top of the page to run the code in either Google Colab or Amazon SageMaker Studio Lab:</li></ul> <img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/notebook-buttons.png" alt="Link to the Hugging Face course notebooks" width="75%"> <p>The Jupyter notebooks containing all the code from the course are hosted on the <a href="https://github.com/huggingface/notebooks" rel="nofollow"><code>huggingface/notebooks</code></a> repo. If you wish to generate them locally, check out the instructions in the <a href="https://github.com/huggingface/course#-jupyter-notebooks" rel="nofollow"><code>course</code></a> repo on GitHub.</p> <ul><li><p><strong>How can I contribute to the course?</strong> There are many ways to contribute to the course! If you find a typo or a bug, please open an issue on the <a href="https://github.com/huggingface/course" rel="nofollow"><code>course</code></a> repo. If you would like to help translate the course into your native language, check out the instructions <a href="https://github.com/huggingface/course#translating-the-course-into-your-language" rel="nofollow">here</a>.</p></li> <li><p><strong>What were the choices made for each translation?</strong> Each translation has a glossary and <code>TRANSLATING.txt</code> file that details the choices that were made for machine learning jargon etc. You can find an example for German <a href="https://github.com/huggingface/course/blob/main/chapters/de/TRANSLATING.txt" rel="nofollow">here</a>.</p></li></ul> <ul><li><strong>Can I reuse this course?</strong> Of course! The course is released under the permissive <a href="https://www.apache.org/licenses/LICENSE-2.0.html" rel="nofollow">Apache 2 license</a>. This means that you must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. If you would like to cite the course, please use the following BibTeX:</li></ul> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>@misc{huggingfacecourse, <span class="hljs-attr">author</span> = {Hugging Face}, <span class="hljs-attr">title</span> = {The Hugging Face Course, <span class="hljs-number">2022</span>}, <span class="hljs-attr">howpublished</span> = <span class="hljs-string">"\url{https://huggingface.co/course}"</span>, <span class="hljs-attr">year</span> = {<span class="hljs-number">2022</span>}, <span class="hljs-attr">note</span> = <span class="hljs-string">"[Online; accessed &lt;today&gt;]"</span> }</pre></div> <h2 class="relative group"><a id="lets-go" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#lets-go"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Let's Go</span></h2> Are you ready to roll? In this chapter, you will learn: <ul><li>How to use the <code>pipeline()</code> function to solve NLP tasks such as text generation and classification</li> <li>About the Transformer architecture</li> <li>How to distinguish between encoder, decoder, and encoder-decoder architectures and use cases</li></ul> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter0/1?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Introduction</a> <a href="/learn/nlp-course/chapter1/2?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Natural Language Processing<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Introduction&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;introduction&quot;,&quot;url&quot;:&quot;#introduction&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Welcome to the 🤗 Course!&quot;,&quot;id&quot;:&quot;welcome-to-the-course&quot;,&quot;url&quot;:&quot;#welcome-to-the-course&quot;},{&quot;title&quot;:&quot;What to expect?&quot;,&quot;id&quot;:&quot;what-to-expect&quot;,&quot;url&quot;:&quot;#what-to-expect&quot;},{&quot;title&quot;:&quot;Who are we?&quot;,&quot;id&quot;:&quot;who-are-we&quot;,&quot;url&quot;:&quot;#who-are-we&quot;},{&quot;title&quot;:&quot;FAQ&quot;,&quot;id&quot;:&quot;faq&quot;,&quot;url&quot;:&quot;#faq&quot;},{&quot;title&quot;:&quot;Let's Go&quot;,&quot;id&quot;:&quot;lets-go&quot;,&quot;url&quot;:&quot;#lets-go&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#introduction" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-introduction"><wbr>Introduction</a> <a href="#welcome-to-the-course" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-welcome-to-the-course"><wbr>Welcome to the 🤗 <wbr>Course!</a> <a href="#what-to-expect" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-what-to-expect"><wbr>What to expect?</a> <a href="#who-are-we" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-who-are-we"><wbr>Who are we?</a> <a href="#faq" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-faq">FAQ</a> <a href="#lets-go" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-lets-go"><wbr>Let's <wbr>Go</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter1/1" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter1/1"); } </script> <iframe name="__privateStripeMetricsController5750" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter1%2F1%3Ffw%3Dpt&amp;title=Introduction%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:02.673Z
Transformers, what can they do? - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter1/3?fw=pt
## [](#transformers-what-can-they-do)Transformers, what can they do? [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-1-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter1/section3.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter1/section3.ipynb) In this section, we will look at what Transformer models can do and use our first tool from the 🤗 Transformers library: the `pipeline()` function. 👀 See that _Open in Colab_ button on the top right? Click on it to open a Google Colab notebook with all the code samples of this section. This button will be present in any section containing code examples. If you want to run the examples locally, we recommend taking a look at the [setup](/course/chapter0). ## [](#transformers-are-everywhere)Transformers are everywhere! Transformer models are used to solve all kinds of NLP tasks, like the ones mentioned in the previous section. Here are some of the companies and organizations using Hugging Face and Transformer models, who also contribute back to the community by sharing their models: ![Companies using Hugging Face](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/companies.PNG) The [🤗 Transformers library](https://github.com/huggingface/transformers) provides the functionality to create and use those shared models. The [Model Hub](https://huggingface.co/models) contains thousands of pretrained models that anyone can download and use. You can also upload your own models to the Hub! ⚠️ The Hugging Face Hub is not limited to Transformer models. Anyone can share any kind of models or datasets they want! [Create a huggingface.co](https://huggingface.co/join) account to benefit from all available features! Before diving into how Transformer models work under the hood, let’s look at a few examples of how they can be used to solve some interesting NLP problems. ## [](#working-with-pipelines)Working with pipelines The most basic object in the 🤗 Transformers library is the `pipeline()` function. It connects a model with its necessary preprocessing and postprocessing steps, allowing us to directly input any text and get an intelligible answer: ``` from transformers import pipeline classifier = pipeline("sentiment-analysis") classifier("I've been waiting for a HuggingFace course my whole life.")``` ``` [{'label': 'POSITIVE', 'score': 0.9598047137260437}]``` We can even pass several sentences! ``` classifier( ["I've been waiting for a HuggingFace course my whole life.", "I hate this so much!"] )``` ``` [{'label': 'POSITIVE', 'score': 0.9598047137260437}, {'label': 'NEGATIVE', 'score': 0.9994558095932007}]``` By default, this pipeline selects a particular pretrained model that has been fine-tuned for sentiment analysis in English. The model is downloaded and cached when you create the `classifier` object. If you rerun the command, the cached model will be used instead and there is no need to download the model again. There are three main steps involved when you pass some text to a pipeline: 1. The text is preprocessed into a format the model can understand. 2. The preprocessed inputs are passed to the model. 3. The predictions of the model are post-processed, so you can make sense of them. Some of the currently [available pipelines](https://huggingface.co/transformers/main_classes/pipelines.html) are: - `feature-extraction` (get the vector representation of a text) - `fill-mask` - `ner` (named entity recognition) - `question-answering` - `sentiment-analysis` - `summarization` - `text-generation` - `translation` - `zero-shot-classification` Let’s have a look at a few of these! ## [](#zero-shot-classification)Zero-shot classification We’ll start by tackling a more challenging task where we need to classify texts that haven’t been labelled. This is a common scenario in real-world projects because annotating text is usually time-consuming and requires domain expertise. For this use case, the `zero-shot-classification` pipeline is very powerful: it allows you to specify which labels to use for the classification, so you don’t have to rely on the labels of the pretrained model. You’ve already seen how the model can classify a sentence as positive or negative using those two labels — but it can also classify the text using any other set of labels you like. ``` from transformers import pipeline classifier = pipeline("zero-shot-classification") classifier( "This is a course about the Transformers library", candidate_labels=["education", "politics", "business"], )``` ``` {'sequence': 'This is a course about the Transformers library', 'labels': ['education', 'business', 'politics'], 'scores': [0.8445963859558105, 0.111976258456707, 0.043427448719739914]}``` This pipeline is called _zero-shot_ because you don’t need to fine-tune the model on your data to use it. It can directly return probability scores for any list of labels you want! ✏️ **Try it out!** Play around with your own sequences and labels and see how the model behaves. ## [](#text-generation)Text generation Now let’s see how to use a pipeline to generate some text. The main idea here is that you provide a prompt and the model will auto-complete it by generating the remaining text. This is similar to the predictive text feature that is found on many phones. Text generation involves randomness, so it’s normal if you don’t get the same results as shown below. ``` from transformers import pipeline generator = pipeline("text-generation") generator("In this course, we will teach you how to")``` ``` [{'generated_text': 'In this course, we will teach you how to understand and use ' 'data flow and data interchange when handling user data. We ' 'will be working with one or more of the most commonly used ' 'data flows — data flows of various types, as seen by the ' 'HTTP'}]``` You can control how many different sequences are generated with the argument `num_return_sequences` and the total length of the output text with the argument `max_length`. ✏️ **Try it out!** Use the `num_return_sequences` and `max_length` arguments to generate two sentences of 15 words each. ## [](#using-any-model-from-the-hub-in-a-pipeline)Using any model from the Hub in a pipeline The previous examples used the default model for the task at hand, but you can also choose a particular model from the Hub to use in a pipeline for a specific task — say, text generation. Go to the [Model Hub](https://huggingface.co/models) and click on the corresponding tag on the left to display only the supported models for that task. You should get to a page like [this one](https://huggingface.co/models?pipeline_tag=text-generation). Let’s try the [`distilgpt2`](https://huggingface.co/distilgpt2) model! Here’s how to load it in the same pipeline as before: ``` from transformers import pipeline generator = pipeline("text-generation", model="distilgpt2") generator( "In this course, we will teach you how to", max_length=30, num_return_sequences=2, )``` ``` [{'generated_text': 'In this course, we will teach you how to manipulate the world and ' 'move your mental and physical capabilities to your advantage.'}, {'generated_text': 'In this course, we will teach you how to become an expert and ' 'practice realtime, and with a hands on experience on both real ' 'time and real'}]``` You can refine your search for a model by clicking on the language tags, and pick a model that will generate text in another language. The Model Hub even contains checkpoints for multilingual models that support several languages. Once you select a model by clicking on it, you’ll see that there is a widget enabling you to try it directly online. This way you can quickly test the model’s capabilities before downloading it. ✏️ **Try it out!** Use the filters to find a text generation model for another language. Feel free to play with the widget and use it in a pipeline! ### [](#the-inference-api)The Inference API All the models can be tested directly through your browser using the Inference API, which is available on the Hugging Face [website](https://huggingface.co/). You can play with the model directly on this page by inputting custom text and watching the model process the input data. The Inference API that powers the widget is also available as a paid product, which comes in handy if you need it for your workflows. See the [pricing page](https://huggingface.co/pricing) for more details. ## [](#mask-filling)Mask filling The next pipeline you’ll try is `fill-mask`. The idea of this task is to fill in the blanks in a given text: ``` from transformers import pipeline unmasker = pipeline("fill-mask") unmasker("This course will teach you all about <mask> models.", top_k=2)``` ``` [{'sequence': 'This course will teach you all about mathematical models.', 'score': 0.19619831442832947, 'token': 30412, 'token_str': ' mathematical'}, {'sequence': 'This course will teach you all about computational models.', 'score': 0.04052725434303284, 'token': 38163, 'token_str': ' computational'}]``` The `top_k` argument controls how many possibilities you want to be displayed. Note that here the model fills in the special `<mask>` word, which is often referred to as a _mask token_. Other mask-filling models might have different mask tokens, so it’s always good to verify the proper mask word when exploring other models. One way to check it is by looking at the mask word used in the widget. ✏️ **Try it out!** Search for the `bert-base-cased` model on the Hub and identify its mask word in the Inference API widget. What does this model predict for the sentence in our `pipeline` example above? ## [](#named-entity-recognition)Named entity recognition Named entity recognition (NER) is a task where the model has to find which parts of the input text correspond to entities such as persons, locations, or organizations. Let’s look at an example: ``` from transformers import pipeline ner = pipeline("ner", grouped_entities=True) ner("My name is Sylvain and I work at Hugging Face in Brooklyn.")``` ``` [{'entity_group': 'PER', 'score': 0.99816, 'word': 'Sylvain', 'start': 11, 'end': 18}, {'entity_group': 'ORG', 'score': 0.97960, 'word': 'Hugging Face', 'start': 33, 'end': 45}, {'entity_group': 'LOC', 'score': 0.99321, 'word': 'Brooklyn', 'start': 49, 'end': 57} ]``` Here the model correctly identified that Sylvain is a person (PER), Hugging Face an organization (ORG), and Brooklyn a location (LOC). We pass the option `grouped_entities=True` in the pipeline creation function to tell the pipeline to regroup together the parts of the sentence that correspond to the same entity: here the model correctly grouped “Hugging” and “Face” as a single organization, even though the name consists of multiple words. In fact, as we will see in the next chapter, the preprocessing even splits some words into smaller parts. For instance, `Sylvain` is split into four pieces: `S`, `##yl`, `##va`, and `##in`. In the post-processing step, the pipeline successfully regrouped those pieces. ✏️ **Try it out!** Search the Model Hub for a model able to do part-of-speech tagging (usually abbreviated as POS) in English. What does this model predict for the sentence in the example above? ## [](#question-answering)Question answering The `question-answering` pipeline answers questions using information from a given context: ``` from transformers import pipeline question_answerer = pipeline("question-answering") question_answerer( question="Where do I work?", context="My name is Sylvain and I work at Hugging Face in Brooklyn", )``` ``` {'score': 0.6385916471481323, 'start': 33, 'end': 45, 'answer': 'Hugging Face'}``` Note that this pipeline works by extracting information from the provided context; it does not generate the answer. ## [](#summarization)Summarization Summarization is the task of reducing a text into a shorter text while keeping all (or most) of the important aspects referenced in the text. Here’s an example: ``` from transformers import pipeline summarizer = pipeline("summarization") summarizer( """ America has changed dramatically during recent years. Not only has the number of graduates in traditional engineering disciplines such as mechanical, civil, electrical, chemical, and aeronautical engineering declined, but in most of the premier American universities engineering curricula now concentrate on and encourage largely the study of engineering science. As a result, there are declining offerings in engineering subjects dealing with infrastructure, the environment, and related issues, and greater concentration on high technology subjects, largely supporting increasingly complex scientific developments. While the latter is important, it should not be at the expense of more traditional engineering. Rapidly developing economies such as China and India, as well as other industrial countries in Europe and Asia, continue to encourage and advance the teaching of engineering. Both China and India, respectively, graduate six and eight times as many traditional engineers as does the United States. Other industrial countries at minimum maintain their output, while America suffers an increasingly serious decline in the number of engineering graduates and a lack of well-educated engineers. """ )``` ``` [{'summary_text': ' America has changed dramatically during recent years . The ' 'number of engineering graduates in the U.S. has declined in ' 'traditional engineering disciplines such as mechanical, civil ' ', electrical, chemical, and aeronautical engineering . Rapidly ' 'developing economies such as China and India, as well as other ' 'industrial countries in Europe and Asia, continue to encourage ' 'and advance engineering .'}]``` Like with text generation, you can specify a `max_length` or a `min_length` for the result. ## [](#translation)Translation For translation, you can use a default model if you provide a language pair in the task name (such as `"translation_en_to_fr"`), but the easiest way is to pick the model you want to use on the [Model Hub](https://huggingface.co/models). Here we’ll try translating from French to English: ``` from transformers import pipeline translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en") translator("Ce cours est produit par Hugging Face.")``` ``` [{'translation_text': 'This course is produced by Hugging Face.'}]``` Like with text generation and summarization, you can specify a `max_length` or a `min_length` for the result. ✏️ **Try it out!** Search for translation models in other languages and try to translate the previous sentence into a few different languages. The pipelines shown so far are mostly for demonstrative purposes. They were programmed for specific tasks and cannot perform variations of them. In the next chapter, you’ll learn what’s inside a `pipeline()` function and how to customize its behavior.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Transformers, what can they do? - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter1/3"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Transformers, what can they do? - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;transformers-what-can-they-do&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;transformers-are-everywhere&quot;,&quot;title&quot;:&quot;Transformers are everywhere!&quot;},{&quot;local&quot;:&quot;working-with-pipelines&quot;,&quot;title&quot;:&quot;Working with pipelines&quot;},{&quot;local&quot;:&quot;zero-shot-classification&quot;,&quot;title&quot;:&quot;Zero-shot classification&quot;},{&quot;local&quot;:&quot;text-generation&quot;,&quot;title&quot;:&quot;Text generation&quot;},{&quot;local&quot;:&quot;using-any-model-from-the-hub-in-a-pipeline&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;the-inference-api&quot;,&quot;title&quot;:&quot;The Inference API&quot;}],&quot;title&quot;:&quot;Using any model from the Hub in a pipeline&quot;},{&quot;local&quot;:&quot;mask-filling&quot;,&quot;title&quot;:&quot;Mask filling&quot;},{&quot;local&quot;:&quot;named-entity-recognition&quot;,&quot;title&quot;:&quot;Named entity recognition&quot;},{&quot;local&quot;:&quot;question-answering&quot;,&quot;title&quot;:&quot;Question answering&quot;},{&quot;local&quot;:&quot;summarization&quot;,&quot;title&quot;:&quot;Summarization&quot;},{&quot;local&quot;:&quot;translation&quot;,&quot;title&quot;:&quot;Translation&quot;}],&quot;title&quot;:&quot;Transformers, what can they do?&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter1/3&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Transformers, what can they do?&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Transformers, what can they do?</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/2?fw=pt">Natural Language Processing </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter1/3?fw=pt">Transformers, what can they do? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/4?fw=pt">How do Transformers work? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/5?fw=pt">Encoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/6?fw=pt">Decoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/7?fw=pt">Sequence-to-sequence models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/8?fw=pt">Bias and limitations </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/9?fw=pt">Summary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="transformers-what-can-they-do" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers-what-can-they-do"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Transformers, what can they do?</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-1-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter1/section3.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter1/section3.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>In this section, we will look at what Transformer models can do and use our first tool from the 🤗 Transformers library: the <code>pipeline()</code> function.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400">👀 See that <em>Open in Colab</em> button on the top right? Click on it to open a Google Colab notebook with all the code samples of this section. This button will be present in any section containing code examples. <p>If you want to run the examples locally, we recommend taking a look at the <a href="/course/chapter0">setup</a>.</p></div> <h2 class="relative group"><a id="transformers-are-everywhere" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers-are-everywhere"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Transformers are everywhere!</span></h2> <p>Transformer models are used to solve all kinds of NLP tasks, like the ones mentioned in the previous section. Here are some of the companies and organizations using Hugging Face and Transformer models, who also contribute back to the community by sharing their models:</p> <img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/companies.PNG" alt="Companies using Hugging Face" width="100%"> <p>The <a href="https://github.com/huggingface/transformers" rel="nofollow">🤗 Transformers library</a> provides the functionality to create and use those shared models. The <a href="https://huggingface.co/models" rel="nofollow">Model Hub</a> contains thousands of pretrained models that anyone can download and use. You can also upload your own models to the Hub!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400">⚠️ The Hugging Face Hub is not limited to Transformer models. Anyone can share any kind of models or datasets they want! <a href="https://huggingface.co/join">Create a huggingface.co</a> account to benefit from all available features!</div> <p>Before diving into how Transformer models work under the hood, let’s look at a few examples of how they can be used to solve some interesting NLP problems.</p> <h2 class="relative group"><a id="working-with-pipelines" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#working-with-pipelines"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Working with pipelines</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/tiZFewofSLM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The most basic object in the 🤗 Transformers library is the <code>pipeline()</code> function. It connects a model with its necessary preprocessing and postprocessing steps, allowing us to directly input any text and get an intelligible answer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline classifier = pipeline(<span class="hljs-string">"sentiment-analysis"</span>) classifier(<span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'label'</span>: <span class="hljs-string">'POSITIVE'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9598047137260437</span>}]</pre></div> <p>We can even pass several sentences!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>classifier( [<span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span>, <span class="hljs-string">"I hate this so much!"</span>] )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'label'</span>: <span class="hljs-string">'POSITIVE'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9598047137260437</span>}, {<span class="hljs-string">'label'</span>: <span class="hljs-string">'NEGATIVE'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9994558095932007</span>}]</pre></div> <p>By default, this pipeline selects a particular pretrained model that has been fine-tuned for sentiment analysis in English. The model is downloaded and cached when you create the <code>classifier</code> object. If you rerun the command, the cached model will be used instead and there is no need to download the model again.</p> <p>There are three main steps involved when you pass some text to a pipeline:</p> <ol><li>The text is preprocessed into a format the model can understand.</li> <li>The preprocessed inputs are passed to the model.</li> <li>The predictions of the model are post-processed, so you can make sense of them.</li></ol> <p>Some of the currently <a href="https://huggingface.co/transformers/main_classes/pipelines.html" rel="nofollow">available pipelines</a> are:</p> <ul><li><code>feature-extraction</code> (get the vector representation of a text)</li> <li><code>fill-mask</code></li> <li><code>ner</code> (named entity recognition)</li> <li><code>question-answering</code></li> <li><code>sentiment-analysis</code></li> <li><code>summarization</code></li> <li><code>text-generation</code></li> <li><code>translation</code></li> <li><code>zero-shot-classification</code></li></ul> <p>Let’s have a look at a few of these!</p> <h2 class="relative group"><a id="zero-shot-classification" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#zero-shot-classification"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Zero-shot classification</span></h2> <p>We’ll start by tackling a more challenging task where we need to classify texts that haven’t been labelled. This is a common scenario in real-world projects because annotating text is usually time-consuming and requires domain expertise. For this use case, the <code>zero-shot-classification</code> pipeline is very powerful: it allows you to specify which labels to use for the classification, so you don’t have to rely on the labels of the pretrained model. You’ve already seen how the model can classify a sentence as positive or negative using those two labels — but it can also classify the text using any other set of labels you like.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline classifier = pipeline(<span class="hljs-string">"zero-shot-classification"</span>) classifier( <span class="hljs-string">"This is a course about the Transformers library"</span>, candidate_labels=[<span class="hljs-string">"education"</span>, <span class="hljs-string">"politics"</span>, <span class="hljs-string">"business"</span>], )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'sequence'</span>: <span class="hljs-string">'This is a course about the Transformers library'</span>, <span class="hljs-string">'labels'</span>: [<span class="hljs-string">'education'</span>, <span class="hljs-string">'business'</span>, <span class="hljs-string">'politics'</span>], <span class="hljs-string">'scores'</span>: [<span class="hljs-number">0.8445963859558105</span>, <span class="hljs-number">0.111976258456707</span>, <span class="hljs-number">0.043427448719739914</span>]}</pre></div> <p>This pipeline is called <em>zero-shot</em> because you don’t need to fine-tune the model on your data to use it. It can directly return probability scores for any list of labels you want!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Play around with your own sequences and labels and see how the model behaves.</p></div> <h2 class="relative group"><a id="text-generation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#text-generation"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Text generation</span></h2> <p>Now let’s see how to use a pipeline to generate some text. The main idea here is that you provide a prompt and the model will auto-complete it by generating the remaining text. This is similar to the predictive text feature that is found on many phones. Text generation involves randomness, so it’s normal if you don’t get the same results as shown below.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline generator = pipeline(<span class="hljs-string">"text-generation"</span>) generator(<span class="hljs-string">"In this course, we will teach you how to"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'generated_text'</span>: <span class="hljs-string">'In this course, we will teach you how to understand and use '</span> <span class="hljs-string">'data flow and data interchange when handling user data. We '</span> <span class="hljs-string">'will be working with one or more of the most commonly used '</span> <span class="hljs-string">'data flows — data flows of various types, as seen by the '</span> <span class="hljs-string">'HTTP'</span>}]</pre></div> <p>You can control how many different sequences are generated with the argument <code>num_return_sequences</code> and the total length of the output text with the argument <code>max_length</code>.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Use the <code>num_return_sequences</code> and <code>max_length</code> arguments to generate two sentences of 15 words each.</p></div> <h2 class="relative group"><a id="using-any-model-from-the-hub-in-a-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-any-model-from-the-hub-in-a-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using any model from the Hub in a pipeline</span></h2> <p>The previous examples used the default model for the task at hand, but you can also choose a particular model from the Hub to use in a pipeline for a specific task — say, text generation. Go to the <a href="https://huggingface.co/models" rel="nofollow">Model Hub</a> and click on the corresponding tag on the left to display only the supported models for that task. You should get to a page like <a href="https://huggingface.co/models?pipeline_tag=text-generation" rel="nofollow">this one</a>.</p> <p>Let’s try the <a href="https://huggingface.co/distilgpt2" rel="nofollow"><code>distilgpt2</code></a> model! Here’s how to load it in the same pipeline as before:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline generator = pipeline(<span class="hljs-string">"text-generation"</span>, model=<span class="hljs-string">"distilgpt2"</span>) generator( <span class="hljs-string">"In this course, we will teach you how to"</span>, max_length=<span class="hljs-number">30</span>, num_return_sequences=<span class="hljs-number">2</span>, )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'generated_text'</span>: <span class="hljs-string">'In this course, we will teach you how to manipulate the world and '</span> <span class="hljs-string">'move your mental and physical capabilities to your advantage.'</span>}, {<span class="hljs-string">'generated_text'</span>: <span class="hljs-string">'In this course, we will teach you how to become an expert and '</span> <span class="hljs-string">'practice realtime, and with a hands on experience on both real '</span> <span class="hljs-string">'time and real'</span>}]</pre></div> <p>You can refine your search for a model by clicking on the language tags, and pick a model that will generate text in another language. The Model Hub even contains checkpoints for multilingual models that support several languages.</p> <p>Once you select a model by clicking on it, you’ll see that there is a widget enabling you to try it directly online. This way you can quickly test the model’s capabilities before downloading it.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Use the filters to find a text generation model for another language. Feel free to play with the widget and use it in a pipeline!</p></div> <h3 class="relative group"><a id="the-inference-api" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-inference-api"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The Inference API</span></h3> <p>All the models can be tested directly through your browser using the Inference API, which is available on the Hugging Face <a href="https://huggingface.co/" rel="nofollow">website</a>. You can play with the model directly on this page by inputting custom text and watching the model process the input data.</p> <p>The Inference API that powers the widget is also available as a paid product, which comes in handy if you need it for your workflows. See the <a href="https://huggingface.co/pricing" rel="nofollow">pricing page</a> for more details.</p> <h2 class="relative group"><a id="mask-filling" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#mask-filling"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Mask filling</span></h2> <p>The next pipeline you’ll try is <code>fill-mask</code>. The idea of this task is to fill in the blanks in a given text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline unmasker = pipeline(<span class="hljs-string">"fill-mask"</span>) unmasker(<span class="hljs-string">"This course will teach you all about &lt;mask&gt; models."</span>, top_k=<span class="hljs-number">2</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'sequence'</span>: <span class="hljs-string">'This course will teach you all about mathematical models.'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.19619831442832947</span>, <span class="hljs-string">'token'</span>: <span class="hljs-number">30412</span>, <span class="hljs-string">'token_str'</span>: <span class="hljs-string">' mathematical'</span>}, {<span class="hljs-string">'sequence'</span>: <span class="hljs-string">'This course will teach you all about computational models.'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.04052725434303284</span>, <span class="hljs-string">'token'</span>: <span class="hljs-number">38163</span>, <span class="hljs-string">'token_str'</span>: <span class="hljs-string">' computational'</span>}]</pre></div> <p>The <code>top_k</code> argument controls how many possibilities you want to be displayed. Note that here the model fills in the special <code>&lt;mask&gt;</code> word, which is often referred to as a <em>mask token</em>. Other mask-filling models might have different mask tokens, so it’s always good to verify the proper mask word when exploring other models. One way to check it is by looking at the mask word used in the widget.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Search for the <code>bert-base-cased</code> model on the Hub and identify its mask word in the Inference API widget. What does this model predict for the sentence in our <code>pipeline</code> example above?</p></div> <h2 class="relative group"><a id="named-entity-recognition" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#named-entity-recognition"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Named entity recognition</span></h2> <p>Named entity recognition (NER) is a task where the model has to find which parts of the input text correspond to entities such as persons, locations, or organizations. Let’s look at an example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline ner = pipeline(<span class="hljs-string">"ner"</span>, grouped_entities=<span class="hljs-literal">True</span>) ner(<span class="hljs-string">"My name is Sylvain and I work at Hugging Face in Brooklyn."</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'entity_group'</span>: <span class="hljs-string">'PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99816</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Sylvain'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">11</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">18</span>}, {<span class="hljs-string">'entity_group'</span>: <span class="hljs-string">'ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.97960</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Hugging Face'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">33</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">45</span>}, {<span class="hljs-string">'entity_group'</span>: <span class="hljs-string">'LOC'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99321</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Brooklyn'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">49</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">57</span>} ]</pre></div> <p>Here the model correctly identified that Sylvain is a person (PER), Hugging Face an organization (ORG), and Brooklyn a location (LOC).</p> <p>We pass the option <code>grouped_entities=True</code> in the pipeline creation function to tell the pipeline to regroup together the parts of the sentence that correspond to the same entity: here the model correctly grouped “Hugging” and “Face” as a single organization, even though the name consists of multiple words. In fact, as we will see in the next chapter, the preprocessing even splits some words into smaller parts. For instance, <code>Sylvain</code> is split into four pieces: <code>S</code>, <code>##yl</code>, <code>##va</code>, and <code>##in</code>. In the post-processing step, the pipeline successfully regrouped those pieces.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Search the Model Hub for a model able to do part-of-speech tagging (usually abbreviated as POS) in English. What does this model predict for the sentence in the example above?</p></div> <h2 class="relative group"><a id="question-answering" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#question-answering"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Question answering</span></h2> <p>The <code>question-answering</code> pipeline answers questions using information from a given context:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline question_answerer = pipeline(<span class="hljs-string">"question-answering"</span>) question_answerer( question=<span class="hljs-string">"Where do I work?"</span>, context=<span class="hljs-string">"My name is Sylvain and I work at Hugging Face in Brooklyn"</span>, )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'score'</span>: <span class="hljs-number">0.6385916471481323</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">33</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">45</span>, <span class="hljs-string">'answer'</span>: <span class="hljs-string">'Hugging Face'</span>}</pre></div> <p>Note that this pipeline works by extracting information from the provided context; it does not generate the answer.</p> <h2 class="relative group"><a id="summarization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#summarization"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Summarization</span></h2> <p>Summarization is the task of reducing a text into a shorter text while keeping all (or most) of the important aspects referenced in the text. Here’s an example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline summarizer = pipeline(<span class="hljs-string">"summarization"</span>) summarizer( <span class="hljs-string">""" America has changed dramatically during recent years. Not only has the number of graduates in traditional engineering disciplines such as mechanical, civil, electrical, chemical, and aeronautical engineering declined, but in most of the premier American universities engineering curricula now concentrate on and encourage largely the study of engineering science. As a result, there are declining offerings in engineering subjects dealing with infrastructure, the environment, and related issues, and greater concentration on high technology subjects, largely supporting increasingly complex scientific developments. While the latter is important, it should not be at the expense of more traditional engineering. Rapidly developing economies such as China and India, as well as other industrial countries in Europe and Asia, continue to encourage and advance the teaching of engineering. Both China and India, respectively, graduate six and eight times as many traditional engineers as does the United States. Other industrial countries at minimum maintain their output, while America suffers an increasingly serious decline in the number of engineering graduates and a lack of well-educated engineers. """</span> )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'summary_text'</span>: <span class="hljs-string">' America has changed dramatically during recent years . The '</span> <span class="hljs-string">'number of engineering graduates in the U.S. has declined in '</span> <span class="hljs-string">'traditional engineering disciplines such as mechanical, civil '</span> <span class="hljs-string">', electrical, chemical, and aeronautical engineering . Rapidly '</span> <span class="hljs-string">'developing economies such as China and India, as well as other '</span> <span class="hljs-string">'industrial countries in Europe and Asia, continue to encourage '</span> <span class="hljs-string">'and advance engineering .'</span>}]</pre></div> <p>Like with text generation, you can specify a <code>max_length</code> or a <code>min_length</code> for the result.</p> <h2 class="relative group"><a id="translation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#translation"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Translation</span></h2> <p>For translation, you can use a default model if you provide a language pair in the task name (such as <code>"translation_en_to_fr"</code>), but the easiest way is to pick the model you want to use on the <a href="https://huggingface.co/models" rel="nofollow">Model Hub</a>. Here we’ll try translating from French to English:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline translator = pipeline(<span class="hljs-string">"translation"</span>, model=<span class="hljs-string">"Helsinki-NLP/opus-mt-fr-en"</span>) translator(<span class="hljs-string">"Ce cours est produit par Hugging Face."</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'translation_text'</span>: <span class="hljs-string">'This course is produced by Hugging Face.'</span>}]</pre></div> <p>Like with text generation and summarization, you can specify a <code>max_length</code> or a <code>min_length</code> for the result.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Search for translation models in other languages and try to translate the previous sentence into a few different languages.</p></div> <p>The pipelines shown so far are mostly for demonstrative purposes. They were programmed for specific tasks and cannot perform variations of them. In the next chapter, you’ll learn what’s inside a <code>pipeline()</code> function and how to customize its behavior.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter1/2?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Natural Language Processing</a> <a href="/learn/nlp-course/chapter1/4?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">How do Transformers work?<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;transformers-what-can-they-do&quot;,&quot;url&quot;:&quot;#transformers-what-can-they-do&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Transformers are everywhere!&quot;,&quot;id&quot;:&quot;transformers-are-everywhere&quot;,&quot;url&quot;:&quot;#transformers-are-everywhere&quot;},{&quot;title&quot;:&quot;Working with pipelines&quot;,&quot;id&quot;:&quot;working-with-pipelines&quot;,&quot;url&quot;:&quot;#working-with-pipelines&quot;},{&quot;title&quot;:&quot;Zero-shot classification&quot;,&quot;id&quot;:&quot;zero-shot-classification&quot;,&quot;url&quot;:&quot;#zero-shot-classification&quot;},{&quot;title&quot;:&quot;Text generation&quot;,&quot;id&quot;:&quot;text-generation&quot;,&quot;url&quot;:&quot;#text-generation&quot;},{&quot;title&quot;:&quot;Using any model from the Hub in a pipeline&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;using-any-model-from-the-hub-in-a-pipeline&quot;,&quot;url&quot;:&quot;#using-any-model-from-the-hub-in-a-pipeline&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Inference API&quot;,&quot;id&quot;:&quot;the-inference-api&quot;,&quot;url&quot;:&quot;#the-inference-api&quot;}]},{&quot;title&quot;:&quot;Mask filling&quot;,&quot;id&quot;:&quot;mask-filling&quot;,&quot;url&quot;:&quot;#mask-filling&quot;},{&quot;title&quot;:&quot;Named entity recognition&quot;,&quot;id&quot;:&quot;named-entity-recognition&quot;,&quot;url&quot;:&quot;#named-entity-recognition&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;question-answering&quot;,&quot;url&quot;:&quot;#question-answering&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;summarization&quot;,&quot;url&quot;:&quot;#summarization&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;translation&quot;,&quot;url&quot;:&quot;#translation&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#transformers-what-can-they-do" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-transformers-what-can-they-do"><wbr>Transformers, what can they do?</a> <a href="#transformers-are-everywhere" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-transformers-are-everywhere"><wbr>Transformers are everywhere!</a> <a href="#working-with-pipelines" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-working-with-pipelines"><wbr>Working with pipelines</a> <a href="#zero-shot-classification" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-zero-shot-classification"><wbr>Zero-shot classification</a> <a href="#text-generation" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-text-generation"><wbr>Text generation</a> <a href="#using-any-model-from-the-hub-in-a-pipeline" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-any-model-from-the-hub-in-a-pipeline"><wbr>Using any model from the <wbr>Hub in a pipeline</a> <a href="#the-inference-api" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-inference-api"><wbr>The <wbr>Inference API</a> <a href="#mask-filling" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-mask-filling"><wbr>Mask filling</a> <a href="#named-entity-recognition" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-named-entity-recognition"><wbr>Named entity recognition</a> <a href="#question-answering" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-question-answering"><wbr>Question answering</a> <a href="#summarization" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-summarization"><wbr>Summarization</a> <a href="#translation" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-translation"><wbr>Translation</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter1/3" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter1/3"); } </script> <iframe name="__privateStripeMetricsController0640" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter1%2F3%3Ffw%3Dpt&amp;title=Transformers%2C%20what%20can%20they%20do%3F%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:03.574Z
Natural Language Processing - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter1/2?fw=pt
NLP Course documentation Natural Language Processing 3\. Fine-tuning a pretrained model 4\. Sharing models and tokenizers 5\. The 🤗 Datasets library 6\. The 🤗 Tokenizers library 9\. Building and sharing demos new ## [](#natural-language-processing)Natural Language Processing [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-1-questions) Before jumping into Transformer models, let’s do a quick overview of what natural language processing is and why we care about it. ## [](#what-is-nlp)What is NLP? NLP is a field of linguistics and machine learning focused on understanding everything related to human language. The aim of NLP tasks is not only to understand single words individually, but to be able to understand the context of those words. The following is a list of common NLP tasks, with some examples of each: - **Classifying whole sentences**: Getting the sentiment of a review, detecting if an email is spam, determining if a sentence is grammatically correct or whether two sentences are logically related or not - **Classifying each word in a sentence**: Identifying the grammatical components of a sentence (noun, verb, adjective), or the named entities (person, location, organization) - **Generating text content**: Completing a prompt with auto-generated text, filling in the blanks in a text with masked words - **Extracting an answer from a text**: Given a question and a context, extracting the answer to the question based on the information provided in the context - **Generating a new sentence from an input text**: Translating a text into another language, summarizing a text NLP isn’t limited to written text though. It also tackles complex challenges in speech recognition and computer vision, such as generating a transcript of an audio sample or a description of an image. ## [](#why-is-it-challenging)Why is it challenging? Computers don’t process information in the same way as humans. For example, when we read the sentence “I am hungry,” we can easily understand its meaning. Similarly, given two sentences such as “I am hungry” and “I am sad,” we’re able to easily determine how similar they are. For machine learning (ML) models, such tasks are more difficult. The text needs to be processed in a way that enables the model to learn from it. And because language is complex, we need to think carefully about how this processing must be done. There has been a lot of research done on how to represent text, and we will look at some methods in the next chapter.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Natural Language Processing - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter1/2"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Natural Language Processing - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;natural-language-processing&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;what-is-nlp&quot;,&quot;title&quot;:&quot;What is NLP?&quot;},{&quot;local&quot;:&quot;why-is-it-challenging&quot;,&quot;title&quot;:&quot;Why is it challenging?&quot;}],&quot;title&quot;:&quot;Natural Language Processing&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter1/2&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Natural Language Processing&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Natural Language Processing</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/1?fw=pt">Introduction </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter1/2?fw=pt">Natural Language Processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/3?fw=pt">Transformers, what can they do? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/4?fw=pt">How do Transformers work? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/5?fw=pt">Encoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/6?fw=pt">Decoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/7?fw=pt">Sequence-to-sequence models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/8?fw=pt">Bias and limitations </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/9?fw=pt">Summary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="natural-language-processing" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#natural-language-processing"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Natural Language Processing</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-1-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>Before jumping into Transformer models, let’s do a quick overview of what natural language processing is and why we care about it.</p> <h2 class="relative group"><a id="what-is-nlp" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#what-is-nlp"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>What is NLP?</span></h2> <p>NLP is a field of linguistics and machine learning focused on understanding everything related to human language. The aim of NLP tasks is not only to understand single words individually, but to be able to understand the context of those words.</p> <p>The following is a list of common NLP tasks, with some examples of each:</p> <ul><li><strong>Classifying whole sentences</strong>: Getting the sentiment of a review, detecting if an email is spam, determining if a sentence is grammatically correct or whether two sentences are logically related or not</li> <li><strong>Classifying each word in a sentence</strong>: Identifying the grammatical components of a sentence (noun, verb, adjective), or the named entities (person, location, organization)</li> <li><strong>Generating text content</strong>: Completing a prompt with auto-generated text, filling in the blanks in a text with masked words</li> <li><strong>Extracting an answer from a text</strong>: Given a question and a context, extracting the answer to the question based on the information provided in the context</li> <li><strong>Generating a new sentence from an input text</strong>: Translating a text into another language, summarizing a text</li></ul> <p>NLP isn’t limited to written text though. It also tackles complex challenges in speech recognition and computer vision, such as generating a transcript of an audio sample or a description of an image.</p> <h2 class="relative group"><a id="why-is-it-challenging" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#why-is-it-challenging"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Why is it challenging?</span></h2> <p>Computers don’t process information in the same way as humans. For example, when we read the sentence “I am hungry,” we can easily understand its meaning. Similarly, given two sentences such as “I am hungry” and “I am sad,” we’re able to easily determine how similar they are. For machine learning (ML) models, such tasks are more difficult. The text needs to be processed in a way that enables the model to learn from it. And because language is complex, we need to think carefully about how this processing must be done. There has been a lot of research done on how to represent text, and we will look at some methods in the next chapter.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter1/1?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Introduction</a> <a href="/learn/nlp-course/chapter1/3?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Transformers, what can they do?<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;natural-language-processing&quot;,&quot;url&quot;:&quot;#natural-language-processing&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;What is NLP?&quot;,&quot;id&quot;:&quot;what-is-nlp&quot;,&quot;url&quot;:&quot;#what-is-nlp&quot;},{&quot;title&quot;:&quot;Why is it challenging?&quot;,&quot;id&quot;:&quot;why-is-it-challenging&quot;,&quot;url&quot;:&quot;#why-is-it-challenging&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#natural-language-processing" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-natural-language-processing"><wbr>Natural <wbr>Language <wbr>Processing</a> <a href="#what-is-nlp" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-what-is-nlp"><wbr>What is NL<wbr>P?</a> <a href="#why-is-it-challenging" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-why-is-it-challenging"><wbr>Why is it challenging?</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter1/2" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter1/2"); } </script> <iframe name="__privateStripeMetricsController3570" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter1%2F2%3Ffw%3Dpt&amp;title=Natural%20Language%20Processing%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:03.635Z
Encoder models - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter1/5?fw=pt
## [](#encoder-models)Encoder models [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-1-questions) Encoder models use only the encoder of a Transformer model. At each stage, the attention layers can access all the words in the initial sentence. These models are often characterized as having “bi-directional” attention, and are often called _auto-encoding models_. The pretraining of these models usually revolves around somehow corrupting a given sentence (for instance, by masking random words in it) and tasking the model with finding or reconstructing the initial sentence. Encoder models are best suited for tasks requiring an understanding of the full sentence, such as sentence classification, named entity recognition (and more generally word classification), and extractive question answering. Representatives of this family of models include: - [ALBERT](https://huggingface.co/transformers/model_doc/albert.html) - [BERT](https://huggingface.co/transformers/model_doc/bert.html) - [DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html) - [ELECTRA](https://huggingface.co/transformers/model_doc/electra.html) - [RoBERTa](https://huggingface.co/transformers/model_doc/roberta.html)
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Encoder models - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter1/5"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Encoder models - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;encoder-models&quot;,&quot;title&quot;:&quot;Encoder models&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter1/5&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Encoder models&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Encoder models</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/2?fw=pt">Natural Language Processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/3?fw=pt">Transformers, what can they do? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/4?fw=pt">How do Transformers work? </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter1/5?fw=pt">Encoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/6?fw=pt">Decoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/7?fw=pt">Sequence-to-sequence models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/8?fw=pt">Bias and limitations </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/9?fw=pt">Summary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="encoder-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#encoder-models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Encoder models</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-1-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/MUqNwgPjJvQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Encoder models use only the encoder of a Transformer model. At each stage, the attention layers can access all the words in the initial sentence. These models are often characterized as having “bi-directional” attention, and are often called <em>auto-encoding models</em>.</p> <p>The pretraining of these models usually revolves around somehow corrupting a given sentence (for instance, by masking random words in it) and tasking the model with finding or reconstructing the initial sentence.</p> <p>Encoder models are best suited for tasks requiring an understanding of the full sentence, such as sentence classification, named entity recognition (and more generally word classification), and extractive question answering.</p> <p>Representatives of this family of models include:</p> <ul><li><a href="https://huggingface.co/transformers/model_doc/albert.html" rel="nofollow">ALBERT</a></li> <li><a href="https://huggingface.co/transformers/model_doc/bert.html" rel="nofollow">BERT</a></li> <li><a href="https://huggingface.co/transformers/model_doc/distilbert.html" rel="nofollow">DistilBERT</a></li> <li><a href="https://huggingface.co/transformers/model_doc/electra.html" rel="nofollow">ELECTRA</a></li> <li><a href="https://huggingface.co/transformers/model_doc/roberta.html" rel="nofollow">RoBERTa</a></li></ul> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter1/4?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>How do Transformers work?</a> <a href="/learn/nlp-course/chapter1/6?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Decoder models<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;encoder-models&quot;,&quot;url&quot;:&quot;#encoder-models&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#encoder-models" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-encoder-models"><wbr>Encoder models</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter1/5" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter1/5"); } </script> <iframe name="__privateStripeMetricsController8150" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter1%2F5%3Ffw%3Dpt&amp;title=Encoder%20models%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:04.874Z
How do Transformers work? - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter1/4?fw=pt
## [](#how-do-transformers-work)How do Transformers work? [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-1-questions) In this section, we will take a high-level look at the architecture of Transformer models. ## [](#a-bit-of-transformer-history)A bit of Transformer history Here are some reference points in the (short) history of Transformer models: ![A brief chronology of Transformers models.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers_chrono.svg) ![A brief chronology of Transformers models.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers_chrono-dark.svg) The [Transformer architecture](https://arxiv.org/abs/1706.03762) was introduced in June 2017. The focus of the original research was on translation tasks. This was followed by the introduction of several influential models, including: - **June 2018**: [GPT](https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf), the first pretrained Transformer model, used for fine-tuning on various NLP tasks and obtained state-of-the-art results - **October 2018**: [BERT](https://arxiv.org/abs/1810.04805), another large pretrained model, this one designed to produce better summaries of sentences (more on this in the next chapter!) - **February 2019**: [GPT-2](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf), an improved (and bigger) version of GPT that was not immediately publicly released due to ethical concerns - **October 2019**: [DistilBERT](https://arxiv.org/abs/1910.01108), a distilled version of BERT that is 60% faster, 40% lighter in memory, and still retains 97% of BERT’s performance - **October 2019**: [BART](https://arxiv.org/abs/1910.13461) and [T5](https://arxiv.org/abs/1910.10683), two large pretrained models using the same architecture as the original Transformer model (the first to do so) - **May 2020**, [GPT-3](https://arxiv.org/abs/2005.14165), an even bigger version of GPT-2 that is able to perform well on a variety of tasks without the need for fine-tuning (called _zero-shot learning_) This list is far from comprehensive, and is just meant to highlight a few of the different kinds of Transformer models. Broadly, they can be grouped into three categories: - GPT-like (also called _auto-regressive_ Transformer models) - BERT-like (also called _auto-encoding_ Transformer models) - BART/T5-like (also called _sequence-to-sequence_ Transformer models) We will dive into these families in more depth later on. ## [](#transformers-are-language-models)Transformers are language models All the Transformer models mentioned above (GPT, BERT, BART, T5, etc.) have been trained as _language models_. This means they have been trained on large amounts of raw text in a self-supervised fashion. Self-supervised learning is a type of training in which the objective is automatically computed from the inputs of the model. That means that humans are not needed to label the data! This type of model develops a statistical understanding of the language it has been trained on, but it’s not very useful for specific practical tasks. Because of this, the general pretrained model then goes through a process called _transfer learning_. During this process, the model is fine-tuned in a supervised way — that is, using human-annotated labels — on a given task. An example of a task is predicting the next word in a sentence having read the _n_ previous words. This is called _causal language modeling_ because the output depends on the past and present inputs, but not the future ones. ![Example of causal language modeling in which the next word from a sentence is predicted.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/causal_modeling.svg) ![Example of causal language modeling in which the next word from a sentence is predicted.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/causal_modeling-dark.svg) Another example is _masked language modeling_, in which the model predicts a masked word in the sentence. ![Example of masked language modeling in which a masked word from a sentence is predicted.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/masked_modeling.svg) ![Example of masked language modeling in which a masked word from a sentence is predicted.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/masked_modeling-dark.svg) ## [](#transformers-are-big-models)Transformers are big models Apart from a few outliers (like DistilBERT), the general strategy to achieve better performance is by increasing the models’ sizes as well as the amount of data they are pretrained on. ![Number of parameters of recent Transformers models](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/model_parameters.png) Unfortunately, training a model, especially a large one, requires a large amount of data. This becomes very costly in terms of time and compute resources. It even translates to environmental impact, as can be seen in the following graph. ![The carbon footprint of a large language model.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/carbon_footprint.svg) ![The carbon footprint of a large language model.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/carbon_footprint-dark.svg) And this is showing a project for a (very big) model led by a team consciously trying to reduce the environmental impact of pretraining. The footprint of running lots of trials to get the best hyperparameters would be even higher. Imagine if each time a research team, a student organization, or a company wanted to train a model, it did so from scratch. This would lead to huge, unnecessary global costs! This is why sharing language models is paramount: sharing the trained weights and building on top of already trained weights reduces the overall compute cost and carbon footprint of the community. By the way, you can evaluate the carbon footprint of your models’ training through several tools. For example [ML CO2 Impact](https://mlco2.github.io/impact/) or [Code Carbon](https://codecarbon.io/) which is integrated in 🤗 Transformers. To learn more about this, you can read this [blog post](https://huggingface.co/blog/carbon-emissions-on-the-hub) which will show you how to generate an `emissions.csv` file with an estimate of the footprint of your training, as well as the [documentation](https://huggingface.co/docs/hub/model-cards-co2) of 🤗 Transformers addressing this topic. ## [](#transfer-learning)Transfer Learning _Pretraining_ is the act of training a model from scratch: the weights are randomly initialized, and the training starts without any prior knowledge. ![The pretraining of a language model is costly in both time and money.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/pretraining.svg) ![The pretraining of a language model is costly in both time and money.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/pretraining-dark.svg) This pretraining is usually done on very large amounts of data. Therefore, it requires a very large corpus of data, and training can take up to several weeks. _Fine-tuning_, on the other hand, is the training done **after** a model has been pretrained. To perform fine-tuning, you first acquire a pretrained language model, then perform additional training with a dataset specific to your task. Wait — why not simply train directly for the final task? There are a couple of reasons: - The pretrained model was already trained on a dataset that has some similarities with the fine-tuning dataset. The fine-tuning process is thus able to take advantage of knowledge acquired by the initial model during pretraining (for instance, with NLP problems, the pretrained model will have some kind of statistical understanding of the language you are using for your task). - Since the pretrained model was already trained on lots of data, the fine-tuning requires way less data to get decent results. - For the same reason, the amount of time and resources needed to get good results are much lower. For example, one could leverage a pretrained model trained on the English language and then fine-tune it on an arXiv corpus, resulting in a science/research-based model. The fine-tuning will only require a limited amount of data: the knowledge the pretrained model has acquired is “transferred,” hence the term _transfer learning_. ![The fine-tuning of a language model is cheaper than pretraining in both time and money.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/finetuning.svg) ![The fine-tuning of a language model is cheaper than pretraining in both time and money.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/finetuning-dark.svg) Fine-tuning a model therefore has lower time, data, financial, and environmental costs. It is also quicker and easier to iterate over different fine-tuning schemes, as the training is less constraining than a full pretraining. This process will also achieve better results than training from scratch (unless you have lots of data), which is why you should always try to leverage a pretrained model — one as close as possible to the task you have at hand — and fine-tune it. ## [](#general-architecture)General architecture In this section, we’ll go over the general architecture of the Transformer model. Don’t worry if you don’t understand some of the concepts; there are detailed sections later covering each of the components. ## [](#introduction)Introduction The model is primarily composed of two blocks: - **Encoder (left)**: The encoder receives an input and builds a representation of it (its features). This means that the model is optimized to acquire understanding from the input. - **Decoder (right)**: The decoder uses the encoder’s representation (features) along with other inputs to generate a target sequence. This means that the model is optimized for generating outputs. ![Architecture of a Transformers models](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers_blocks.svg) ![Architecture of a Transformers models](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers_blocks-dark.svg) Each of these parts can be used independently, depending on the task: - **Encoder-only models**: Good for tasks that require understanding of the input, such as sentence classification and named entity recognition. - **Decoder-only models**: Good for generative tasks such as text generation. - **Encoder-decoder models** or **sequence-to-sequence models**: Good for generative tasks that require an input, such as translation or summarization. We will dive into those architectures independently in later sections. ## [](#attention-layers)Attention layers A key feature of Transformer models is that they are built with special layers called _attention layers_. In fact, the title of the paper introducing the Transformer architecture was [“Attention Is All You Need”](https://arxiv.org/abs/1706.03762)! We will explore the details of attention layers later in the course; for now, all you need to know is that this layer will tell the model to pay specific attention to certain words in the sentence you passed it (and more or less ignore the others) when dealing with the representation of each word. To put this into context, consider the task of translating text from English to French. Given the input “You like this course”, a translation model will need to also attend to the adjacent word “You” to get the proper translation for the word “like”, because in French the verb “like” is conjugated differently depending on the subject. The rest of the sentence, however, is not useful for the translation of that word. In the same vein, when translating “this” the model will also need to pay attention to the word “course”, because “this” translates differently depending on whether the associated noun is masculine or feminine. Again, the other words in the sentence will not matter for the translation of “this”. With more complex sentences (and more complex grammar rules), the model would need to pay special attention to words that might appear farther away in the sentence to properly translate each word. The same concept applies to any task associated with natural language: a word by itself has a meaning, but that meaning is deeply affected by the context, which can be any other word (or words) before or after the word being studied. Now that you have an idea of what attention layers are all about, let’s take a closer look at the Transformer architecture. ## [](#the-original-architecture)The original architecture The Transformer architecture was originally designed for translation. During training, the encoder receives inputs (sentences) in a certain language, while the decoder receives the same sentences in the desired target language. In the encoder, the attention layers can use all the words in a sentence (since, as we just saw, the translation of a given word can be dependent on what is after as well as before it in the sentence). The decoder, however, works sequentially and can only pay attention to the words in the sentence that it has already translated (so, only the words before the word currently being generated). For example, when we have predicted the first three words of the translated target, we give them to the decoder which then uses all the inputs of the encoder to try to predict the fourth word. To speed things up during training (when the model has access to target sentences), the decoder is fed the whole target, but it is not allowed to use future words (if it had access to the word at position 2 when trying to predict the word at position 2, the problem would not be very hard!). For instance, when trying to predict the fourth word, the attention layer will only have access to the words in positions 1 to 3. The original Transformer architecture looked like this, with the encoder on the left and the decoder on the right: ![Architecture of a Transformers models](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers.svg) ![Architecture of a Transformers models](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers-dark.svg) Note that the first attention layer in a decoder block pays attention to all (past) inputs to the decoder, but the second attention layer uses the output of the encoder. It can thus access the whole input sentence to best predict the current word. This is very useful as different languages can have grammatical rules that put the words in different orders, or some context provided later in the sentence may be helpful to determine the best translation of a given word. The _attention mask_ can also be used in the encoder/decoder to prevent the model from paying attention to some special words — for instance, the special padding word used to make all the inputs the same length when batching together sentences. ## [](#architecture-vs-checkpoints)Architectures vs. checkpoints As we dive into Transformer models in this course, you’ll see mentions of _architectures_ and _checkpoints_ as well as _models_. These terms all have slightly different meanings: - **Architecture**: This is the skeleton of the model — the definition of each layer and each operation that happens within the model. - **Checkpoints**: These are the weights that will be loaded in a given architecture. - **Model**: This is an umbrella term that isn’t as precise as “architecture” or “checkpoint”: it can mean both. This course will specify _architecture_ or _checkpoint_ when it matters to reduce ambiguity. For example, BERT is an architecture while `bert-base-cased`, a set of weights trained by the Google team for the first release of BERT, is a checkpoint. However, one can say “the BERT model” and “the `bert-base-cased` model.”
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="How do Transformers work? - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter1/4"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>How do Transformers work? - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;how-do-transformers-work&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;a-bit-of-transformer-history&quot;,&quot;title&quot;:&quot;A bit of Transformer history&quot;},{&quot;local&quot;:&quot;transformers-are-language-models&quot;,&quot;title&quot;:&quot;Transformers are language models&quot;},{&quot;local&quot;:&quot;transformers-are-big-models&quot;,&quot;title&quot;:&quot;Transformers are big models&quot;},{&quot;local&quot;:&quot;transfer-learning&quot;,&quot;title&quot;:&quot;Transfer Learning&quot;},{&quot;local&quot;:&quot;general-architecture&quot;,&quot;title&quot;:&quot;General architecture&quot;},{&quot;local&quot;:&quot;introduction&quot;,&quot;title&quot;:&quot;Introduction&quot;},{&quot;local&quot;:&quot;attention-layers&quot;,&quot;title&quot;:&quot;Attention layers&quot;},{&quot;local&quot;:&quot;the-original-architecture&quot;,&quot;title&quot;:&quot;The original architecture&quot;},{&quot;local&quot;:&quot;architecture-vs-checkpoints&quot;,&quot;title&quot;:&quot;Architectures vs. checkpoints&quot;}],&quot;title&quot;:&quot;How do Transformers work?&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter1/4&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;How do Transformers work?&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">How do Transformers work?</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/2?fw=pt">Natural Language Processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/3?fw=pt">Transformers, what can they do? </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter1/4?fw=pt">How do Transformers work? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/5?fw=pt">Encoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/6?fw=pt">Decoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/7?fw=pt">Sequence-to-sequence models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/8?fw=pt">Bias and limitations </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/9?fw=pt">Summary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="how-do-transformers-work" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#how-do-transformers-work"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>How do Transformers work?</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-1-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>In this section, we will take a high-level look at the architecture of Transformer models.</p> <h2 class="relative group"><a id="a-bit-of-transformer-history" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#a-bit-of-transformer-history"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>A bit of Transformer history</span></h2> <p>Here are some reference points in the (short) history of Transformer models:</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers_chrono.svg" alt="A brief chronology of Transformers models."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers_chrono-dark.svg" alt="A brief chronology of Transformers models."></div> <p>The <a href="https://arxiv.org/abs/1706.03762" rel="nofollow">Transformer architecture</a> was introduced in June 2017. The focus of the original research was on translation tasks. This was followed by the introduction of several influential models, including:</p> <ul><li><p><strong>June 2018</strong>: <a href="https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf" rel="nofollow">GPT</a>, the first pretrained Transformer model, used for fine-tuning on various NLP tasks and obtained state-of-the-art results</p></li> <li><p><strong>October 2018</strong>: <a href="https://arxiv.org/abs/1810.04805" rel="nofollow">BERT</a>, another large pretrained model, this one designed to produce better summaries of sentences (more on this in the next chapter!)</p></li> <li><p><strong>February 2019</strong>: <a href="https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf" rel="nofollow">GPT-2</a>, an improved (and bigger) version of GPT that was not immediately publicly released due to ethical concerns</p></li> <li><p><strong>October 2019</strong>: <a href="https://arxiv.org/abs/1910.01108" rel="nofollow">DistilBERT</a>, a distilled version of BERT that is 60% faster, 40% lighter in memory, and still retains 97% of BERT’s performance</p></li> <li><p><strong>October 2019</strong>: <a href="https://arxiv.org/abs/1910.13461" rel="nofollow">BART</a> and <a href="https://arxiv.org/abs/1910.10683" rel="nofollow">T5</a>, two large pretrained models using the same architecture as the original Transformer model (the first to do so)</p></li> <li><p><strong>May 2020</strong>, <a href="https://arxiv.org/abs/2005.14165" rel="nofollow">GPT-3</a>, an even bigger version of GPT-2 that is able to perform well on a variety of tasks without the need for fine-tuning (called <em>zero-shot learning</em>)</p></li></ul> <p>This list is far from comprehensive, and is just meant to highlight a few of the different kinds of Transformer models. Broadly, they can be grouped into three categories:</p> <ul><li>GPT-like (also called <em>auto-regressive</em> Transformer models)</li> <li>BERT-like (also called <em>auto-encoding</em> Transformer models)</li> <li>BART/T5-like (also called <em>sequence-to-sequence</em> Transformer models)</li></ul> <p>We will dive into these families in more depth later on.</p> <h2 class="relative group"><a id="transformers-are-language-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers-are-language-models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Transformers are language models</span></h2> <p>All the Transformer models mentioned above (GPT, BERT, BART, T5, etc.) have been trained as <em>language models</em>. This means they have been trained on large amounts of raw text in a self-supervised fashion. Self-supervised learning is a type of training in which the objective is automatically computed from the inputs of the model. That means that humans are not needed to label the data!</p> <p>This type of model develops a statistical understanding of the language it has been trained on, but it’s not very useful for specific practical tasks. Because of this, the general pretrained model then goes through a process called <em>transfer learning</em>. During this process, the model is fine-tuned in a supervised way — that is, using human-annotated labels — on a given task.</p> <p>An example of a task is predicting the next word in a sentence having read the <em>n</em> previous words. This is called <em>causal language modeling</em> because the output depends on the past and present inputs, but not the future ones.</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/causal_modeling.svg" alt="Example of causal language modeling in which the next word from a sentence is predicted."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/causal_modeling-dark.svg" alt="Example of causal language modeling in which the next word from a sentence is predicted."></div> <p>Another example is <em>masked language modeling</em>, in which the model predicts a masked word in the sentence.</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/masked_modeling.svg" alt="Example of masked language modeling in which a masked word from a sentence is predicted."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/masked_modeling-dark.svg" alt="Example of masked language modeling in which a masked word from a sentence is predicted."></div> <h2 class="relative group"><a id="transformers-are-big-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers-are-big-models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Transformers are big models</span></h2> <p>Apart from a few outliers (like DistilBERT), the general strategy to achieve better performance is by increasing the models’ sizes as well as the amount of data they are pretrained on.</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/model_parameters.png" alt="Number of parameters of recent Transformers models" width="90%"></div> <p>Unfortunately, training a model, especially a large one, requires a large amount of data. This becomes very costly in terms of time and compute resources. It even translates to environmental impact, as can be seen in the following graph.</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/carbon_footprint.svg" alt="The carbon footprint of a large language model."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/carbon_footprint-dark.svg" alt="The carbon footprint of a large language model."></div> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/ftWlj4FBHTg" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>And this is showing a project for a (very big) model led by a team consciously trying to reduce the environmental impact of pretraining. The footprint of running lots of trials to get the best hyperparameters would be even higher.</p> <p>Imagine if each time a research team, a student organization, or a company wanted to train a model, it did so from scratch. This would lead to huge, unnecessary global costs!</p> <p>This is why sharing language models is paramount: sharing the trained weights and building on top of already trained weights reduces the overall compute cost and carbon footprint of the community.</p> <p>By the way, you can evaluate the carbon footprint of your models’ training through several tools. For example <a href="https://mlco2.github.io/impact/" rel="nofollow">ML CO2 Impact</a> or <a href="https://codecarbon.io/" rel="nofollow">Code Carbon</a> which is integrated in 🤗 Transformers. To learn more about this, you can read this <a href="https://huggingface.co/blog/carbon-emissions-on-the-hub" rel="nofollow">blog post</a> which will show you how to generate an <code>emissions.csv</code> file with an estimate of the footprint of your training, as well as the <a href="https://huggingface.co/docs/hub/model-cards-co2" rel="nofollow">documentation</a> of 🤗 Transformers addressing this topic.</p> <h2 class="relative group"><a id="transfer-learning" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transfer-learning"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Transfer Learning</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/BqqfQnyjmgg" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p><em>Pretraining</em> is the act of training a model from scratch: the weights are randomly initialized, and the training starts without any prior knowledge.</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/pretraining.svg" alt="The pretraining of a language model is costly in both time and money."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/pretraining-dark.svg" alt="The pretraining of a language model is costly in both time and money."></div> <p>This pretraining is usually done on very large amounts of data. Therefore, it requires a very large corpus of data, and training can take up to several weeks.</p> <p><em>Fine-tuning</em>, on the other hand, is the training done <strong>after</strong> a model has been pretrained. To perform fine-tuning, you first acquire a pretrained language model, then perform additional training with a dataset specific to your task. Wait — why not simply train directly for the final task? There are a couple of reasons:</p> <ul><li>The pretrained model was already trained on a dataset that has some similarities with the fine-tuning dataset. The fine-tuning process is thus able to take advantage of knowledge acquired by the initial model during pretraining (for instance, with NLP problems, the pretrained model will have some kind of statistical understanding of the language you are using for your task).</li> <li>Since the pretrained model was already trained on lots of data, the fine-tuning requires way less data to get decent results.</li> <li>For the same reason, the amount of time and resources needed to get good results are much lower.</li></ul> <p>For example, one could leverage a pretrained model trained on the English language and then fine-tune it on an arXiv corpus, resulting in a science/research-based model. The fine-tuning will only require a limited amount of data: the knowledge the pretrained model has acquired is “transferred,” hence the term <em>transfer learning</em>.</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/finetuning.svg" alt="The fine-tuning of a language model is cheaper than pretraining in both time and money."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/finetuning-dark.svg" alt="The fine-tuning of a language model is cheaper than pretraining in both time and money."></div> <p>Fine-tuning a model therefore has lower time, data, financial, and environmental costs. It is also quicker and easier to iterate over different fine-tuning schemes, as the training is less constraining than a full pretraining.</p> <p>This process will also achieve better results than training from scratch (unless you have lots of data), which is why you should always try to leverage a pretrained model — one as close as possible to the task you have at hand — and fine-tune it.</p> <h2 class="relative group"><a id="general-architecture" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#general-architecture"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>General architecture</span></h2> <p>In this section, we’ll go over the general architecture of the Transformer model. Don’t worry if you don’t understand some of the concepts; there are detailed sections later covering each of the components.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/H39Z_720T5s" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <h2 class="relative group"><a id="introduction" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#introduction"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Introduction</span></h2> <p>The model is primarily composed of two blocks:</p> <ul><li><strong>Encoder (left)</strong>: The encoder receives an input and builds a representation of it (its features). This means that the model is optimized to acquire understanding from the input.</li> <li><strong>Decoder (right)</strong>: The decoder uses the encoder’s representation (features) along with other inputs to generate a target sequence. This means that the model is optimized for generating outputs.</li></ul> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers_blocks.svg" alt="Architecture of a Transformers models"> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers_blocks-dark.svg" alt="Architecture of a Transformers models"></div> <p>Each of these parts can be used independently, depending on the task:</p> <ul><li><strong>Encoder-only models</strong>: Good for tasks that require understanding of the input, such as sentence classification and named entity recognition.</li> <li><strong>Decoder-only models</strong>: Good for generative tasks such as text generation.</li> <li><strong>Encoder-decoder models</strong> or <strong>sequence-to-sequence models</strong>: Good for generative tasks that require an input, such as translation or summarization.</li></ul> <p>We will dive into those architectures independently in later sections.</p> <h2 class="relative group"><a id="attention-layers" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#attention-layers"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Attention layers</span></h2> <p>A key feature of Transformer models is that they are built with special layers called <em>attention layers</em>. In fact, the title of the paper introducing the Transformer architecture was <a href="https://arxiv.org/abs/1706.03762" rel="nofollow">“Attention Is All You Need”</a>! We will explore the details of attention layers later in the course; for now, all you need to know is that this layer will tell the model to pay specific attention to certain words in the sentence you passed it (and more or less ignore the others) when dealing with the representation of each word.</p> <p>To put this into context, consider the task of translating text from English to French. Given the input “You like this course”, a translation model will need to also attend to the adjacent word “You” to get the proper translation for the word “like”, because in French the verb “like” is conjugated differently depending on the subject. The rest of the sentence, however, is not useful for the translation of that word. In the same vein, when translating “this” the model will also need to pay attention to the word “course”, because “this” translates differently depending on whether the associated noun is masculine or feminine. Again, the other words in the sentence will not matter for the translation of “this”. With more complex sentences (and more complex grammar rules), the model would need to pay special attention to words that might appear farther away in the sentence to properly translate each word.</p> <p>The same concept applies to any task associated with natural language: a word by itself has a meaning, but that meaning is deeply affected by the context, which can be any other word (or words) before or after the word being studied.</p> <p>Now that you have an idea of what attention layers are all about, let’s take a closer look at the Transformer architecture.</p> <h2 class="relative group"><a id="the-original-architecture" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-original-architecture"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The original architecture</span></h2> <p>The Transformer architecture was originally designed for translation. During training, the encoder receives inputs (sentences) in a certain language, while the decoder receives the same sentences in the desired target language. In the encoder, the attention layers can use all the words in a sentence (since, as we just saw, the translation of a given word can be dependent on what is after as well as before it in the sentence). The decoder, however, works sequentially and can only pay attention to the words in the sentence that it has already translated (so, only the words before the word currently being generated). For example, when we have predicted the first three words of the translated target, we give them to the decoder which then uses all the inputs of the encoder to try to predict the fourth word.</p> <p>To speed things up during training (when the model has access to target sentences), the decoder is fed the whole target, but it is not allowed to use future words (if it had access to the word at position 2 when trying to predict the word at position 2, the problem would not be very hard!). For instance, when trying to predict the fourth word, the attention layer will only have access to the words in positions 1 to 3.</p> <p>The original Transformer architecture looked like this, with the encoder on the left and the decoder on the right:</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers.svg" alt="Architecture of a Transformers models"> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter1/transformers-dark.svg" alt="Architecture of a Transformers models"></div> <p>Note that the first attention layer in a decoder block pays attention to all (past) inputs to the decoder, but the second attention layer uses the output of the encoder. It can thus access the whole input sentence to best predict the current word. This is very useful as different languages can have grammatical rules that put the words in different orders, or some context provided later in the sentence may be helpful to determine the best translation of a given word.</p> <p>The <em>attention mask</em> can also be used in the encoder/decoder to prevent the model from paying attention to some special words — for instance, the special padding word used to make all the inputs the same length when batching together sentences.</p> <h2 class="relative group"><a id="architecture-vs-checkpoints" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#architecture-vs-checkpoints"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Architectures vs. checkpoints</span></h2> <p>As we dive into Transformer models in this course, you’ll see mentions of <em>architectures</em> and <em>checkpoints</em> as well as <em>models</em>. These terms all have slightly different meanings:</p> <ul><li><strong>Architecture</strong>: This is the skeleton of the model — the definition of each layer and each operation that happens within the model.</li> <li><strong>Checkpoints</strong>: These are the weights that will be loaded in a given architecture.</li> <li><strong>Model</strong>: This is an umbrella term that isn’t as precise as “architecture” or “checkpoint”: it can mean both. This course will specify <em>architecture</em> or <em>checkpoint</em> when it matters to reduce ambiguity.</li></ul> <p>For example, BERT is an architecture while <code>bert-base-cased</code>, a set of weights trained by the Google team for the first release of BERT, is a checkpoint. However, one can say “the BERT model” and “the <code>bert-base-cased</code> model.”</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter1/3?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Transformers, what can they do?</a> <a href="/learn/nlp-course/chapter1/5?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Encoder models<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;how-do-transformers-work&quot;,&quot;url&quot;:&quot;#how-do-transformers-work&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;A bit of Transformer history&quot;,&quot;id&quot;:&quot;a-bit-of-transformer-history&quot;,&quot;url&quot;:&quot;#a-bit-of-transformer-history&quot;},{&quot;title&quot;:&quot;Transformers are language models&quot;,&quot;id&quot;:&quot;transformers-are-language-models&quot;,&quot;url&quot;:&quot;#transformers-are-language-models&quot;},{&quot;title&quot;:&quot;Transformers are big models&quot;,&quot;id&quot;:&quot;transformers-are-big-models&quot;,&quot;url&quot;:&quot;#transformers-are-big-models&quot;},{&quot;title&quot;:&quot;Transfer Learning&quot;,&quot;id&quot;:&quot;transfer-learning&quot;,&quot;url&quot;:&quot;#transfer-learning&quot;},{&quot;title&quot;:&quot;General architecture&quot;,&quot;id&quot;:&quot;general-architecture&quot;,&quot;url&quot;:&quot;#general-architecture&quot;},{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;introduction&quot;,&quot;url&quot;:&quot;#introduction&quot;},{&quot;title&quot;:&quot;Attention layers&quot;,&quot;id&quot;:&quot;attention-layers&quot;,&quot;url&quot;:&quot;#attention-layers&quot;},{&quot;title&quot;:&quot;The original architecture&quot;,&quot;id&quot;:&quot;the-original-architecture&quot;,&quot;url&quot;:&quot;#the-original-architecture&quot;},{&quot;title&quot;:&quot;Architectures vs. checkpoints&quot;,&quot;id&quot;:&quot;architecture-vs-checkpoints&quot;,&quot;url&quot;:&quot;#architecture-vs-checkpoints&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#how-do-transformers-work" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-how-do-transformers-work"><wbr>How do <wbr>Transformers work?</a> <a href="#a-bit-of-transformer-history" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-a-bit-of-transformer-history"><wbr>A bit of <wbr>Transformer history</a> <a href="#transformers-are-language-models" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-transformers-are-language-models"><wbr>Transformers are language models</a> <a href="#transformers-are-big-models" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-transformers-are-big-models"><wbr>Transformers are big models</a> <a href="#transfer-learning" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-transfer-learning"><wbr>Transfer <wbr>Learning</a> <a href="#general-architecture" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-general-architecture"><wbr>General architecture</a> <a href="#introduction" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-introduction"><wbr>Introduction</a> <a href="#attention-layers" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-attention-layers"><wbr>Attention layers</a> <a href="#the-original-architecture" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-original-architecture"><wbr>The original architecture</a> <a href="#architecture-vs-checkpoints" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-architecture-vs-checkpoints"><wbr>Architectures vs. checkpoints</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter1/4" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter1/4"); } </script> <iframe name="__privateStripeMetricsController0070" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter1%2F4%3Ffw%3Dpt&amp;title=How%20do%20Transformers%20work%3F%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:04.982Z
Decoder models - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter1/6?fw=pt
## [](#decoder-models)Decoder models [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-1-questions) Decoder models use only the decoder of a Transformer model. At each stage, for a given word the attention layers can only access the words positioned before it in the sentence. These models are often called _auto-regressive models_. The pretraining of decoder models usually revolves around predicting the next word in the sentence. These models are best suited for tasks involving text generation. Representatives of this family of models include: - [CTRL](https://huggingface.co/transformers/model_doc/ctrl.html) - [GPT](https://huggingface.co/docs/transformers/model_doc/openai-gpt) - [GPT-2](https://huggingface.co/transformers/model_doc/gpt2.html) - [Transformer XL](https://huggingface.co/transformers/model_doc/transfo-xl.html)
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Decoder models - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter1/6"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Decoder models - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;decoder-models&quot;,&quot;title&quot;:&quot;Decoder models&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter1/6&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Decoder models&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Decoder models</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/2?fw=pt">Natural Language Processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/3?fw=pt">Transformers, what can they do? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/4?fw=pt">How do Transformers work? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/5?fw=pt">Encoder models </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter1/6?fw=pt">Decoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/7?fw=pt">Sequence-to-sequence models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/8?fw=pt">Bias and limitations </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/9?fw=pt">Summary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="decoder-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#decoder-models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Decoder models</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-1-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/d_ixlCubqQw" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Decoder models use only the decoder of a Transformer model. At each stage, for a given word the attention layers can only access the words positioned before it in the sentence. These models are often called <em>auto-regressive models</em>.</p> <p>The pretraining of decoder models usually revolves around predicting the next word in the sentence.</p> <p>These models are best suited for tasks involving text generation.</p> <p>Representatives of this family of models include:</p> <ul><li><a href="https://huggingface.co/transformers/model_doc/ctrl.html" rel="nofollow">CTRL</a></li> <li><a href="https://huggingface.co/docs/transformers/model_doc/openai-gpt" rel="nofollow">GPT</a></li> <li><a href="https://huggingface.co/transformers/model_doc/gpt2.html" rel="nofollow">GPT-2</a></li> <li><a href="https://huggingface.co/transformers/model_doc/transfo-xl.html" rel="nofollow">Transformer XL</a></li></ul> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter1/5?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Encoder models</a> <a href="/learn/nlp-course/chapter1/7?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Sequence-to-sequence models<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;decoder-models&quot;,&quot;url&quot;:&quot;#decoder-models&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#decoder-models" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-decoder-models"><wbr>Decoder models</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter1/6" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter1/6"); } </script> <iframe name="__privateStripeMetricsController3780" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter1%2F6%3Ffw%3Dpt&amp;title=Decoder%20models%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:05.751Z
Sequence-to-sequence models[sequence-to-sequence-models] - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter1/7?fw=pt
## [](#sequencetosequence-modelssequencetosequencemodels)Sequence-to-sequence models\[sequence-to-sequence-models\] [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-1-questions) Encoder-decoder models (also called _sequence-to-sequence models_) use both parts of the Transformer architecture. At each stage, the attention layers of the encoder can access all the words in the initial sentence, whereas the attention layers of the decoder can only access the words positioned before a given word in the input. The pretraining of these models can be done using the objectives of encoder or decoder models, but usually involves something a bit more complex. For instance, [T5](https://huggingface.co/t5-base) is pretrained by replacing random spans of text (that can contain several words) with a single mask special word, and the objective is then to predict the text that this mask word replaces. Sequence-to-sequence models are best suited for tasks revolving around generating new sentences depending on a given input, such as summarization, translation, or generative question answering. Representatives of this family of models include: - [BART](https://huggingface.co/transformers/model_doc/bart.html) - [mBART](https://huggingface.co/transformers/model_doc/mbart.html) - [Marian](https://huggingface.co/transformers/model_doc/marian.html) - [T5](https://huggingface.co/transformers/model_doc/t5.html)
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Sequence-to-sequence models[sequence-to-sequence-models] - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter1/7"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Sequence-to-sequence models[sequence-to-sequence-models] - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;sequencetosequence-modelssequencetosequencemodels&quot;,&quot;title&quot;:&quot;Sequence-to-sequence models[sequence-to-sequence-models]&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter1/7&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Sequence-to-sequence models[sequence-to-sequence-models]&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Sequence-to-sequence models[sequence-to-sequence-models]</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/2?fw=pt">Natural Language Processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/3?fw=pt">Transformers, what can they do? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/4?fw=pt">How do Transformers work? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/5?fw=pt">Encoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/6?fw=pt">Decoder models </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter1/7?fw=pt">Sequence-to-sequence models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/8?fw=pt">Bias and limitations </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/9?fw=pt">Summary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="sequencetosequence-modelssequencetosequencemodels" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#sequencetosequence-modelssequencetosequencemodels"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Sequence-to-sequence models[sequence-to-sequence-models]</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-1-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/0_4KEb08xrE" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Encoder-decoder models (also called <em>sequence-to-sequence models</em>) use both parts of the Transformer architecture. At each stage, the attention layers of the encoder can access all the words in the initial sentence, whereas the attention layers of the decoder can only access the words positioned before a given word in the input.</p> <p>The pretraining of these models can be done using the objectives of encoder or decoder models, but usually involves something a bit more complex. For instance, <a href="https://huggingface.co/t5-base" rel="nofollow">T5</a> is pretrained by replacing random spans of text (that can contain several words) with a single mask special word, and the objective is then to predict the text that this mask word replaces.</p> <p>Sequence-to-sequence models are best suited for tasks revolving around generating new sentences depending on a given input, such as summarization, translation, or generative question answering.</p> <p>Representatives of this family of models include:</p> <ul><li><a href="https://huggingface.co/transformers/model_doc/bart.html" rel="nofollow">BART</a></li> <li><a href="https://huggingface.co/transformers/model_doc/mbart.html" rel="nofollow">mBART</a></li> <li><a href="https://huggingface.co/transformers/model_doc/marian.html" rel="nofollow">Marian</a></li> <li><a href="https://huggingface.co/transformers/model_doc/t5.html" rel="nofollow">T5</a></li></ul> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter1/6?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Decoder models</a> <a href="/learn/nlp-course/chapter1/8?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Bias and limitations<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Sequence-to-sequence models[sequence-to-sequence-models]&quot;,&quot;id&quot;:&quot;sequencetosequence-modelssequencetosequencemodels&quot;,&quot;url&quot;:&quot;#sequencetosequence-modelssequencetosequencemodels&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#sequencetosequence-modelssequencetosequencemodels" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-sequencetosequence-modelssequencetosequencemodels"><wbr>Sequence-to-sequence models[sequence-to-sequence-models]</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter1/7" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter1/7"); } </script> <iframe name="__privateStripeMetricsController5090" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter1%2F7%3Ffw%3Dpt&amp;title=Sequence-to-sequence%20models%5Bsequence-to-sequence-models%5D%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:05.887Z
Bias and limitations - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter1/8?fw=pt
3\. Fine-tuning a pretrained model 4\. Sharing models and tokenizers 5\. The 🤗 Datasets library 6\. The 🤗 Tokenizers library 9\. Building and sharing demos new ## [](#bias-and-limitations)Bias and limitations [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-1-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter1/section8.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter1/section8.ipynb) If your intent is to use a pretrained model or a fine-tuned version in production, please be aware that, while these models are powerful tools, they come with limitations. The biggest of these is that, to enable pretraining on large amounts of data, researchers often scrape all the content they can find, taking the best as well as the worst of what is available on the internet. To give a quick illustration, let’s go back the example of a `fill-mask` pipeline with the BERT model: ``` from transformers import pipeline unmasker = pipeline("fill-mask", model="bert-base-uncased") result = unmasker("This man works as a [MASK].") print([r["token_str"] for r in result]) result = unmasker("This woman works as a [MASK].") print([r["token_str"] for r in result])``` ``` ['lawyer', 'carpenter', 'doctor', 'waiter', 'mechanic'] ['nurse', 'waitress', 'teacher', 'maid', 'prostitute']``` When asked to fill in the missing word in these two sentences, the model gives only one gender-free answer (waiter/waitress). The others are work occupations usually associated with one specific gender — and yes, prostitute ended up in the top 5 possibilities the model associates with “woman” and “work.” This happens even though BERT is one of the rare Transformer models not built by scraping data from all over the internet, but rather using apparently neutral data (it’s trained on the [English Wikipedia](https://huggingface.co/datasets/wikipedia) and [BookCorpus](https://huggingface.co/datasets/bookcorpus) datasets). When you use these tools, you therefore need to keep in the back of your mind that the original model you are using could very easily generate sexist, racist, or homophobic content. Fine-tuning the model on your data won’t make this intrinsic bias disappear.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Bias and limitations - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter1/8"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Bias and limitations - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;bias-and-limitations&quot;,&quot;title&quot;:&quot;Bias and limitations&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter1/8&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Bias and limitations&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Bias and limitations</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/2?fw=pt">Natural Language Processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/3?fw=pt">Transformers, what can they do? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/4?fw=pt">How do Transformers work? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/5?fw=pt">Encoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/6?fw=pt">Decoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/7?fw=pt">Sequence-to-sequence models </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter1/8?fw=pt">Bias and limitations </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/9?fw=pt">Summary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="bias-and-limitations" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#bias-and-limitations"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Bias and limitations</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-1-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter1/section8.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter1/section8.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>If your intent is to use a pretrained model or a fine-tuned version in production, please be aware that, while these models are powerful tools, they come with limitations. The biggest of these is that, to enable pretraining on large amounts of data, researchers often scrape all the content they can find, taking the best as well as the worst of what is available on the internet.</p> <p>To give a quick illustration, let’s go back the example of a <code>fill-mask</code> pipeline with the BERT model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline unmasker = pipeline(<span class="hljs-string">"fill-mask"</span>, model=<span class="hljs-string">"bert-base-uncased"</span>) result = unmasker(<span class="hljs-string">"This man works as a [MASK]."</span>) <span class="hljs-built_in">print</span>([r[<span class="hljs-string">"token_str"</span>] <span class="hljs-keyword">for</span> r <span class="hljs-keyword">in</span> result]) result = unmasker(<span class="hljs-string">"This woman works as a [MASK]."</span>) <span class="hljs-built_in">print</span>([r[<span class="hljs-string">"token_str"</span>] <span class="hljs-keyword">for</span> r <span class="hljs-keyword">in</span> result])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'lawyer'</span>, <span class="hljs-string">'carpenter'</span>, <span class="hljs-string">'doctor'</span>, <span class="hljs-string">'waiter'</span>, <span class="hljs-string">'mechanic'</span>] [<span class="hljs-string">'nurse'</span>, <span class="hljs-string">'waitress'</span>, <span class="hljs-string">'teacher'</span>, <span class="hljs-string">'maid'</span>, <span class="hljs-string">'prostitute'</span>]</pre></div> <p>When asked to fill in the missing word in these two sentences, the model gives only one gender-free answer (waiter/waitress). The others are work occupations usually associated with one specific gender — and yes, prostitute ended up in the top 5 possibilities the model associates with “woman” and “work.” This happens even though BERT is one of the rare Transformer models not built by scraping data from all over the internet, but rather using apparently neutral data (it’s trained on the <a href="https://huggingface.co/datasets/wikipedia" rel="nofollow">English Wikipedia</a> and <a href="https://huggingface.co/datasets/bookcorpus" rel="nofollow">BookCorpus</a> datasets).</p> <p>When you use these tools, you therefore need to keep in the back of your mind that the original model you are using could very easily generate sexist, racist, or homophobic content. Fine-tuning the model on your data won’t make this intrinsic bias disappear.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter1/7?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Sequence-to-sequence models</a> <a href="/learn/nlp-course/chapter1/9?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Summary<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;bias-and-limitations&quot;,&quot;url&quot;:&quot;#bias-and-limitations&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#bias-and-limitations" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-bias-and-limitations"><wbr>Bias and limitations</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter1/8" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter1/8"); } </script> <iframe name="__privateStripeMetricsController3410" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter1%2F8%3Ffw%3Dpt&amp;title=Bias%20and%20limitations%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:06.452Z
Summary - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter1/9?fw=pt
## [](#summary)Summary [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-1-questions) In this chapter, you saw how to approach different NLP tasks using the high-level `pipeline()` function from 🤗 Transformers. You also saw how to search for and use models in the Hub, as well as how to use the Inference API to test the models directly in your browser. We discussed how Transformer models work at a high level, and talked about the importance of transfer learning and fine-tuning. A key aspect is that you can use the full architecture or only the encoder or decoder, depending on what kind of task you aim to solve. The following table summarizes this: | Model | Examples | Tasks | | --- | --- | --- | | Encoder | ALBERT, BERT, DistilBERT, ELECTRA, RoBERTa | Sentence classification, named entity recognition, extractive question answering | | Decoder | CTRL, GPT, GPT-2, Transformer XL | Text generation | | Encoder-decoder | BART, T5, Marian, mBART | Summarization, translation, generative question answering |
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Summary - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter1/9"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Summary - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;summary&quot;,&quot;title&quot;:&quot;Summary&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter1/9&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Summary&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Summary</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/2?fw=pt">Natural Language Processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/3?fw=pt">Transformers, what can they do? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/4?fw=pt">How do Transformers work? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/5?fw=pt">Encoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/6?fw=pt">Decoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/7?fw=pt">Sequence-to-sequence models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/8?fw=pt">Bias and limitations </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter1/9?fw=pt">Summary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="summary" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#summary"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Summary</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-1-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>In this chapter, you saw how to approach different NLP tasks using the high-level <code>pipeline()</code> function from 🤗 Transformers. You also saw how to search for and use models in the Hub, as well as how to use the Inference API to test the models directly in your browser.</p> <p>We discussed how Transformer models work at a high level, and talked about the importance of transfer learning and fine-tuning. A key aspect is that you can use the full architecture or only the encoder or decoder, depending on what kind of task you aim to solve. The following table summarizes this:</p> <table><thead><tr><th>Model</th> <th>Examples</th> <th>Tasks</th></tr></thead> <tbody><tr><td>Encoder</td> <td>ALBERT, BERT, DistilBERT, ELECTRA, RoBERTa</td> <td>Sentence classification, named entity recognition, extractive question answering</td></tr> <tr><td>Decoder</td> <td>CTRL, GPT, GPT-2, Transformer XL</td> <td>Text generation</td></tr> <tr><td>Encoder-decoder</td> <td>BART, T5, Marian, mBART</td> <td>Summarization, translation, generative question answering</td></tr></tbody></table> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter1/8?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Bias and limitations</a> <a href="/learn/nlp-course/chapter1/10?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">End-of-chapter quiz<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;summary&quot;,&quot;url&quot;:&quot;#summary&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#summary" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-summary"><wbr>Summary</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter1/9" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter1/9"); } </script> <iframe name="__privateStripeMetricsController1010" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter1%2F9%3Ffw%3Dpt&amp;title=Summary%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:06.604Z
End-of-chapter quiz - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter1/10?fw=pt
3\. Fine-tuning a pretrained model 4\. Sharing models and tokenizers 5\. The 🤗 Datasets library 6\. The 🤗 Tokenizers library 9\. Building and sharing demos new ## [](#end-of-chapter-quiz)End-of-chapter quiz [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-1-questions) This chapter covered a lot of ground! Don’t worry if you didn’t grasp all the details; the next chapters will help you understand how things work under the hood. First, though, let’s test what you learned in this chapter! ### [](#1.-explore-the-hub-and-look-for-the-<code>roberta-large-mnli</code>-checkpoint.-what-task-does-it-perform?)1\. Explore the Hub and look for the `roberta-large-mnli` checkpoint. What task does it perform? ### [](#2.-what-will-the-following-code-return?)2\. What will the following code return? ``` from transformers import pipeline ner = pipeline("ner", grouped_entities=True) ner("My name is Sylvain and I work at Hugging Face in Brooklyn.")``` ### [](#3.-what-should-replace-…-in-this-code-sample?)3\. What should replace … in this code sample? ``` from transformers import pipeline filler = pipeline("fill-mask", model="bert-base-cased") result = filler("...")``` ### [](#4.-why-will-this-code-fail?)4\. Why will this code fail? ``` from transformers import pipeline classifier = pipeline("zero-shot-classification") result = classifier("This is a course about the Transformers library")``` ### [](#5.-what-does-“transfer-learning”-mean?)5\. What does “transfer learning” mean? ### [](#6.-true-or-false?-a-language-model-usually-does-not-need-labels-for-its-pretraining.)6\. True or false? A language model usually does not need labels for its pretraining. ### [](#7.-select-the-sentence-that-best-describes-the-terms-“model”,-“architecture”,-and-“weights”.)7\. Select the sentence that best describes the terms “model”, “architecture”, and “weights”. ### [](#8.-which-of-these-types-of-models-would-you-use-for-completing-prompts-with-generated-text?)8\. Which of these types of models would you use for completing prompts with generated text? ### [](#9.-which-of-those-types-of-models-would-you-use-for-summarizing-texts?)9\. Which of those types of models would you use for summarizing texts? ### [](#10.-which-of-these-types-of-models-would-you-use-for-classifying-text-inputs-according-to-certain-labels?)10\. Which of these types of models would you use for classifying text inputs according to certain labels? ### [](#11.-what-possible-source-can-the-bias-observed-in-a-model-have?)11\. What possible source can the bias observed in a model have?
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="End-of-chapter quiz - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter1/10"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>End-of-chapter quiz - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;end-of-chapter-quiz&quot;,&quot;title&quot;:&quot;End-of-chapter quiz&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter1/10&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;End-of-chapter quiz&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">End-of-chapter quiz</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/2?fw=pt">Natural Language Processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/3?fw=pt">Transformers, what can they do? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/4?fw=pt">How do Transformers work? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/5?fw=pt">Encoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/6?fw=pt">Decoder models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/7?fw=pt">Sequence-to-sequence models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/8?fw=pt">Bias and limitations </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter1/9?fw=pt">Summary </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter1/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="end-of-chapter-quiz" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#end-of-chapter-quiz"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>End-of-chapter quiz</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-1-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>This chapter covered a lot of ground! Don’t worry if you didn’t grasp all the details; the next chapters will help you understand how things work under the hood.</p> <p>First, though, let’s test what you learned in this chapter!</p> <h3 class="relative group"><a id="1.-explore-the-hub-and-look-for-the-<code>roberta-large-mnli</code>-checkpoint.-what-task-does-it-perform?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#1.-explore-the-hub-and-look-for-the-<code>roberta-large-mnli</code>-checkpoint.-what-task-does-it-perform?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>1. Explore the Hub and look for the <code>roberta-large-mnli</code> checkpoint. What task does it perform?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Summarization</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Text classification</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Text generation</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="2.-what-will-the-following-code-return?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#2.-what-will-the-following-code-return?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>2. What will the following code return?</span></h3> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline ner = pipeline(<span class="hljs-string">"ner"</span>, grouped_entities=<span class="hljs-literal">True</span>) ner(<span class="hljs-string">"My name is Sylvain and I work at Hugging Face in Brooklyn."</span>)</pre></div> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It will return classification scores for this sentence, with labels "positive" or "negative".</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It will return a generated text completing this sentence.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It will return the words representing persons, organizations or locations.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="3.-what-should-replace-…-in-this-code-sample?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#3.-what-should-replace-…-in-this-code-sample?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>3. What should replace … in this code sample?</span></h3> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline filler = pipeline(<span class="hljs-string">"fill-mask"</span>, model=<span class="hljs-string">"bert-base-cased"</span>) result = filler(<span class="hljs-string">"..."</span>)</pre></div> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> This &lt;mask&gt; has been waiting for you.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> This [MASK] has been waiting for you.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> This man has been waiting for you.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="4.-why-will-this-code-fail?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#4.-why-will-this-code-fail?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>4. Why will this code fail?</span></h3> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline classifier = pipeline(<span class="hljs-string">"zero-shot-classification"</span>) result = classifier(<span class="hljs-string">"This is a course about the Transformers library"</span>)</pre></div> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> This pipeline requires that labels be given to classify this text.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> This pipeline requires several sentences, not just one.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> The 🤗 Transformers library is broken, as usual.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> This pipeline requires longer inputs; this one is too short.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="5.-what-does-“transfer-learning”-mean?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#5.-what-does-“transfer-learning”-mean?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>5. What does “transfer learning” mean?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Transferring the knowledge of a pretrained model to a new model by training it on the same dataset.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Transferring the knowledge of a pretrained model to a new model by initializing the second model with the first model's weights.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Transferring the knowledge of a pretrained model to a new model by building the second model with the same architecture as the first model.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="6.-true-or-false?-a-language-model-usually-does-not-need-labels-for-its-pretraining." class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#6.-true-or-false?-a-language-model-usually-does-not-need-labels-for-its-pretraining."><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>6. True or false? A language model usually does not need labels for its pretraining.</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> True</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> False</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="7.-select-the-sentence-that-best-describes-the-terms-“model”,-“architecture”,-and-“weights”." class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#7.-select-the-sentence-that-best-describes-the-terms-“model”,-“architecture”,-and-“weights”."><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>7. Select the sentence that best describes the terms “model”, “architecture”, and “weights”.</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> If a model is a building, its architecture is the blueprint and the weights are the people living inside.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> An architecture is a map to build a model and its weights are the cities represented on the map.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> An architecture is a succession of mathematical functions to build a model and its weights are those functions parameters.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="8.-which-of-these-types-of-models-would-you-use-for-completing-prompts-with-generated-text?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#8.-which-of-these-types-of-models-would-you-use-for-completing-prompts-with-generated-text?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>8. Which of these types of models would you use for completing prompts with generated text?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> An encoder model</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> A decoder model</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A sequence-to-sequence model</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="9.-which-of-those-types-of-models-would-you-use-for-summarizing-texts?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#9.-which-of-those-types-of-models-would-you-use-for-summarizing-texts?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>9. Which of those types of models would you use for summarizing texts?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> An encoder model</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> A decoder model</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A sequence-to-sequence model</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="10.-which-of-these-types-of-models-would-you-use-for-classifying-text-inputs-according-to-certain-labels?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#10.-which-of-these-types-of-models-would-you-use-for-classifying-text-inputs-according-to-certain-labels?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>10. Which of these types of models would you use for classifying text inputs according to certain labels?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> An encoder model</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> A decoder model</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A sequence-to-sequence model</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="11.-what-possible-source-can-the-bias-observed-in-a-model-have?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#11.-what-possible-source-can-the-bias-observed-in-a-model-have?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>11. What possible source can the bias observed in a model have?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> The model is a fine-tuned version of a pretrained model and it picked up its bias from it.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> The data the model was trained on is biased.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> The metric the model was optimizing for is biased.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter1/9?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Summary</a> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isLoggedIn&quot;:false,&quot;nextChapterUrl&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;,&quot;quizChapter&quot;:1}" data-target="CourseCompleteButton"><div class="relative ml-auto"> <button class="btn btn-lg" disabled=""><svg class="mr-2" width="1em" height="1em" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0 16C0 7.16344 7.16344 0 16 0V0C24.8366 0 32 7.16344 32 16V16C32 24.8366 24.8366 32 16 32V32C7.16344 32 0 24.8366 0 16V16Z" fill="#D9FFD2"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M23.5607 10.9393C24.1464 11.5251 24.1464 12.4749 23.5607 13.0607L14.5 22.1213L8.93934 16.5607C8.35355 15.9749 8.35355 15.0251 8.93934 14.4393C9.52513 13.8536 10.4749 13.8536 11.0607 14.4393L14.5 17.8787L21.4393 10.9393C22.0251 10.3536 22.9749 10.3536 23.5607 10.9393Z" fill="#188B3F"></path></svg> Complete Chapter</button></div></div> <a href="/learn/nlp-course/chapter2/1?fw=pt" class="hidden">Next chapter</a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;id&quot;:&quot;end-of-chapter-quiz&quot;,&quot;url&quot;:&quot;#end-of-chapter-quiz&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#end-of-chapter-quiz" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-end-of-chapter-quiz"><wbr>End-of-chapter quiz</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter1/10" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter1/10"); } </script> <iframe name="__privateStripeMetricsController5890" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter1%2F10%3Ffw%3Dpt&amp;title=End-of-chapter%20quiz%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:06.996Z
Introduction - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter0/1?fw=pt
## [](#introduction)Introduction Welcome to the Hugging Face course! This introduction will guide you through setting up a working environment. If you’re just starting the course, we recommend you first take a look at [Chapter 1](/course/chapter1), then come back and set up your environment so you can try the code yourself. All the libraries that we’ll be using in this course are available as Python packages, so here we’ll show you how to set up a Python environment and install the specific libraries you’ll need. We’ll cover two ways of setting up your working environment, using a Colab notebook or a Python virtual environment. Feel free to choose the one that resonates with you the most. For beginners, we strongly recommend that you get started by using a Colab notebook. Note that we will not be covering the Windows system. If you’re running on Windows, we recommend following along using a Colab notebook. If you’re using a Linux distribution or macOS, you can use either approach described here. Most of the course relies on you having a Hugging Face account. We recommend creating one now: [create an account](https://huggingface.co/join). ## [](#using-a-google-colab-notebook)Using a Google Colab notebook Using a Colab notebook is the simplest possible setup; boot up a notebook in your browser and get straight to coding! If you’re not familiar with Colab, we recommend you start by following the [introduction](https://colab.research.google.com/notebooks/intro.ipynb). Colab allows you to use some accelerating hardware, like GPUs or TPUs, and it is free for smaller workloads. Once you’re comfortable moving around in Colab, create a new notebook and get started with the setup: ![An empty colab notebook](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter0/new_colab.png) The next step is to install the libraries that we’ll be using in this course. We’ll use `pip` for the installation, which is the package manager for Python. In notebooks, you can run system commands by preceding them with the `!` character, so you can install the 🤗 Transformers library as follows: ``` !pip install transformers``` You can make sure the package was correctly installed by importing it within your Python runtime: ![A gif showing the result of the two commands above: installation and import](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter0/install.gif) This installs a very light version of 🤗 Transformers. In particular, no specific machine learning frameworks (like PyTorch or TensorFlow) are installed. Since we’ll be using a lot of different features of the library, we recommend installing the development version, which comes with all the required dependencies for pretty much any imaginable use case: ``` !pip install transformers[sentencepiece]``` This will take a bit of time, but then you’ll be ready to go for the rest of the course! ## [](#using-a-python-virtual-environment)Using a Python virtual environment If you prefer to use a Python virtual environment, the first step is to install Python on your system. We recommend following [this guide](https://realpython.com/installing-python/) to get started. Once you have Python installed, you should be able to run Python commands in your terminal. You can start by running the following command to ensure that it is correctly installed before proceeding to the next steps: `python --version`. This should print out the Python version now available on your system. When running a Python command in your terminal, such as `python --version`, you should think of the program running your command as the “main” Python on your system. We recommend keeping this main installation free of any packages, and using it to create separate environments for each application you work on — this way, each application can have its own dependencies and packages, and you won’t need to worry about potential compatibility issues with other applications. In Python this is done with [_virtual environments_](https://docs.python.org/3/tutorial/venv.html), which are self-contained directory trees that each contain a Python installation with a particular Python version alongside all the packages the application needs. Creating such a virtual environment can be done with a number of different tools, but we’ll use the official Python package for that purpose, which is called [`venv`](https://docs.python.org/3/library/venv.html#module-venv). First, create the directory you’d like your application to live in — for example, you might want to make a new directory called _transformers-course_ at the root of your home directory: ``` mkdir ~/transformers-course cd ~/transformers-course``` From inside this directory, create a virtual environment using the Python `venv` module: You should now have a directory called _.env_ in your otherwise empty folder: You can jump in and out of your virtual environment with the `activate` and `deactivate` scripts: ``` source .env/bin/activate source .env/bin/deactivate``` You can make sure that the environment is activated by running the `which python` command: if it points to the virtual environment, then you have successfully activated it! ``` /home/<user>/transformers-course/.env/bin/python``` ### [](#installing-dependencies)Installing dependencies As in the previous section on using Google Colab instances, you’ll now need to install the packages required to continue. Again, you can install the development version of 🤗 Transformers using the `pip` package manager: ``` pip install "transformers[sentencepiece]"``` You’re now all set up and ready to go!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Introduction - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter0/1"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Introduction - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;introduction&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;using-a-google-colab-notebook&quot;,&quot;title&quot;:&quot;Using a Google Colab notebook&quot;},{&quot;local&quot;:&quot;using-a-python-virtual-environment&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;installing-dependencies&quot;,&quot;title&quot;:&quot;Installing dependencies&quot;}],&quot;title&quot;:&quot;Using a Python virtual environment&quot;}],&quot;title&quot;:&quot;Introduction&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter0/1&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Introduction&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Introduction</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter0/1?fw=pt">Introduction </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="introduction" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#introduction"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Introduction</span></h1> <p>Welcome to the Hugging Face course! This introduction will guide you through setting up a working environment. If you’re just starting the course, we recommend you first take a look at <a href="/course/chapter1">Chapter 1</a>, then come back and set up your environment so you can try the code yourself.</p> <p>All the libraries that we’ll be using in this course are available as Python packages, so here we’ll show you how to set up a Python environment and install the specific libraries you’ll need.</p> <p>We’ll cover two ways of setting up your working environment, using a Colab notebook or a Python virtual environment. Feel free to choose the one that resonates with you the most. For beginners, we strongly recommend that you get started by using a Colab notebook.</p> <p>Note that we will not be covering the Windows system. If you’re running on Windows, we recommend following along using a Colab notebook. If you’re using a Linux distribution or macOS, you can use either approach described here.</p> <p>Most of the course relies on you having a Hugging Face account. We recommend creating one now: <a href="https://huggingface.co/join" rel="nofollow">create an account</a>.</p> <h2 class="relative group"><a id="using-a-google-colab-notebook" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-a-google-colab-notebook"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using a Google Colab notebook</span></h2> <p>Using a Colab notebook is the simplest possible setup; boot up a notebook in your browser and get straight to coding!</p> <p>If you’re not familiar with Colab, we recommend you start by following the <a href="https://colab.research.google.com/notebooks/intro.ipynb" rel="nofollow">introduction</a>. Colab allows you to use some accelerating hardware, like GPUs or TPUs, and it is free for smaller workloads.</p> <p>Once you’re comfortable moving around in Colab, create a new notebook and get started with the setup:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter0/new_colab.png" alt="An empty colab notebook" width="80%"></div> <p>The next step is to install the libraries that we’ll be using in this course. We’ll use <code>pip</code> for the installation, which is the package manager for Python. In notebooks, you can run system commands by preceding them with the <code>!</code> character, so you can install the 🤗 Transformers library as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!pip <span class="hljs-keyword">install</span> transformers</pre></div> <p>You can make sure the package was correctly installed by importing it within your Python runtime:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> transformers</pre></div> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter0/install.gif" alt="A gif showing the result of the two commands above: installation and import" width="80%"></div> <p>This installs a very light version of 🤗 Transformers. In particular, no specific machine learning frameworks (like PyTorch or TensorFlow) are installed. Since we’ll be using a lot of different features of the library, we recommend installing the development version, which comes with all the required dependencies for pretty much any imaginable use case:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!pip <span class="hljs-keyword">install</span> transformers[sentencepiece]</pre></div> <p>This will take a bit of time, but then you’ll be ready to go for the rest of the course!</p> <h2 class="relative group"><a id="using-a-python-virtual-environment" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-a-python-virtual-environment"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using a Python virtual environment</span></h2> <p>If you prefer to use a Python virtual environment, the first step is to install Python on your system. We recommend following <a href="https://realpython.com/installing-python/" rel="nofollow">this guide</a> to get started.</p> <p>Once you have Python installed, you should be able to run Python commands in your terminal. You can start by running the following command to ensure that it is correctly installed before proceeding to the next steps: <code>python --version</code>. This should print out the Python version now available on your system.</p> <p>When running a Python command in your terminal, such as <code>python --version</code>, you should think of the program running your command as the “main” Python on your system. We recommend keeping this main installation free of any packages, and using it to create separate environments for each application you work on — this way, each application can have its own dependencies and packages, and you won’t need to worry about potential compatibility issues with other applications.</p> <p>In Python this is done with <a href="https://docs.python.org/3/tutorial/venv.html" rel="nofollow"><em>virtual environments</em></a>, which are self-contained directory trees that each contain a Python installation with a particular Python version alongside all the packages the application needs. Creating such a virtual environment can be done with a number of different tools, but we’ll use the official Python package for that purpose, which is called <a href="https://docs.python.org/3/library/venv.html#module-venv" rel="nofollow"><code>venv</code></a>.</p> <p>First, create the directory you’d like your application to live in — for example, you might want to make a new directory called <em>transformers-course</em> at the root of your home directory:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">mkdir</span> ~/transformers-course <span class="hljs-built_in">cd</span> ~/transformers-course</pre></div> <p>From inside this directory, create a virtual environment using the Python <code>venv</code> module:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">python</span> -m venv .<span class="hljs-keyword">env</span></pre></div> <p>You should now have a directory called <em>.env</em> in your otherwise empty folder:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">ls</span> -a</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>. <span class="hljs-string">..</span> <span class="hljs-string">.env</span></pre></div> <p>You can jump in and out of your virtual environment with the <code>activate</code> and <code>deactivate</code> scripts:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># Activate the virtual environment</span> <span class="hljs-built_in">source</span> .<span class="hljs-built_in">env</span>/bin/activate <span class="hljs-comment"># Deactivate the virtual environment</span> <span class="hljs-built_in">source</span> .<span class="hljs-built_in">env</span>/bin/deactivate</pre></div> <p>You can make sure that the environment is activated by running the <code>which python</code> command: if it points to the virtual environment, then you have successfully activated it!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">which</span> python</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-regexp">/home/</span>&lt;user&gt;<span class="hljs-regexp">/transformers-course/</span>.env<span class="hljs-regexp">/bin/</span>python</pre></div> <h3 class="relative group"><a id="installing-dependencies" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#installing-dependencies"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Installing dependencies</span></h3> <p>As in the previous section on using Google Colab instances, you’ll now need to install the packages required to continue. Again, you can install the development version of 🤗 Transformers using the <code>pip</code> package manager:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip <span class="hljs-keyword">install</span> <span class="hljs-string">"transformers[sentencepiece]"</span></pre></div> <p>You’re now all set up and ready to go!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"> <a href="/learn/nlp-course/chapter1/1?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Introduction<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Introduction&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;introduction&quot;,&quot;url&quot;:&quot;#introduction&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Using a Google Colab notebook&quot;,&quot;id&quot;:&quot;using-a-google-colab-notebook&quot;,&quot;url&quot;:&quot;#using-a-google-colab-notebook&quot;},{&quot;title&quot;:&quot;Using a Python virtual environment&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;using-a-python-virtual-environment&quot;,&quot;url&quot;:&quot;#using-a-python-virtual-environment&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Installing dependencies&quot;,&quot;id&quot;:&quot;installing-dependencies&quot;,&quot;url&quot;:&quot;#installing-dependencies&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#introduction" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-introduction"><wbr>Introduction</a> <a href="#using-a-google-colab-notebook" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-a-google-colab-notebook"><wbr>Using a <wbr>Google <wbr>Colab notebook</a> <a href="#using-a-python-virtual-environment" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-a-python-virtual-environment"><wbr>Using a <wbr>Python virtual environment</a> <a href="#installing-dependencies" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-installing-dependencies"><wbr>Installing dependencies</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter0/1" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter0/1"); } </script> <iframe name="__privateStripeMetricsController2160" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter0%2F1%3Ffw%3Dpt&amp;title=Introduction%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:07.451Z
Introduction - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter2/1?fw=pt
## [](#introduction)Introduction [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-2-questions) As you saw in [Chapter 1](/course/chapter1), Transformer models are usually very large. With millions to tens of _billions_ of parameters, training and deploying these models is a complicated undertaking. Furthermore, with new models being released on a near-daily basis and each having its own implementation, trying them all out is no easy task. The 🤗 Transformers library was created to solve this problem. Its goal is to provide a single API through which any Transformer model can be loaded, trained, and saved. The library’s main features are: - **Ease of use**: Downloading, loading, and using a state-of-the-art NLP model for inference can be done in just two lines of code. - **Flexibility**: At their core, all models are simple PyTorch `nn.Module` or TensorFlow `tf.keras.Model` classes and can be handled like any other models in their respective machine learning (ML) frameworks. - **Simplicity**: Hardly any abstractions are made across the library. The “All in one file” is a core concept: a model’s forward pass is entirely defined in a single file, so that the code itself is understandable and hackable. This last feature makes 🤗 Transformers quite different from other ML libraries. The models are not built on modules that are shared across files; instead, each model has its own layers. In addition to making the models more approachable and understandable, this allows you to easily experiment on one model without affecting others. This chapter will begin with an end-to-end example where we use a model and a tokenizer together to replicate the `pipeline()` function introduced in [Chapter 1](/course/chapter1). Next, we’ll discuss the model API: we’ll dive into the model and configuration classes, and show you how to load a model and how it processes numerical inputs to output predictions. Then we’ll look at the tokenizer API, which is the other main component of the `pipeline()` function. Tokenizers take care of the first and last processing steps, handling the conversion from text to numerical inputs for the neural network, and the conversion back to text when it is needed. Finally, we’ll show you how to handle sending multiple sentences through a model in a prepared batch, then wrap it all up with a closer look at the high-level `tokenizer()` function. ⚠️ In order to benefit from all features available with the Model Hub and 🤗 Transformers, we recommend [creating an account](https://huggingface.co/join).
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Introduction - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter2/1"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Introduction - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;introduction&quot;,&quot;title&quot;:&quot;Introduction&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter2/1&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Introduction&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Introduction</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter2/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/2?fw=pt">Behind the pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/3?fw=pt">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/4?fw=pt">Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/5?fw=pt">Handling multiple sequences </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/6?fw=pt">Putting it all together </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/7?fw=pt">Basic usage completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="introduction" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#introduction"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Introduction</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-2-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>As you saw in <a href="/course/chapter1">Chapter 1</a>, Transformer models are usually very large. With millions to tens of <em>billions</em> of parameters, training and deploying these models is a complicated undertaking. Furthermore, with new models being released on a near-daily basis and each having its own implementation, trying them all out is no easy task.</p> <p>The 🤗 Transformers library was created to solve this problem. Its goal is to provide a single API through which any Transformer model can be loaded, trained, and saved. The library’s main features are:</p> <ul><li><strong>Ease of use</strong>: Downloading, loading, and using a state-of-the-art NLP model for inference can be done in just two lines of code.</li> <li><strong>Flexibility</strong>: At their core, all models are simple PyTorch <code>nn.Module</code> or TensorFlow <code>tf.keras.Model</code> classes and can be handled like any other models in their respective machine learning (ML) frameworks.</li> <li><strong>Simplicity</strong>: Hardly any abstractions are made across the library. The “All in one file” is a core concept: a model’s forward pass is entirely defined in a single file, so that the code itself is understandable and hackable.</li></ul> <p>This last feature makes 🤗 Transformers quite different from other ML libraries. The models are not built on modules that are shared across files; instead, each model has its own layers. In addition to making the models more approachable and understandable, this allows you to easily experiment on one model without affecting others.</p> <p>This chapter will begin with an end-to-end example where we use a model and a tokenizer together to replicate the <code>pipeline()</code> function introduced in <a href="/course/chapter1">Chapter 1</a>. Next, we’ll discuss the model API: we’ll dive into the model and configuration classes, and show you how to load a model and how it processes numerical inputs to output predictions.</p> <p>Then we’ll look at the tokenizer API, which is the other main component of the <code>pipeline()</code> function. Tokenizers take care of the first and last processing steps, handling the conversion from text to numerical inputs for the neural network, and the conversion back to text when it is needed. Finally, we’ll show you how to handle sending multiple sentences through a model in a prepared batch, then wrap it all up with a closer look at the high-level <code>tokenizer()</code> function.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400">⚠️ In order to benefit from all features available with the Model Hub and 🤗 Transformers, we recommend <a href="https://huggingface.co/join">creating an account</a>.</div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter1/10?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>End-of-chapter quiz</a> <a href="/learn/nlp-course/chapter2/2?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Behind the pipeline<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;introduction&quot;,&quot;url&quot;:&quot;#introduction&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#introduction" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-introduction"><wbr>Introduction</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter2/1" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter2/1"); } </script> <iframe name="__privateStripeMetricsController3440" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter2%2F1%3Ffw%3Dpt&amp;title=Introduction%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:07.627Z
Models - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter2/3?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#models)Models [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-2-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter2/section3_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter2/section3_pt.ipynb) In this section we’ll take a closer look at creating and using a model. We’ll use the `AutoModel` class, which is handy when you want to instantiate any model from a checkpoint. The `AutoModel` class and all of its relatives are actually simple wrappers over the wide variety of models available in the library. It’s a clever wrapper as it can automatically guess the appropriate model architecture for your checkpoint, and then instantiates a model with this architecture. However, if you know the type of model you want to use, you can use the class that defines its architecture directly. Let’s take a look at how this works with a BERT model. ## [](#creating-a-transformer)Creating a Transformer The first thing we’ll need to do to initialize a BERT model is load a configuration object: ``` from transformers import BertConfig, BertModel config = BertConfig() model = BertModel(config)``` The configuration contains many attributes that are used to build the model: ``` BertConfig { [...] "hidden_size": 768, "intermediate_size": 3072, "max_position_embeddings": 512, "num_attention_heads": 12, "num_hidden_layers": 12, [...] }``` While you haven’t seen what all of these attributes do yet, you should recognize some of them: the `hidden_size` attribute defines the size of the `hidden_states` vector, and `num_hidden_layers` defines the number of layers the Transformer model has. ### [](#different-loading-methods)Different loading methods Creating a model from the default configuration initializes it with random values: ``` from transformers import BertConfig, BertModel config = BertConfig() model = BertModel(config) ``` The model can be used in this state, but it will output gibberish; it needs to be trained first. We could train the model from scratch on the task at hand, but as you saw in [Chapter 1](/course/chapter1), this would require a long time and a lot of data, and it would have a non-negligible environmental impact. To avoid unnecessary and duplicated effort, it’s imperative to be able to share and reuse models that have already been trained. Loading a Transformer model that is already trained is simple — we can do this using the `from_pretrained()` method: ``` from transformers import BertModel model = BertModel.from_pretrained("bert-base-cased")``` As you saw earlier, we could replace `BertModel` with the equivalent `AutoModel` class. We’ll do this from now on as this produces checkpoint-agnostic code; if your code works for one checkpoint, it should work seamlessly with another. This applies even if the architecture is different, as long as the checkpoint was trained for a similar task (for example, a sentiment analysis task). In the code sample above we didn’t use `BertConfig`, and instead loaded a pretrained model via the `bert-base-cased` identifier. This is a model checkpoint that was trained by the authors of BERT themselves; you can find more details about it in its [model card](https://huggingface.co/bert-base-cased). This model is now initialized with all the weights of the checkpoint. It can be used directly for inference on the tasks it was trained on, and it can also be fine-tuned on a new task. By training with pretrained weights rather than from scratch, we can quickly achieve good results. The weights have been downloaded and cached (so future calls to the `from_pretrained()` method won’t re-download them) in the cache folder, which defaults to _~/.cache/huggingface/transformers_. You can customize your cache folder by setting the `HF_HOME` environment variable. The identifier used to load the model can be the identifier of any model on the Model Hub, as long as it is compatible with the BERT architecture. The entire list of available BERT checkpoints can be found [here](https://huggingface.co/models?filter=bert). ### [](#saving-methods)Saving methods Saving a model is as easy as loading one — we use the `save_pretrained()` method, which is analogous to the `from_pretrained()` method: ``` model.save_pretrained("directory_on_my_computer")``` This saves two files to your disk: ``` ls directory_on_my_computer config.json pytorch_model.bin``` If you take a look at the _config.json_ file, you’ll recognize the attributes necessary to build the model architecture. This file also contains some metadata, such as where the checkpoint originated and what 🤗 Transformers version you were using when you last saved the checkpoint. The _pytorch\_model.bin_ file is known as the _state dictionary_; it contains all your model’s weights. The two files go hand in hand; the configuration is necessary to know your model’s architecture, while the model weights are your model’s parameters. ## [](#using-a-transformer-model-for-inference)Using a Transformer model for inference Now that you know how to load and save a model, let’s try using it to make some predictions. Transformer models can only process numbers — numbers that the tokenizer generates. But before we discuss tokenizers, let’s explore what inputs the model accepts. Tokenizers can take care of casting the inputs to the appropriate framework’s tensors, but to help you understand what’s going on, we’ll take a quick look at what must be done before sending the inputs to the model. Let’s say we have a couple of sequences: ``` sequences = ["Hello!", "Cool.", "Nice!"]``` The tokenizer converts these to vocabulary indices which are typically called _input IDs_. Each sequence is now a list of numbers! The resulting output is: ``` encoded_sequences = [ [101, 7592, 999, 102], [101, 4658, 1012, 102], [101, 3835, 999, 102], ]``` This is a list of encoded sequences: a list of lists. Tensors only accept rectangular shapes (think matrices). This “array” is already of rectangular shape, so converting it to a tensor is easy: ``` import torch model_inputs = torch.tensor(encoded_sequences)``` ### [](#using-the-tensors-as-inputs-to-the-model)Using the tensors as inputs to the model Making use of the tensors with the model is extremely simple — we just call the model with the inputs: ``` output = model(model_inputs)``` While the model accepts a lot of different arguments, only the input IDs are necessary. We’ll explain what the other arguments do and when they are required later, but first we need to take a closer look at the tokenizers that build the inputs that a Transformer model can understand.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Models - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter2/3"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Models - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;models&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;creating-a-transformer&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;different-loading-methods&quot;,&quot;title&quot;:&quot;Different loading methods&quot;},{&quot;local&quot;:&quot;saving-methods&quot;,&quot;title&quot;:&quot;Saving methods&quot;}],&quot;title&quot;:&quot;Creating a Transformer&quot;},{&quot;local&quot;:&quot;using-a-transformer-model-for-inference&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;using-the-tensors-as-inputs-to-the-model&quot;,&quot;title&quot;:&quot;Using the tensors as inputs to the model&quot;}],&quot;title&quot;:&quot;Using a Transformer model for inference&quot;}],&quot;title&quot;:&quot;Models&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter2/3&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Models&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Models</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/2?fw=pt">Behind the pipeline </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter2/3?fw=pt">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/4?fw=pt">Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/5?fw=pt">Handling multiple sequences </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/6?fw=pt">Putting it all together </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/7?fw=pt">Basic usage completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Models</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-2-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter2/section3_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter2/section3_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/AhChOFRegn4" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>In this section we’ll take a closer look at creating and using a model. We’ll use the <code>AutoModel</code> class, which is handy when you want to instantiate any model from a checkpoint.</p> <p>The <code>AutoModel</code> class and all of its relatives are actually simple wrappers over the wide variety of models available in the library. It’s a clever wrapper as it can automatically guess the appropriate model architecture for your checkpoint, and then instantiates a model with this architecture.</p> <p>However, if you know the type of model you want to use, you can use the class that defines its architecture directly. Let’s take a look at how this works with a BERT model.</p> <h2 class="relative group"><a id="creating-a-transformer" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#creating-a-transformer"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Creating a Transformer</span></h2> <p>The first thing we’ll need to do to initialize a BERT model is load a configuration object:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> BertConfig, BertModel <span class="hljs-comment"># Building the config</span> config = BertConfig() <span class="hljs-comment"># Building the model from the config</span> model = BertModel(config)</pre></div> <p>The configuration contains many attributes that are used to build the model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(config)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>BertConfig { [...] <span class="hljs-string">"hidden_size"</span>: <span class="hljs-number">768</span>, <span class="hljs-string">"intermediate_size"</span>: <span class="hljs-number">3072</span>, <span class="hljs-string">"max_position_embeddings"</span>: <span class="hljs-number">512</span>, <span class="hljs-string">"num_attention_heads"</span>: <span class="hljs-number">12</span>, <span class="hljs-string">"num_hidden_layers"</span>: <span class="hljs-number">12</span>, [...] }</pre></div> <p>While you haven’t seen what all of these attributes do yet, you should recognize some of them: the <code>hidden_size</code> attribute defines the size of the <code>hidden_states</code> vector, and <code>num_hidden_layers</code> defines the number of layers the Transformer model has.</p> <h3 class="relative group"><a id="different-loading-methods" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#different-loading-methods"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Different loading methods</span></h3> <p>Creating a model from the default configuration initializes it with random values:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> BertConfig, BertModel config = BertConfig() model = BertModel(config) <span class="hljs-comment"># Model is randomly initialized!</span></pre></div> <p>The model can be used in this state, but it will output gibberish; it needs to be trained first. We could train the model from scratch on the task at hand, but as you saw in <a href="/course/chapter1">Chapter 1</a>, this would require a long time and a lot of data, and it would have a non-negligible environmental impact. To avoid unnecessary and duplicated effort, it’s imperative to be able to share and reuse models that have already been trained.</p> <p>Loading a Transformer model that is already trained is simple — we can do this using the <code>from_pretrained()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> BertModel model = BertModel.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>)</pre></div> <p>As you saw earlier, we could replace <code>BertModel</code> with the equivalent <code>AutoModel</code> class. We’ll do this from now on as this produces checkpoint-agnostic code; if your code works for one checkpoint, it should work seamlessly with another. This applies even if the architecture is different, as long as the checkpoint was trained for a similar task (for example, a sentiment analysis task).</p> <p>In the code sample above we didn’t use <code>BertConfig</code>, and instead loaded a pretrained model via the <code>bert-base-cased</code> identifier. This is a model checkpoint that was trained by the authors of BERT themselves; you can find more details about it in its <a href="https://huggingface.co/bert-base-cased" rel="nofollow">model card</a>.</p> <p>This model is now initialized with all the weights of the checkpoint. It can be used directly for inference on the tasks it was trained on, and it can also be fine-tuned on a new task. By training with pretrained weights rather than from scratch, we can quickly achieve good results.</p> <p>The weights have been downloaded and cached (so future calls to the <code>from_pretrained()</code> method won’t re-download them) in the cache folder, which defaults to <em>~/.cache/huggingface/transformers</em>. You can customize your cache folder by setting the <code>HF_HOME</code> environment variable.</p> <p>The identifier used to load the model can be the identifier of any model on the Model Hub, as long as it is compatible with the BERT architecture. The entire list of available BERT checkpoints can be found <a href="https://huggingface.co/models?filter=bert" rel="nofollow">here</a>.</p> <h3 class="relative group"><a id="saving-methods" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#saving-methods"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Saving methods</span></h3> <p>Saving a model is as easy as loading one — we use the <code>save_pretrained()</code> method, which is analogous to the <code>from_pretrained()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model.save_pretrained(<span class="hljs-string">"directory_on_my_computer"</span>)</pre></div> <p>This saves two files to your disk:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>ls <span class="hljs-keyword">directory_on_my_computer </span> <span class="hljs-built_in">config</span>.<span class="hljs-keyword">json </span>pytorch_model.<span class="hljs-keyword">bin</span></pre></div> <p>If you take a look at the <em>config.json</em> file, you’ll recognize the attributes necessary to build the model architecture. This file also contains some metadata, such as where the checkpoint originated and what 🤗 Transformers version you were using when you last saved the checkpoint.</p> <p>The <em>pytorch_model.bin</em> file is known as the <em>state dictionary</em>; it contains all your model’s weights. The two files go hand in hand; the configuration is necessary to know your model’s architecture, while the model weights are your model’s parameters.</p> <h2 class="relative group"><a id="using-a-transformer-model-for-inference" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-a-transformer-model-for-inference"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using a Transformer model for inference</span></h2> <p>Now that you know how to load and save a model, let’s try using it to make some predictions. Transformer models can only process numbers — numbers that the tokenizer generates. But before we discuss tokenizers, let’s explore what inputs the model accepts.</p> <p>Tokenizers can take care of casting the inputs to the appropriate framework’s tensors, but to help you understand what’s going on, we’ll take a quick look at what must be done before sending the inputs to the model.</p> <p>Let’s say we have a couple of sequences:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sequences = [<span class="hljs-string">"Hello!"</span>, <span class="hljs-string">"Cool."</span>, <span class="hljs-string">"Nice!"</span>]</pre></div> <p>The tokenizer converts these to vocabulary indices which are typically called <em>input IDs</em>. Each sequence is now a list of numbers! The resulting output is:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>encoded_sequences = [ [<span class="hljs-number">101</span>, <span class="hljs-number">7592</span>, <span class="hljs-number">999</span>, <span class="hljs-number">102</span>], [<span class="hljs-number">101</span>, <span class="hljs-number">4658</span>, <span class="hljs-number">1012</span>, <span class="hljs-number">102</span>], [<span class="hljs-number">101</span>, <span class="hljs-number">3835</span>, <span class="hljs-number">999</span>, <span class="hljs-number">102</span>], ]</pre></div> <p>This is a list of encoded sequences: a list of lists. Tensors only accept rectangular shapes (think matrices). This “array” is already of rectangular shape, so converting it to a tensor is easy:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch model_inputs = torch.tensor(encoded_sequences)</pre></div> <h3 class="relative group"><a id="using-the-tensors-as-inputs-to-the-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-the-tensors-as-inputs-to-the-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using the tensors as inputs to the model</span></h3> <p>Making use of the tensors with the model is extremely simple — we just call the model with the inputs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>output = model(model_inputs)</pre></div> <p>While the model accepts a lot of different arguments, only the input IDs are necessary. We’ll explain what the other arguments do and when they are required later, but first we need to take a closer look at the tokenizers that build the inputs that a Transformer model can understand.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter2/2?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Behind the pipeline</a> <a href="/learn/nlp-course/chapter2/4?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Tokenizers<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;models&quot;,&quot;url&quot;:&quot;#models&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Creating a Transformer&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;creating-a-transformer&quot;,&quot;url&quot;:&quot;#creating-a-transformer&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Different loading methods&quot;,&quot;id&quot;:&quot;different-loading-methods&quot;,&quot;url&quot;:&quot;#different-loading-methods&quot;},{&quot;title&quot;:&quot;Saving methods&quot;,&quot;id&quot;:&quot;saving-methods&quot;,&quot;url&quot;:&quot;#saving-methods&quot;}]},{&quot;title&quot;:&quot;Using a Transformer model for inference&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;using-a-transformer-model-for-inference&quot;,&quot;url&quot;:&quot;#using-a-transformer-model-for-inference&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Using the tensors as inputs to the model&quot;,&quot;id&quot;:&quot;using-the-tensors-as-inputs-to-the-model&quot;,&quot;url&quot;:&quot;#using-the-tensors-as-inputs-to-the-model&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#models" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-models"><wbr>Models</a> <a href="#creating-a-transformer" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-creating-a-transformer"><wbr>Creating a <wbr>Transformer</a> <a href="#different-loading-methods" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-different-loading-methods"><wbr>Different loading methods</a> <a href="#saving-methods" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-saving-methods"><wbr>Saving methods</a> <a href="#using-a-transformer-model-for-inference" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-a-transformer-model-for-inference"><wbr>Using a <wbr>Transformer model for inference</a> <a href="#using-the-tensors-as-inputs-to-the-model" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-the-tensors-as-inputs-to-the-model"><wbr>Using the tensors as inputs to the model</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter2/3" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter2/3"); } </script> <iframe name="__privateStripeMetricsController2100" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter2%2F3%3Ffw%3Dpt&amp;title=Models%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:08.326Z
Behind the pipeline - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter2/2?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#behind-the-pipeline)Behind the pipeline [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-2-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter2/section2_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter2/section2_pt.ipynb) This is the first section where the content is slightly different depending on whether you use PyTorch or TensorFlow. Toggle the switch on top of the title to select the platform you prefer! Let’s start with a complete example, taking a look at what happened behind the scenes when we executed the following code in [Chapter 1](/course/chapter1): ``` from transformers import pipeline classifier = pipeline("sentiment-analysis") classifier( [ "I've been waiting for a HuggingFace course my whole life.", "I hate this so much!", ] )``` and obtained: ``` [{'label': 'POSITIVE', 'score': 0.9598047137260437}, {'label': 'NEGATIVE', 'score': 0.9994558095932007}]``` As we saw in [Chapter 1](/course/chapter1), this pipeline groups together three steps: preprocessing, passing the inputs through the model, and postprocessing: ![The full NLP pipeline: tokenization of text, conversion to IDs, and inference through the Transformer model and the model head.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/full_nlp_pipeline.svg) ![The full NLP pipeline: tokenization of text, conversion to IDs, and inference through the Transformer model and the model head.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/full_nlp_pipeline-dark.svg) Let’s quickly go over each of these. ## [](#preprocessing-with-a-tokenizer)Preprocessing with a tokenizer Like other neural networks, Transformer models can’t process raw text directly, so the first step of our pipeline is to convert the text inputs into numbers that the model can make sense of. To do this we use a _tokenizer_, which will be responsible for: - Splitting the input into words, subwords, or symbols (like punctuation) that are called _tokens_ - Mapping each token to an integer - Adding additional inputs that may be useful to the model All this preprocessing needs to be done in exactly the same way as when the model was pretrained, so we first need to download that information from the [Model Hub](https://huggingface.co/models). To do this, we use the `AutoTokenizer` class and its `from_pretrained()` method. Using the checkpoint name of our model, it will automatically fetch the data associated with the model’s tokenizer and cache it (so it’s only downloaded the first time you run the code below). Since the default checkpoint of the `sentiment-analysis` pipeline is `distilbert-base-uncased-finetuned-sst-2-english` (you can see its model card [here](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english)), we run the following: ``` from transformers import AutoTokenizer checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(checkpoint)``` Once we have the tokenizer, we can directly pass our sentences to it and we’ll get back a dictionary that’s ready to feed to our model! The only thing left to do is to convert the list of input IDs to tensors. You can use 🤗 Transformers without having to worry about which ML framework is used as a backend; it might be PyTorch or TensorFlow, or Flax for some models. However, Transformer models only accept _tensors_ as input. If this is your first time hearing about tensors, you can think of them as NumPy arrays instead. A NumPy array can be a scalar (0D), a vector (1D), a matrix (2D), or have more dimensions. It’s effectively a tensor; other ML frameworks’ tensors behave similarly, and are usually as simple to instantiate as NumPy arrays. To specify the type of tensors we want to get back (PyTorch, TensorFlow, or plain NumPy), we use the `return_tensors` argument: ``` raw_inputs = [ "I've been waiting for a HuggingFace course my whole life.", "I hate this so much!", ] inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors="pt") print(inputs)``` Don’t worry about padding and truncation just yet; we’ll explain those later. The main things to remember here are that you can pass one sentence or a list of sentences, as well as specifying the type of tensors you want to get back (if no type is passed, you will get a list of lists as a result). Here’s what the results look like as PyTorch tensors: ``` { 'input_ids': tensor([ [ 101, 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012, 102], [ 101, 1045, 5223, 2023, 2061, 2172, 999, 102, 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, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0] ]) }``` The output itself is a dictionary containing two keys, `input_ids` and `attention_mask`. `input_ids` contains two rows of integers (one for each sentence) that are the unique identifiers of the tokens in each sentence. We’ll explain what the `attention_mask` is later in this chapter. ## [](#going-through-the-model)Going through the model We can download our pretrained model the same way we did with our tokenizer. 🤗 Transformers provides an `AutoModel` class which also has a `from_pretrained()` method: ``` from transformers import AutoModel checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" model = AutoModel.from_pretrained(checkpoint)``` In this code snippet, we have downloaded the same checkpoint we used in our pipeline before (it should actually have been cached already) and instantiated a model with it. This architecture contains only the base Transformer module: given some inputs, it outputs what we’ll call _hidden states_, also known as _features_. For each model input, we’ll retrieve a high-dimensional vector representing the **contextual understanding of that input by the Transformer model**. If this doesn’t make sense, don’t worry about it. We’ll explain it all later. While these hidden states can be useful on their own, they’re usually inputs to another part of the model, known as the _head_. In [Chapter 1](/course/chapter1), the different tasks could have been performed with the same architecture, but each of these tasks will have a different head associated with it. ### [](#a-high-dimensional-vector)A high-dimensional vector? The vector output by the Transformer module is usually large. It generally has three dimensions: - **Batch size**: The number of sequences processed at a time (2 in our example). - **Sequence length**: The length of the numerical representation of the sequence (16 in our example). - **Hidden size**: The vector dimension of each model input. It is said to be “high dimensional” because of the last value. The hidden size can be very large (768 is common for smaller models, and in larger models this can reach 3072 or more). We can see this if we feed the inputs we preprocessed to our model: ``` outputs = model(**inputs) print(outputs.last_hidden_state.shape)``` Note that the outputs of 🤗 Transformers models behave like `namedtuple`s or dictionaries. You can access the elements by attributes (like we did) or by key (`outputs["last_hidden_state"]`), or even by index if you know exactly where the thing you are looking for is (`outputs[0]`). ### [](#model-heads-making-sense-out-of-numbers)Model heads: Making sense out of numbers The model heads take the high-dimensional vector of hidden states as input and project them onto a different dimension. They are usually composed of one or a few linear layers: ![A Transformer network alongside its head.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/transformer_and_head.svg) ![A Transformer network alongside its head.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/transformer_and_head-dark.svg) The output of the Transformer model is sent directly to the model head to be processed. In this diagram, the model is represented by its embeddings layer and the subsequent layers. The embeddings layer converts each input ID in the tokenized input into a vector that represents the associated token. The subsequent layers manipulate those vectors using the attention mechanism to produce the final representation of the sentences. There are many different architectures available in 🤗 Transformers, with each one designed around tackling a specific task. Here is a non-exhaustive list: - `*Model` (retrieve the hidden states) - `*ForCausalLM` - `*ForMaskedLM` - `*ForMultipleChoice` - `*ForQuestionAnswering` - `*ForSequenceClassification` - `*ForTokenClassification` - and others 🤗 For our example, we will need a model with a sequence classification head (to be able to classify the sentences as positive or negative). So, we won’t actually use the `AutoModel` class, but `AutoModelForSequenceClassification`: ``` from transformers import AutoModelForSequenceClassification checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" model = AutoModelForSequenceClassification.from_pretrained(checkpoint) outputs = model(**inputs)``` Now if we look at the shape of our outputs, the dimensionality will be much lower: the model head takes as input the high-dimensional vectors we saw before, and outputs vectors containing two values (one per label): ``` print(outputs.logits.shape)``` Since we have just two sentences and two labels, the result we get from our model is of shape 2 x 2. ## [](#postprocessing-the-output)Postprocessing the output The values we get as output from our model don’t necessarily make sense by themselves. Let’s take a look: ``` tensor([[-1.5607, 1.6123], [ 4.1692, -3.3464]], grad_fn=<AddmmBackward>)``` Our model predicted `[-1.5607, 1.6123]` for the first sentence and `[ 4.1692, -3.3464]` for the second one. Those are not probabilities but _logits_, the raw, unnormalized scores outputted by the last layer of the model. To be converted to probabilities, they need to go through a [SoftMax](https://en.wikipedia.org/wiki/Softmax_function) layer (all 🤗 Transformers models output the logits, as the loss function for training will generally fuse the last activation function, such as SoftMax, with the actual loss function, such as cross entropy): ``` import torch predictions = torch.nn.functional.softmax(outputs.logits, dim=-1) print(predictions)``` ``` tensor([[4.0195e-02, 9.5980e-01], [9.9946e-01, 5.4418e-04]], grad_fn=<SoftmaxBackward>)``` Now we can see that the model predicted `[0.0402, 0.9598]` for the first sentence and `[0.9995, 0.0005]` for the second one. These are recognizable probability scores. To get the labels corresponding to each position, we can inspect the `id2label` attribute of the model config (more on this in the next section): ``` {0: 'NEGATIVE', 1: 'POSITIVE'}``` Now we can conclude that the model predicted the following: - First sentence: NEGATIVE: 0.0402, POSITIVE: 0.9598 - Second sentence: NEGATIVE: 0.9995, POSITIVE: 0.0005 We have successfully reproduced the three steps of the pipeline: preprocessing with tokenizers, passing the inputs through the model, and postprocessing! Now let’s take some time to dive deeper into each of those steps. ✏️ **Try it out!** Choose two (or more) texts of your own and run them through the `sentiment-analysis` pipeline. Then replicate the steps you saw here yourself and check that you obtain the same results!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Behind the pipeline - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter2/2"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Behind the pipeline - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;behind-the-pipeline&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;preprocessing-with-a-tokenizer&quot;,&quot;title&quot;:&quot;Preprocessing with a tokenizer&quot;},{&quot;local&quot;:&quot;going-through-the-model&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;a-high-dimensional-vector&quot;,&quot;title&quot;:&quot;A high-dimensional vector?&quot;},{&quot;local&quot;:&quot;model-heads-making-sense-out-of-numbers&quot;,&quot;title&quot;:&quot;Model heads: Making sense out of numbers&quot;}],&quot;title&quot;:&quot;Going through the model&quot;},{&quot;local&quot;:&quot;postprocessing-the-output&quot;,&quot;title&quot;:&quot;Postprocessing the output&quot;}],&quot;title&quot;:&quot;Behind the pipeline&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter2/2&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Behind the pipeline&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Behind the pipeline</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/1?fw=pt">Introduction </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter2/2?fw=pt">Behind the pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/3?fw=pt">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/4?fw=pt">Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/5?fw=pt">Handling multiple sequences </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/6?fw=pt">Putting it all together </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/7?fw=pt">Basic usage completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="behind-the-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#behind-the-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Behind the pipeline</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-2-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter2/section2_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter2/section2_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400">This is the first section where the content is slightly different depending on whether you use PyTorch or TensorFlow. Toggle the switch on top of the title to select the platform you prefer!</div> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/1pedAIvTWXk" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Let’s start with a complete example, taking a look at what happened behind the scenes when we executed the following code in <a href="/course/chapter1">Chapter 1</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline classifier = pipeline(<span class="hljs-string">"sentiment-analysis"</span>) classifier( [ <span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span>, <span class="hljs-string">"I hate this so much!"</span>, ] )</pre></div> <p>and obtained:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'label'</span>: <span class="hljs-string">'POSITIVE'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9598047137260437</span>}, {<span class="hljs-string">'label'</span>: <span class="hljs-string">'NEGATIVE'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9994558095932007</span>}]</pre></div> <p>As we saw in <a href="/course/chapter1">Chapter 1</a>, this pipeline groups together three steps: preprocessing, passing the inputs through the model, and postprocessing:</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/full_nlp_pipeline.svg" alt="The full NLP pipeline: tokenization of text, conversion to IDs, and inference through the Transformer model and the model head."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/full_nlp_pipeline-dark.svg" alt="The full NLP pipeline: tokenization of text, conversion to IDs, and inference through the Transformer model and the model head."></div> <p>Let’s quickly go over each of these.</p> <h2 class="relative group"><a id="preprocessing-with-a-tokenizer" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preprocessing-with-a-tokenizer"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preprocessing with a tokenizer</span></h2> <p>Like other neural networks, Transformer models can’t process raw text directly, so the first step of our pipeline is to convert the text inputs into numbers that the model can make sense of. To do this we use a <em>tokenizer</em>, which will be responsible for:</p> <ul><li>Splitting the input into words, subwords, or symbols (like punctuation) that are called <em>tokens</em></li> <li>Mapping each token to an integer</li> <li>Adding additional inputs that may be useful to the model</li></ul> <p>All this preprocessing needs to be done in exactly the same way as when the model was pretrained, so we first need to download that information from the <a href="https://huggingface.co/models" rel="nofollow">Model Hub</a>. To do this, we use the <code>AutoTokenizer</code> class and its <code>from_pretrained()</code> method. Using the checkpoint name of our model, it will automatically fetch the data associated with the model’s tokenizer and cache it (so it’s only downloaded the first time you run the code below).</p> <p>Since the default checkpoint of the <code>sentiment-analysis</code> pipeline is <code>distilbert-base-uncased-finetuned-sst-2-english</code> (you can see its model card <a href="https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english" rel="nofollow">here</a>), we run the following:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer checkpoint = <span class="hljs-string">"distilbert-base-uncased-finetuned-sst-2-english"</span> tokenizer = AutoTokenizer.from_pretrained(checkpoint)</pre></div> <p>Once we have the tokenizer, we can directly pass our sentences to it and we’ll get back a dictionary that’s ready to feed to our model! The only thing left to do is to convert the list of input IDs to tensors.</p> <p>You can use 🤗 Transformers without having to worry about which ML framework is used as a backend; it might be PyTorch or TensorFlow, or Flax for some models. However, Transformer models only accept <em>tensors</em> as input. If this is your first time hearing about tensors, you can think of them as NumPy arrays instead. A NumPy array can be a scalar (0D), a vector (1D), a matrix (2D), or have more dimensions. It’s effectively a tensor; other ML frameworks’ tensors behave similarly, and are usually as simple to instantiate as NumPy arrays.</p> <p>To specify the type of tensors we want to get back (PyTorch, TensorFlow, or plain NumPy), we use the <code>return_tensors</code> argument:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>raw_inputs = [ <span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span>, <span class="hljs-string">"I hate this so much!"</span>, ] inputs = tokenizer(raw_inputs, padding=<span class="hljs-literal">True</span>, truncation=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-built_in">print</span>(inputs)</pre></div> <p>Don’t worry about padding and truncation just yet; we’ll explain those later. The main things to remember here are that you can pass one sentence or a list of sentences, as well as specifying the type of tensors you want to get back (if no type is passed, you will get a list of lists as a result).</p> <p>Here’s what the results look like as PyTorch tensors:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{ <span class="hljs-string">'input_ids'</span>: tensor([ [ <span class="hljs-number">101</span>, <span class="hljs-number">1045</span>, <span class="hljs-number">1005</span>, <span class="hljs-number">2310</span>, <span class="hljs-number">2042</span>, <span class="hljs-number">3403</span>, <span class="hljs-number">2005</span>, <span class="hljs-number">1037</span>, <span class="hljs-number">17662</span>, <span class="hljs-number">12172</span>, <span class="hljs-number">2607</span>, <span class="hljs-number">2026</span>, <span class="hljs-number">2878</span>, <span class="hljs-number">2166</span>, <span class="hljs-number">1012</span>, <span class="hljs-number">102</span>], [ <span class="hljs-number">101</span>, <span class="hljs-number">1045</span>, <span class="hljs-number">5223</span>, <span class="hljs-number">2023</span>, <span class="hljs-number">2061</span>, <span class="hljs-number">2172</span>, <span class="hljs-number">999</span>, <span class="hljs-number">102</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>] ]), <span class="hljs-string">'attention_mask'</span>: tensor([ [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>] ]) }</pre></div> <p>The output itself is a dictionary containing two keys, <code>input_ids</code> and <code>attention_mask</code>. <code>input_ids</code> contains two rows of integers (one for each sentence) that are the unique identifiers of the tokens in each sentence. We’ll explain what the <code>attention_mask</code> is later in this chapter.</p> <h2 class="relative group"><a id="going-through-the-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#going-through-the-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Going through the model</span></h2> <p>We can download our pretrained model the same way we did with our tokenizer. 🤗 Transformers provides an <code>AutoModel</code> class which also has a <code>from_pretrained()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModel checkpoint = <span class="hljs-string">"distilbert-base-uncased-finetuned-sst-2-english"</span> model = AutoModel.from_pretrained(checkpoint)</pre></div> <p>In this code snippet, we have downloaded the same checkpoint we used in our pipeline before (it should actually have been cached already) and instantiated a model with it.</p> <p>This architecture contains only the base Transformer module: given some inputs, it outputs what we’ll call <em>hidden states</em>, also known as <em>features</em>. For each model input, we’ll retrieve a high-dimensional vector representing the <strong>contextual understanding of that input by the Transformer model</strong>.</p> <p>If this doesn’t make sense, don’t worry about it. We’ll explain it all later.</p> <p>While these hidden states can be useful on their own, they’re usually inputs to another part of the model, known as the <em>head</em>. In <a href="/course/chapter1">Chapter 1</a>, the different tasks could have been performed with the same architecture, but each of these tasks will have a different head associated with it.</p> <h3 class="relative group"><a id="a-high-dimensional-vector" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#a-high-dimensional-vector"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>A high-dimensional vector?</span></h3> <p>The vector output by the Transformer module is usually large. It generally has three dimensions:</p> <ul><li><strong>Batch size</strong>: The number of sequences processed at a time (2 in our example).</li> <li><strong>Sequence length</strong>: The length of the numerical representation of the sequence (16 in our example).</li> <li><strong>Hidden size</strong>: The vector dimension of each model input.</li></ul> <p>It is said to be “high dimensional” because of the last value. The hidden size can be very large (768 is common for smaller models, and in larger models this can reach 3072 or more).</p> <p>We can see this if we feed the inputs we preprocessed to our model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>outputs = model(**inputs) <span class="hljs-built_in">print</span>(outputs.last_hidden_state.shape)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>torch.Size([<span class="hljs-number">2</span>, <span class="hljs-number">16</span>, <span class="hljs-number">768</span>])</pre></div> <p>Note that the outputs of 🤗 Transformers models behave like <code>namedtuple</code>s or dictionaries. You can access the elements by attributes (like we did) or by key (<code>outputs["last_hidden_state"]</code>), or even by index if you know exactly where the thing you are looking for is (<code>outputs[0]</code>).</p> <h3 class="relative group"><a id="model-heads-making-sense-out-of-numbers" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#model-heads-making-sense-out-of-numbers"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Model heads: Making sense out of numbers</span></h3> <p>The model heads take the high-dimensional vector of hidden states as input and project them onto a different dimension. They are usually composed of one or a few linear layers:</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/transformer_and_head.svg" alt="A Transformer network alongside its head."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/transformer_and_head-dark.svg" alt="A Transformer network alongside its head."></div> <p>The output of the Transformer model is sent directly to the model head to be processed.</p> <p>In this diagram, the model is represented by its embeddings layer and the subsequent layers. The embeddings layer converts each input ID in the tokenized input into a vector that represents the associated token. The subsequent layers manipulate those vectors using the attention mechanism to produce the final representation of the sentences.</p> <p>There are many different architectures available in 🤗 Transformers, with each one designed around tackling a specific task. Here is a non-exhaustive list:</p> <ul><li><code>*Model</code> (retrieve the hidden states)</li> <li><code>*ForCausalLM</code></li> <li><code>*ForMaskedLM</code></li> <li><code>*ForMultipleChoice</code></li> <li><code>*ForQuestionAnswering</code></li> <li><code>*ForSequenceClassification</code></li> <li><code>*ForTokenClassification</code></li> <li>and others 🤗</li></ul> <p>For our example, we will need a model with a sequence classification head (to be able to classify the sentences as positive or negative). So, we won’t actually use the <code>AutoModel</code> class, but <code>AutoModelForSequenceClassification</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification checkpoint = <span class="hljs-string">"distilbert-base-uncased-finetuned-sst-2-english"</span> model = AutoModelForSequenceClassification.from_pretrained(checkpoint) outputs = model(**inputs)</pre></div> <p>Now if we look at the shape of our outputs, the dimensionality will be much lower: the model head takes as input the high-dimensional vectors we saw before, and outputs vectors containing two values (one per label):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(outputs.logits.shape)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>torch.Size([<span class="hljs-number">2</span>, <span class="hljs-number">2</span>])</pre></div> <p>Since we have just two sentences and two labels, the result we get from our model is of shape 2 x 2.</p> <h2 class="relative group"><a id="postprocessing-the-output" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#postprocessing-the-output"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Postprocessing the output</span></h2> <p>The values we get as output from our model don’t necessarily make sense by themselves. Let’s take a look:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(outputs.logits)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tensor([[-<span class="hljs-number">1.5607</span>, <span class="hljs-number">1.6123</span>], [ <span class="hljs-number">4.1692</span>, -<span class="hljs-number">3.3464</span>]], grad_fn=&lt;AddmmBackward&gt;)</pre></div> <p>Our model predicted <code>[-1.5607, 1.6123]</code> for the first sentence and <code>[ 4.1692, -3.3464]</code> for the second one. Those are not probabilities but <em>logits</em>, the raw, unnormalized scores outputted by the last layer of the model. To be converted to probabilities, they need to go through a <a href="https://en.wikipedia.org/wiki/Softmax_function" rel="nofollow">SoftMax</a> layer (all 🤗 Transformers models output the logits, as the loss function for training will generally fuse the last activation function, such as SoftMax, with the actual loss function, such as cross entropy):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch predictions = torch.nn.functional.softmax(outputs.logits, dim=-<span class="hljs-number">1</span>) <span class="hljs-built_in">print</span>(predictions)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tensor([[<span class="hljs-number">4.0195e-02</span>, <span class="hljs-number">9.5980e-01</span>], [<span class="hljs-number">9.9946e-01</span>, <span class="hljs-number">5.4418e-04</span>]], grad_fn=&lt;SoftmaxBackward&gt;)</pre></div> <p>Now we can see that the model predicted <code>[0.0402, 0.9598]</code> for the first sentence and <code>[0.9995, 0.0005]</code> for the second one. These are recognizable probability scores.</p> <p>To get the labels corresponding to each position, we can inspect the <code>id2label</code> attribute of the model config (more on this in the next section):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model.config.id2label</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-number">0</span>: <span class="hljs-string">'NEGATIVE'</span>, <span class="hljs-number">1</span>: <span class="hljs-string">'POSITIVE'</span>}</pre></div> <p>Now we can conclude that the model predicted the following:</p> <ul><li>First sentence: NEGATIVE: 0.0402, POSITIVE: 0.9598</li> <li>Second sentence: NEGATIVE: 0.9995, POSITIVE: 0.0005</li></ul> <p>We have successfully reproduced the three steps of the pipeline: preprocessing with tokenizers, passing the inputs through the model, and postprocessing! Now let’s take some time to dive deeper into each of those steps.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Choose two (or more) texts of your own and run them through the <code>sentiment-analysis</code> pipeline. Then replicate the steps you saw here yourself and check that you obtain the same results!</p></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter2/1?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Introduction</a> <a href="/learn/nlp-course/chapter2/3?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Models<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;behind-the-pipeline&quot;,&quot;url&quot;:&quot;#behind-the-pipeline&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Preprocessing with a tokenizer&quot;,&quot;id&quot;:&quot;preprocessing-with-a-tokenizer&quot;,&quot;url&quot;:&quot;#preprocessing-with-a-tokenizer&quot;},{&quot;title&quot;:&quot;Going through the model&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;going-through-the-model&quot;,&quot;url&quot;:&quot;#going-through-the-model&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;A high-dimensional vector?&quot;,&quot;id&quot;:&quot;a-high-dimensional-vector&quot;,&quot;url&quot;:&quot;#a-high-dimensional-vector&quot;},{&quot;title&quot;:&quot;Model heads: Making sense out of numbers&quot;,&quot;id&quot;:&quot;model-heads-making-sense-out-of-numbers&quot;,&quot;url&quot;:&quot;#model-heads-making-sense-out-of-numbers&quot;}]},{&quot;title&quot;:&quot;Postprocessing the output&quot;,&quot;id&quot;:&quot;postprocessing-the-output&quot;,&quot;url&quot;:&quot;#postprocessing-the-output&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#behind-the-pipeline" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-behind-the-pipeline"><wbr>Behind the pipeline</a> <a href="#preprocessing-with-a-tokenizer" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preprocessing-with-a-tokenizer"><wbr>Preprocessing with a tokenizer</a> <a href="#going-through-the-model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-going-through-the-model"><wbr>Going through the model</a> <a href="#a-high-dimensional-vector" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-a-high-dimensional-vector"><wbr>A high-dimensional vector?</a> <a href="#model-heads-making-sense-out-of-numbers" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-model-heads-making-sense-out-of-numbers"><wbr>Model heads: <wbr>Making sense out of numbers</a> <a href="#postprocessing-the-output" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-postprocessing-the-output"><wbr>Postprocessing the output</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter2/2" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter2/2"); } </script> <iframe name="__privateStripeMetricsController4630" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter2%2F2%3Ffw%3Dpt&amp;title=Behind%20the%20pipeline%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:08.761Z
Tokenizers - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter2/4?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#tokenizers)Tokenizers [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-2-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter2/section4_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter2/section4_pt.ipynb) Tokenizers are one of the core components of the NLP pipeline. They serve one purpose: to translate text into data that can be processed by the model. Models can only process numbers, so tokenizers need to convert our text inputs to numerical data. In this section, we’ll explore exactly what happens in the tokenization pipeline. In NLP tasks, the data that is generally processed is raw text. Here’s an example of such text: However, models can only process numbers, so we need to find a way to convert the raw text to numbers. That’s what the tokenizers do, and there are a lot of ways to go about this. The goal is to find the most meaningful representation — that is, the one that makes the most sense to the model — and, if possible, the smallest representation. Let’s take a look at some examples of tokenization algorithms, and try to answer some of the questions you may have about tokenization. ## [](#word-based)Word-based The first type of tokenizer that comes to mind is _word-based_. It’s generally very easy to set up and use with only a few rules, and it often yields decent results. For example, in the image below, the goal is to split the raw text into words and find a numerical representation for each of them: ![An example of word-based tokenization.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/word_based_tokenization.svg) ![An example of word-based tokenization.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/word_based_tokenization-dark.svg) There are different ways to split the text. For example, we could use whitespace to tokenize the text into words by applying Python’s `split()` function: ``` tokenized_text = "Jim Henson was a puppeteer".split() print(tokenized_text)``` ``` ['Jim', 'Henson', 'was', 'a', 'puppeteer']``` There are also variations of word tokenizers that have extra rules for punctuation. With this kind of tokenizer, we can end up with some pretty large “vocabularies,” where a vocabulary is defined by the total number of independent tokens that we have in our corpus. Each word gets assigned an ID, starting from 0 and going up to the size of the vocabulary. The model uses these IDs to identify each word. If we want to completely cover a language with a word-based tokenizer, we’ll need to have an identifier for each word in the language, which will generate a huge amount of tokens. For example, there are over 500,000 words in the English language, so to build a map from each word to an input ID we’d need to keep track of that many IDs. Furthermore, words like “dog” are represented differently from words like “dogs”, and the model will initially have no way of knowing that “dog” and “dogs” are similar: it will identify the two words as unrelated. The same applies to other similar words, like “run” and “running”, which the model will not see as being similar initially. Finally, we need a custom token to represent words that are not in our vocabulary. This is known as the “unknown” token, often represented as ”\[UNK\]” or ””. It’s generally a bad sign if you see that the tokenizer is producing a lot of these tokens, as it wasn’t able to retrieve a sensible representation of a word and you’re losing information along the way. The goal when crafting the vocabulary is to do it in such a way that the tokenizer tokenizes as few words as possible into the unknown token. One way to reduce the amount of unknown tokens is to go one level deeper, using a _character-based_ tokenizer. ## [](#character-based)Character-based Character-based tokenizers split the text into characters, rather than words. This has two primary benefits: - The vocabulary is much smaller. - There are much fewer out-of-vocabulary (unknown) tokens, since every word can be built from characters. But here too some questions arise concerning spaces and punctuation: ![An example of character-based tokenization.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/character_based_tokenization.svg) ![An example of character-based tokenization.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/character_based_tokenization-dark.svg) This approach isn’t perfect either. Since the representation is now based on characters rather than words, one could argue that, intuitively, it’s less meaningful: each character doesn’t mean a lot on its own, whereas that is the case with words. However, this again differs according to the language; in Chinese, for example, each character carries more information than a character in a Latin language. Another thing to consider is that we’ll end up with a very large amount of tokens to be processed by our model: whereas a word would only be a single token with a word-based tokenizer, it can easily turn into 10 or more tokens when converted into characters. To get the best of both worlds, we can use a third technique that combines the two approaches: _subword tokenization_. ## [](#subword-tokenization)Subword tokenization Subword tokenization algorithms rely on the principle that frequently used words should not be split into smaller subwords, but rare words should be decomposed into meaningful subwords. For instance, “annoyingly” might be considered a rare word and could be decomposed into “annoying” and “ly”. These are both likely to appear more frequently as standalone subwords, while at the same time the meaning of “annoyingly” is kept by the composite meaning of “annoying” and “ly”. Here is an example showing how a subword tokenization algorithm would tokenize the sequence “Let’s do tokenization!“: ![A subword tokenization algorithm.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/bpe_subword.svg) ![A subword tokenization algorithm.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/bpe_subword-dark.svg) These subwords end up providing a lot of semantic meaning: for instance, in the example above “tokenization” was split into “token” and “ization”, two tokens that have a semantic meaning while being space-efficient (only two tokens are needed to represent a long word). This allows us to have relatively good coverage with small vocabularies, and close to no unknown tokens. This approach is especially useful in agglutinative languages such as Turkish, where you can form (almost) arbitrarily long complex words by stringing together subwords. ### [](#and-more)And more! Unsurprisingly, there are many more techniques out there. To name a few: - Byte-level BPE, as used in GPT-2 - WordPiece, as used in BERT - SentencePiece or Unigram, as used in several multilingual models You should now have sufficient knowledge of how tokenizers work to get started with the API. ## [](#loading-and-saving)Loading and saving Loading and saving tokenizers is as simple as it is with models. Actually, it’s based on the same two methods: `from_pretrained()` and `save_pretrained()`. These methods will load or save the algorithm used by the tokenizer (a bit like the _architecture_ of the model) as well as its vocabulary (a bit like the _weights_ of the model). Loading the BERT tokenizer trained with the same checkpoint as BERT is done the same way as loading the model, except we use the `BertTokenizer` class: ``` from transformers import BertTokenizer tokenizer = BertTokenizer.from_pretrained("bert-base-cased")``` Similar to `AutoModel`, the `AutoTokenizer` class will grab the proper tokenizer class in the library based on the checkpoint name, and can be used directly with any checkpoint: ``` from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")``` We can now use the tokenizer as shown in the previous section: ``` tokenizer("Using a Transformer network is simple")``` ``` {'input_ids': [101, 7993, 170, 11303, 1200, 2443, 1110, 3014, 102], 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1]}``` Saving a tokenizer is identical to saving a model: ``` tokenizer.save_pretrained("directory_on_my_computer")``` We’ll talk more about `token_type_ids` in [Chapter 3](/course/chapter3), and we’ll explain the `attention_mask` key a little later. First, let’s see how the `input_ids` are generated. To do this, we’ll need to look at the intermediate methods of the tokenizer. ## [](#encoding)Encoding Translating text to numbers is known as _encoding_. Encoding is done in a two-step process: the tokenization, followed by the conversion to input IDs. As we’ve seen, the first step is to split the text into words (or parts of words, punctuation symbols, etc.), usually called _tokens_. There are multiple rules that can govern that process, which is why we need to instantiate the tokenizer using the name of the model, to make sure we use the same rules that were used when the model was pretrained. The second step is to convert those tokens into numbers, so we can build a tensor out of them and feed them to the model. To do this, the tokenizer has a _vocabulary_, which is the part we download when we instantiate it with the `from_pretrained()` method. Again, we need to use the same vocabulary used when the model was pretrained. To get a better understanding of the two steps, we’ll explore them separately. Note that we will use some methods that perform parts of the tokenization pipeline separately to show you the intermediate results of those steps, but in practice, you should call the tokenizer directly on your inputs (as shown in the section 2). ### [](#tokenization)Tokenization The tokenization process is done by the `tokenize()` method of the tokenizer: ``` from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") sequence = "Using a Transformer network is simple" tokens = tokenizer.tokenize(sequence) print(tokens)``` The output of this method is a list of strings, or tokens: ``` ['Using', 'a', 'transform', '##er', 'network', 'is', 'simple']``` This tokenizer is a subword tokenizer: it splits the words until it obtains tokens that can be represented by its vocabulary. That’s the case here with `transformer`, which is split into two tokens: `transform` and `##er`. ### [](#from-tokens-to-input-ids)From tokens to input IDs The conversion to input IDs is handled by the `convert_tokens_to_ids()` tokenizer method: ``` ids = tokenizer.convert_tokens_to_ids(tokens) print(ids)``` ``` [7993, 170, 11303, 1200, 2443, 1110, 3014]``` These outputs, once converted to the appropriate framework tensor, can then be used as inputs to a model as seen earlier in this chapter. ✏️ **Try it out!** Replicate the two last steps (tokenization and conversion to input IDs) on the input sentences we used in section 2 (“I’ve been waiting for a HuggingFace course my whole life.” and “I hate this so much!”). Check that you get the same input IDs we got earlier! ## [](#decoding)Decoding _Decoding_ is going the other way around: from vocabulary indices, we want to get a string. This can be done with the `decode()` method as follows: ``` decoded_string = tokenizer.decode([7993, 170, 11303, 1200, 2443, 1110, 3014]) print(decoded_string)``` ``` 'Using a Transformer network is simple'``` Note that the `decode` method not only converts the indices back to tokens, but also groups together the tokens that were part of the same words to produce a readable sentence. This behavior will be extremely useful when we use models that predict new text (either text generated from a prompt, or for sequence-to-sequence problems like translation or summarization). By now you should understand the atomic operations a tokenizer can handle: tokenization, conversion to IDs, and converting IDs back to a string. However, we’ve just scraped the tip of the iceberg. In the following section, we’ll take our approach to its limits and take a look at how to overcome them.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Tokenizers - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter2/4"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Tokenizers - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;tokenizers&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;word-based&quot;,&quot;title&quot;:&quot;Word-based&quot;},{&quot;local&quot;:&quot;character-based&quot;,&quot;title&quot;:&quot;Character-based&quot;},{&quot;local&quot;:&quot;subword-tokenization&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;and-more&quot;,&quot;title&quot;:&quot;And more!&quot;}],&quot;title&quot;:&quot;Subword tokenization&quot;},{&quot;local&quot;:&quot;loading-and-saving&quot;,&quot;title&quot;:&quot;Loading and saving&quot;},{&quot;local&quot;:&quot;encoding&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;tokenization&quot;,&quot;title&quot;:&quot;Tokenization&quot;},{&quot;local&quot;:&quot;from-tokens-to-input-ids&quot;,&quot;title&quot;:&quot;From tokens to input IDs&quot;}],&quot;title&quot;:&quot;Encoding&quot;},{&quot;local&quot;:&quot;decoding&quot;,&quot;title&quot;:&quot;Decoding&quot;}],&quot;title&quot;:&quot;Tokenizers&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter2/4&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Tokenizers&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"> <div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation </p> <div class="flex items-center"><p class="font-semibold">Tokenizers</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "> <button class=" " type="button"> <h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> </button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en" selected="">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"> <button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"> <svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> </a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->0. Setup<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->1. Transformer models<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->2. Using 🤗 Transformers<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/1?fw=pt"><!-- HTML_TAG_START -->Introduction<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/2?fw=pt"><!-- HTML_TAG_START -->Behind the pipeline<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/3?fw=pt"><!-- HTML_TAG_START -->Models<!-- HTML_TAG_END --> </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter2/4?fw=pt"><!-- HTML_TAG_START -->Tokenizers<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/5?fw=pt"><!-- HTML_TAG_START -->Handling multiple sequences<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/6?fw=pt"><!-- HTML_TAG_START -->Putting it all together<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/7?fw=pt"><!-- HTML_TAG_START -->Basic usage completed!<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/8?fw=pt"><!-- HTML_TAG_START -->End-of-chapter quiz<!-- HTML_TAG_END --> </a> </div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->3. Fine-tuning a pretrained model<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->4. Sharing models and tokenizers<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->5. The 🤗 Datasets library<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->6. The 🤗 Tokenizers library<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->7. Main NLP tasks<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->8. How to ask for help<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->9. Building and sharing demos<!-- HTML_TAG_END --></span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->Course Events<!-- HTML_TAG_END --></span> </span></span> </div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="tokenizers" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tokenizers"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Tokenizers</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-2-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter2/section4_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter2/section4_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/VFp38yj8h3A" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Tokenizers are one of the core components of the NLP pipeline. They serve one purpose: to translate text into data that can be processed by the model. Models can only process numbers, so tokenizers need to convert our text inputs to numerical data. In this section, we’ll explore exactly what happens in the tokenization pipeline.</p> <p>In NLP tasks, the data that is generally processed is raw text. Here’s an example of such text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment">Jim Henson was a puppeteer</span></pre></div> <p>However, models can only process numbers, so we need to find a way to convert the raw text to numbers. That’s what the tokenizers do, and there are a lot of ways to go about this. The goal is to find the most meaningful representation — that is, the one that makes the most sense to the model — and, if possible, the smallest representation.</p> <p>Let’s take a look at some examples of tokenization algorithms, and try to answer some of the questions you may have about tokenization.</p> <h2 class="relative group"><a id="word-based" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#word-based"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Word-based</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/nhJxYji1aho" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The first type of tokenizer that comes to mind is <em>word-based</em>. It’s generally very easy to set up and use with only a few rules, and it often yields decent results. For example, in the image below, the goal is to split the raw text into words and find a numerical representation for each of them:</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/word_based_tokenization.svg" alt="An example of word-based tokenization."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/word_based_tokenization-dark.svg" alt="An example of word-based tokenization."></div> <p>There are different ways to split the text. For example, we could use whitespace to tokenize the text into words by applying Python’s <code>split()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_text = <span class="hljs-string">"Jim Henson was a puppeteer"</span>.split() <span class="hljs-built_in">print</span>(tokenized_text)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'Jim'</span>, <span class="hljs-string">'Henson'</span>, <span class="hljs-string">'was'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'puppeteer'</span>]</pre></div> <p>There are also variations of word tokenizers that have extra rules for punctuation. With this kind of tokenizer, we can end up with some pretty large “vocabularies,” where a vocabulary is defined by the total number of independent tokens that we have in our corpus.</p> <p>Each word gets assigned an ID, starting from 0 and going up to the size of the vocabulary. The model uses these IDs to identify each word.</p> <p>If we want to completely cover a language with a word-based tokenizer, we’ll need to have an identifier for each word in the language, which will generate a huge amount of tokens. For example, there are over 500,000 words in the English language, so to build a map from each word to an input ID we’d need to keep track of that many IDs. Furthermore, words like “dog” are represented differently from words like “dogs”, and the model will initially have no way of knowing that “dog” and “dogs” are similar: it will identify the two words as unrelated. The same applies to other similar words, like “run” and “running”, which the model will not see as being similar initially.</p> <p>Finally, we need a custom token to represent words that are not in our vocabulary. This is known as the “unknown” token, often represented as ”[UNK]” or ”<unk>”. It’s generally a bad sign if you see that the tokenizer is producing a lot of these tokens, as it wasn’t able to retrieve a sensible representation of a word and you’re losing information along the way. The goal when crafting the vocabulary is to do it in such a way that the tokenizer tokenizes as few words as possible into the unknown token.</unk></p> <p>One way to reduce the amount of unknown tokens is to go one level deeper, using a <em>character-based</em> tokenizer.</p> <h2 class="relative group"><a id="character-based" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#character-based"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Character-based</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/ssLq_EK2jLE" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Character-based tokenizers split the text into characters, rather than words. This has two primary benefits:</p> <ul><li>The vocabulary is much smaller.</li> <li>There are much fewer out-of-vocabulary (unknown) tokens, since every word can be built from characters.</li></ul> <p>But here too some questions arise concerning spaces and punctuation:</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/character_based_tokenization.svg" alt="An example of character-based tokenization."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/character_based_tokenization-dark.svg" alt="An example of character-based tokenization."></div> <p>This approach isn’t perfect either. Since the representation is now based on characters rather than words, one could argue that, intuitively, it’s less meaningful: each character doesn’t mean a lot on its own, whereas that is the case with words. However, this again differs according to the language; in Chinese, for example, each character carries more information than a character in a Latin language.</p> <p>Another thing to consider is that we’ll end up with a very large amount of tokens to be processed by our model: whereas a word would only be a single token with a word-based tokenizer, it can easily turn into 10 or more tokens when converted into characters.</p> <p>To get the best of both worlds, we can use a third technique that combines the two approaches: <em>subword tokenization</em>.</p> <h2 class="relative group"><a id="subword-tokenization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#subword-tokenization"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Subword tokenization</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/zHvTiHr506c" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Subword tokenization algorithms rely on the principle that frequently used words should not be split into smaller subwords, but rare words should be decomposed into meaningful subwords.</p> <p>For instance, “annoyingly” might be considered a rare word and could be decomposed into “annoying” and “ly”. These are both likely to appear more frequently as standalone subwords, while at the same time the meaning of “annoyingly” is kept by the composite meaning of “annoying” and “ly”.</p> <p>Here is an example showing how a subword tokenization algorithm would tokenize the sequence “Let’s do tokenization!“:</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/bpe_subword.svg" alt="A subword tokenization algorithm."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter2/bpe_subword-dark.svg" alt="A subword tokenization algorithm."></div> <p>These subwords end up providing a lot of semantic meaning: for instance, in the example above “tokenization” was split into “token” and “ization”, two tokens that have a semantic meaning while being space-efficient (only two tokens are needed to represent a long word). This allows us to have relatively good coverage with small vocabularies, and close to no unknown tokens.</p> <p>This approach is especially useful in agglutinative languages such as Turkish, where you can form (almost) arbitrarily long complex words by stringing together subwords.</p> <h3 class="relative group"><a id="and-more" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#and-more"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>And more!</span></h3> <p>Unsurprisingly, there are many more techniques out there. To name a few:</p> <ul><li>Byte-level BPE, as used in GPT-2</li> <li>WordPiece, as used in BERT</li> <li>SentencePiece or Unigram, as used in several multilingual models</li></ul> <p>You should now have sufficient knowledge of how tokenizers work to get started with the API.</p> <h2 class="relative group"><a id="loading-and-saving" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-and-saving"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Loading and saving</span></h2> <p>Loading and saving tokenizers is as simple as it is with models. Actually, it’s based on the same two methods: <code>from_pretrained()</code> and <code>save_pretrained()</code>. These methods will load or save the algorithm used by the tokenizer (a bit like the <em>architecture</em> of the model) as well as its vocabulary (a bit like the <em>weights</em> of the model).</p> <p>Loading the BERT tokenizer trained with the same checkpoint as BERT is done the same way as loading the model, except we use the <code>BertTokenizer</code> class:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> BertTokenizer tokenizer = BertTokenizer.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>)</pre></div> <p>Similar to <code>AutoModel</code>, the <code>AutoTokenizer</code> class will grab the proper tokenizer class in the library based on the checkpoint name, and can be used directly with any checkpoint:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>)</pre></div> <p>We can now use the tokenizer as shown in the previous section:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer(<span class="hljs-string">"Using a Transformer network is simple"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'input_ids'</span>: [<span class="hljs-number">101</span>, <span class="hljs-number">7993</span>, <span class="hljs-number">170</span>, <span class="hljs-number">11303</span>, <span class="hljs-number">1200</span>, <span class="hljs-number">2443</span>, <span class="hljs-number">1110</span>, <span class="hljs-number">3014</span>, <span class="hljs-number">102</span>], <span class="hljs-string">'token_type_ids'</span>: [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], <span class="hljs-string">'attention_mask'</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]}</pre></div> <p>Saving a tokenizer is identical to saving a model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.save_pretrained(<span class="hljs-string">"directory_on_my_computer"</span>)</pre></div> <p>We’ll talk more about <code>token_type_ids</code> in <a href="/course/chapter3">Chapter 3</a>, and we’ll explain the <code>attention_mask</code> key a little later. First, let’s see how the <code>input_ids</code> are generated. To do this, we’ll need to look at the intermediate methods of the tokenizer.</p> <h2 class="relative group"><a id="encoding" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#encoding"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Encoding</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/Yffk5aydLzg" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Translating text to numbers is known as <em>encoding</em>. Encoding is done in a two-step process: the tokenization, followed by the conversion to input IDs.</p> <p>As we’ve seen, the first step is to split the text into words (or parts of words, punctuation symbols, etc.), usually called <em>tokens</em>. There are multiple rules that can govern that process, which is why we need to instantiate the tokenizer using the name of the model, to make sure we use the same rules that were used when the model was pretrained.</p> <p>The second step is to convert those tokens into numbers, so we can build a tensor out of them and feed them to the model. To do this, the tokenizer has a <em>vocabulary</em>, which is the part we download when we instantiate it with the <code>from_pretrained()</code> method. Again, we need to use the same vocabulary used when the model was pretrained.</p> <p>To get a better understanding of the two steps, we’ll explore them separately. Note that we will use some methods that perform parts of the tokenization pipeline separately to show you the intermediate results of those steps, but in practice, you should call the tokenizer directly on your inputs (as shown in the section 2).</p> <h3 class="relative group"><a id="tokenization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tokenization"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Tokenization</span></h3> <p>The tokenization process is done by the <code>tokenize()</code> method of the tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>) sequence = <span class="hljs-string">"Using a Transformer network is simple"</span> tokens = tokenizer.tokenize(sequence) <span class="hljs-built_in">print</span>(tokens)</pre></div> <p>The output of this method is a list of strings, or tokens:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'Using'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'transform'</span>, <span class="hljs-string">'##er'</span>, <span class="hljs-string">'network'</span>, <span class="hljs-string">'is'</span>, <span class="hljs-string">'simple'</span>]</pre></div> <p>This tokenizer is a subword tokenizer: it splits the words until it obtains tokens that can be represented by its vocabulary. That’s the case here with <code>transformer</code>, which is split into two tokens: <code>transform</code> and <code>##er</code>.</p> <h3 class="relative group"><a id="from-tokens-to-input-ids" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#from-tokens-to-input-ids"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>From tokens to input IDs</span></h3> <p>The conversion to input IDs is handled by the <code>convert_tokens_to_ids()</code> tokenizer method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>ids = tokenizer.convert_tokens_to_ids(tokens) <span class="hljs-built_in">print</span>(ids)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">7993</span>, <span class="hljs-number">170</span>, <span class="hljs-number">11303</span>, <span class="hljs-number">1200</span>, <span class="hljs-number">2443</span>, <span class="hljs-number">1110</span>, <span class="hljs-number">3014</span>]</pre></div> <p>These outputs, once converted to the appropriate framework tensor, can then be used as inputs to a model as seen earlier in this chapter.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Replicate the two last steps (tokenization and conversion to input IDs) on the input sentences we used in section 2 (“I’ve been waiting for a HuggingFace course my whole life.” and “I hate this so much!”). Check that you get the same input IDs we got earlier!</p></div> <h2 class="relative group"><a id="decoding" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#decoding"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Decoding</span></h2> <p><em>Decoding</em> is going the other way around: from vocabulary indices, we want to get a string. This can be done with the <code>decode()</code> method as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>decoded_string = tokenizer.decode([<span class="hljs-number">7993</span>, <span class="hljs-number">170</span>, <span class="hljs-number">11303</span>, <span class="hljs-number">1200</span>, <span class="hljs-number">2443</span>, <span class="hljs-number">1110</span>, <span class="hljs-number">3014</span>]) <span class="hljs-built_in">print</span>(decoded_string)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'Using a Transformer network is simple'</span></pre></div> <p>Note that the <code>decode</code> method not only converts the indices back to tokens, but also groups together the tokens that were part of the same words to produce a readable sentence. This behavior will be extremely useful when we use models that predict new text (either text generated from a prompt, or for sequence-to-sequence problems like translation or summarization).</p> <p>By now you should understand the atomic operations a tokenizer can handle: tokenization, conversion to IDs, and converting IDs back to a string. However, we’ve just scraped the tip of the iceberg. In the following section, we’ll take our approach to its limits and take a look at how to overcome them.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter2/3?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Models</a> <a href="/learn/nlp-course/chapter2/5?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Handling multiple sequences<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;tokenizers&quot;,&quot;url&quot;:&quot;#tokenizers&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Word-based&quot;,&quot;id&quot;:&quot;word-based&quot;,&quot;url&quot;:&quot;#word-based&quot;},{&quot;title&quot;:&quot;Character-based&quot;,&quot;id&quot;:&quot;character-based&quot;,&quot;url&quot;:&quot;#character-based&quot;},{&quot;title&quot;:&quot;Subword tokenization&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;subword-tokenization&quot;,&quot;url&quot;:&quot;#subword-tokenization&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;And more!&quot;,&quot;id&quot;:&quot;and-more&quot;,&quot;url&quot;:&quot;#and-more&quot;}]},{&quot;title&quot;:&quot;Loading and saving&quot;,&quot;id&quot;:&quot;loading-and-saving&quot;,&quot;url&quot;:&quot;#loading-and-saving&quot;},{&quot;title&quot;:&quot;Encoding&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;encoding&quot;,&quot;url&quot;:&quot;#encoding&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Tokenization&quot;,&quot;id&quot;:&quot;tokenization&quot;,&quot;url&quot;:&quot;#tokenization&quot;},{&quot;title&quot;:&quot;From tokens to input IDs&quot;,&quot;id&quot;:&quot;from-tokens-to-input-ids&quot;,&quot;url&quot;:&quot;#from-tokens-to-input-ids&quot;}]},{&quot;title&quot;:&quot;Decoding&quot;,&quot;id&quot;:&quot;decoding&quot;,&quot;url&quot;:&quot;#decoding&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#tokenizers" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tokenizers"><wbr>Tokenizers</a> <a href="#word-based" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-word-based"><wbr>Word-based</a> <a href="#character-based" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-character-based"><wbr>Character-based</a> <a href="#subword-tokenization" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-subword-tokenization"><wbr>Subword tokenization</a> <a href="#and-more" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-and-more"><wbr>And more!</a> <a href="#loading-and-saving" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-loading-and-saving"><wbr>Loading and saving</a> <a href="#encoding" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-encoding"><wbr>Encoding</a> <a href="#tokenization" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tokenization"><wbr>Tokenization</a> <a href="#from-tokens-to-input-ids" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-from-tokens-to-input-ids"><wbr>From tokens to input I<wbr>Ds</a> <a href="#decoding" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-decoding"><wbr>Decoding</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter2/4" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter2/4"); } </script> <iframe name="__privateStripeMetricsController8970" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter2%2F4%3Ffw%3Dpt&amp;title=Tokenizers%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:09.560Z
Handling multiple sequences - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter2/5?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#handling-multiple-sequences)Handling multiple sequences [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-2-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter2/section5_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter2/section5_pt.ipynb) In the previous section, we explored the simplest of use cases: doing inference on a single sequence of a small length. However, some questions emerge already: - How do we handle multiple sequences? - How do we handle multiple sequences _of different lengths_? - Are vocabulary indices the only inputs that allow a model to work well? - Is there such a thing as too long a sequence? Let’s see what kinds of problems these questions pose, and how we can solve them using the 🤗 Transformers API. ## [](#models-expect-a-batch-of-inputs)Models expect a batch of inputs In the previous exercise you saw how sequences get translated into lists of numbers. Let’s convert this list of numbers to a tensor and send it to the model: ``` import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequence = "I've been waiting for a HuggingFace course my whole life." tokens = tokenizer.tokenize(sequence) ids = tokenizer.convert_tokens_to_ids(tokens) input_ids = torch.tensor(ids) model(input_ids)``` ``` IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)``` Oh no! Why did this fail? “We followed the steps from the pipeline in section 2. The problem is that we sent a single sequence to the model, whereas 🤗 Transformers models expect multiple sentences by default. Here we tried to do everything the tokenizer did behind the scenes when we applied it to a `sequence`. But if you look closely, you’ll see that the tokenizer didn’t just convert the list of input IDs into a tensor, it added a dimension on top of it: ``` tokenized_inputs = tokenizer(sequence, return_tensors="pt") print(tokenized_inputs["input_ids"])``` ``` tensor([[ 101, 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012, 102]])``` Let’s try again and add a new dimension: ``` import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequence = "I've been waiting for a HuggingFace course my whole life." tokens = tokenizer.tokenize(sequence) ids = tokenizer.convert_tokens_to_ids(tokens) input_ids = torch.tensor([ids]) print("Input IDs:", input_ids) output = model(input_ids) print("Logits:", output.logits)``` We print the input IDs as well as the resulting logits — here’s the output: ``` Input IDs: [[ 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012]] Logits: [[-2.7276, 2.8789]]``` _Batching_ is the act of sending multiple sentences through the model, all at once. If you only have one sentence, you can just build a batch with a single sequence: This is a batch of two identical sequences! ✏️ **Try it out!** Convert this `batched_ids` list into a tensor and pass it through your model. Check that you obtain the same logits as before (but twice)! Batching allows the model to work when you feed it multiple sentences. Using multiple sequences is just as simple as building a batch with a single sequence. There’s a second issue, though. When you’re trying to batch together two (or more) sentences, they might be of different lengths. If you’ve ever worked with tensors before, you know that they need to be of rectangular shape, so you won’t be able to convert the list of input IDs into a tensor directly. To work around this problem, we usually _pad_ the inputs. ## [](#padding-the-inputs)Padding the inputs The following list of lists cannot be converted to a tensor: ``` batched_ids = [ [200, 200, 200], [200, 200] ]``` In order to work around this, we’ll use _padding_ to make our tensors have a rectangular shape. Padding makes sure all our sentences have the same length by adding a special word called the _padding token_ to the sentences with fewer values. For example, if you have 10 sentences with 10 words and 1 sentence with 20 words, padding will ensure all the sentences have 20 words. In our example, the resulting tensor looks like this: ``` padding_id = 100 batched_ids = [ [200, 200, 200], [200, 200, padding_id], ]``` The padding token ID can be found in `tokenizer.pad_token_id`. Let’s use it and send our two sentences through the model individually and batched together: ``` model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequence1_ids = [[200, 200, 200]] sequence2_ids = [[200, 200]] batched_ids = [ [200, 200, 200], [200, 200, tokenizer.pad_token_id], ] print(model(torch.tensor(sequence1_ids)).logits) print(model(torch.tensor(sequence2_ids)).logits) print(model(torch.tensor(batched_ids)).logits)``` ``` tensor([[ 1.5694, -1.3895]], grad_fn=<AddmmBackward>) tensor([[ 0.5803, -0.4125]], grad_fn=<AddmmBackward>) tensor([[ 1.5694, -1.3895], [ 1.3373, -1.2163]], grad_fn=<AddmmBackward>)``` There’s something wrong with the logits in our batched predictions: the second row should be the same as the logits for the second sentence, but we’ve got completely different values! This is because the key feature of Transformer models is attention layers that _contextualize_ each token. These will take into account the padding tokens since they attend to all of the tokens of a sequence. To get the same result when passing individual sentences of different lengths through the model or when passing a batch with the same sentences and padding applied, we need to tell those attention layers to ignore the padding tokens. This is done by using an attention mask. ## [](#attention-masks)Attention masks _Attention masks_ are tensors with the exact same shape as the input IDs tensor, filled with 0s and 1s: 1s indicate the corresponding tokens should be attended to, and 0s indicate the corresponding tokens should not be attended to (i.e., they should be ignored by the attention layers of the model). Let’s complete the previous example with an attention mask: ``` batched_ids = [ [200, 200, 200], [200, 200, tokenizer.pad_token_id], ] attention_mask = [ [1, 1, 1], [1, 1, 0], ] outputs = model(torch.tensor(batched_ids), attention_mask=torch.tensor(attention_mask)) print(outputs.logits)``` ``` tensor([[ 1.5694, -1.3895], [ 0.5803, -0.4125]], grad_fn=<AddmmBackward>)``` Now we get the same logits for the second sentence in the batch. Notice how the last value of the second sequence is a padding ID, which is a 0 value in the attention mask. ✏️ **Try it out!** Apply the tokenization manually on the two sentences used in section 2 (“I’ve been waiting for a HuggingFace course my whole life.” and “I hate this so much!”). Pass them through the model and check that you get the same logits as in section 2. Now batch them together using the padding token, then create the proper attention mask. Check that you obtain the same results when going through the model! ## [](#longer-sequences)Longer sequences With Transformer models, there is a limit to the lengths of the sequences we can pass the models. Most models handle sequences of up to 512 or 1024 tokens, and will crash when asked to process longer sequences. There are two solutions to this problem: - Use a model with a longer supported sequence length. - Truncate your sequences. Models have different supported sequence lengths, and some specialize in handling very long sequences. [Longformer](https://huggingface.co/transformers/model_doc/longformer.html) is one example, and another is [LED](https://huggingface.co/transformers/model_doc/led.html). If you’re working on a task that requires very long sequences, we recommend you take a look at those models. Otherwise, we recommend you truncate your sequences by specifying the `max_sequence_length` parameter: ``` sequence = sequence[:max_sequence_length]```
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Handling multiple sequences - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter2/5"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Handling multiple sequences - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;handling-multiple-sequences&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;models-expect-a-batch-of-inputs&quot;,&quot;title&quot;:&quot;Models expect a batch of inputs&quot;},{&quot;local&quot;:&quot;padding-the-inputs&quot;,&quot;title&quot;:&quot;Padding the inputs&quot;},{&quot;local&quot;:&quot;attention-masks&quot;,&quot;title&quot;:&quot;Attention masks&quot;},{&quot;local&quot;:&quot;longer-sequences&quot;,&quot;title&quot;:&quot;Longer sequences&quot;}],&quot;title&quot;:&quot;Handling multiple sequences&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter2/5&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Handling multiple sequences&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Handling multiple sequences</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/2?fw=pt">Behind the pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/3?fw=pt">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/4?fw=pt">Tokenizers </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter2/5?fw=pt">Handling multiple sequences </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/6?fw=pt">Putting it all together </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/7?fw=pt">Basic usage completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="handling-multiple-sequences" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#handling-multiple-sequences"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Handling multiple sequences</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-2-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter2/section5_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter2/section5_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/M6adb1j2jPI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>In the previous section, we explored the simplest of use cases: doing inference on a single sequence of a small length. However, some questions emerge already:</p> <ul><li>How do we handle multiple sequences?</li> <li>How do we handle multiple sequences <em>of different lengths</em>?</li> <li>Are vocabulary indices the only inputs that allow a model to work well?</li> <li>Is there such a thing as too long a sequence?</li></ul> <p>Let’s see what kinds of problems these questions pose, and how we can solve them using the 🤗 Transformers API.</p> <h2 class="relative group"><a id="models-expect-a-batch-of-inputs" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#models-expect-a-batch-of-inputs"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Models expect a batch of inputs</span></h2> <p>In the previous exercise you saw how sequences get translated into lists of numbers. Let’s convert this list of numbers to a tensor and send it to the model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForSequenceClassification checkpoint = <span class="hljs-string">"distilbert-base-uncased-finetuned-sst-2-english"</span> tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequence = <span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span> tokens = tokenizer.tokenize(sequence) ids = tokenizer.convert_tokens_to_ids(tokens) input_ids = torch.tensor(ids) <span class="hljs-comment"># This line will fail.</span> model(input_ids)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>IndexError: Dimension out of <span class="hljs-built_in">range</span> (expected to be <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span> of [-<span class="hljs-number">1</span>, <span class="hljs-number">0</span>], but got <span class="hljs-number">1</span>)</pre></div> <p>Oh no! Why did this fail? “We followed the steps from the pipeline in section 2.</p> <p>The problem is that we sent a single sequence to the model, whereas 🤗 Transformers models expect multiple sentences by default. Here we tried to do everything the tokenizer did behind the scenes when we applied it to a <code>sequence</code>. But if you look closely, you’ll see that the tokenizer didn’t just convert the list of input IDs into a tensor, it added a dimension on top of it:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_inputs = tokenizer(sequence, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-built_in">print</span>(tokenized_inputs[<span class="hljs-string">"input_ids"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tensor([[ <span class="hljs-number">101</span>, <span class="hljs-number">1045</span>, <span class="hljs-number">1005</span>, <span class="hljs-number">2310</span>, <span class="hljs-number">2042</span>, <span class="hljs-number">3403</span>, <span class="hljs-number">2005</span>, <span class="hljs-number">1037</span>, <span class="hljs-number">17662</span>, <span class="hljs-number">12172</span>, <span class="hljs-number">2607</span>, <span class="hljs-number">2026</span>, <span class="hljs-number">2878</span>, <span class="hljs-number">2166</span>, <span class="hljs-number">1012</span>, <span class="hljs-number">102</span>]])</pre></div> <p>Let’s try again and add a new dimension:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForSequenceClassification checkpoint = <span class="hljs-string">"distilbert-base-uncased-finetuned-sst-2-english"</span> tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequence = <span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span> tokens = tokenizer.tokenize(sequence) ids = tokenizer.convert_tokens_to_ids(tokens) input_ids = torch.tensor([ids]) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Input IDs:"</span>, input_ids) output = model(input_ids) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Logits:"</span>, output.logits)</pre></div> <p>We print the input IDs as well as the resulting logits — here’s the output:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Input IDs: [[ <span class="hljs-number">1045</span>, <span class="hljs-number">1005</span>, <span class="hljs-number">2310</span>, <span class="hljs-number">2042</span>, <span class="hljs-number">3403</span>, <span class="hljs-number">2005</span>, <span class="hljs-number">1037</span>, <span class="hljs-number">17662</span>, <span class="hljs-number">12172</span>, <span class="hljs-number">2607</span>, <span class="hljs-number">2026</span>, <span class="hljs-number">2878</span>, <span class="hljs-number">2166</span>, <span class="hljs-number">1012</span>]] Logits: [[-<span class="hljs-number">2.7276</span>, <span class="hljs-number">2.8789</span>]]</pre></div> <p><em>Batching</em> is the act of sending multiple sentences through the model, all at once. If you only have one sentence, you can just build a batch with a single sequence:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-attr">batched_ids</span> = [ids, ids]</pre></div> <p>This is a batch of two identical sequences!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Convert this <code>batched_ids</code> list into a tensor and pass it through your model. Check that you obtain the same logits as before (but twice)!</p></div> <p>Batching allows the model to work when you feed it multiple sentences. Using multiple sequences is just as simple as building a batch with a single sequence. There’s a second issue, though. When you’re trying to batch together two (or more) sentences, they might be of different lengths. If you’ve ever worked with tensors before, you know that they need to be of rectangular shape, so you won’t be able to convert the list of input IDs into a tensor directly. To work around this problem, we usually <em>pad</em> the inputs.</p> <h2 class="relative group"><a id="padding-the-inputs" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#padding-the-inputs"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Padding the inputs</span></h2> <p>The following list of lists cannot be converted to a tensor:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>batched_ids = [ [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>], [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>] ]</pre></div> <p>In order to work around this, we’ll use <em>padding</em> to make our tensors have a rectangular shape. Padding makes sure all our sentences have the same length by adding a special word called the <em>padding token</em> to the sentences with fewer values. For example, if you have 10 sentences with 10 words and 1 sentence with 20 words, padding will ensure all the sentences have 20 words. In our example, the resulting tensor looks like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>padding_id = <span class="hljs-number">100</span> batched_ids = [ [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>], [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, padding_id], ]</pre></div> <p>The padding token ID can be found in <code>tokenizer.pad_token_id</code>. Let’s use it and send our two sentences through the model individually and batched together:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequence1_ids = [[<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>]] sequence2_ids = [[<span class="hljs-number">200</span>, <span class="hljs-number">200</span>]] batched_ids = [ [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>], [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, tokenizer.pad_token_id], ] <span class="hljs-built_in">print</span>(model(torch.tensor(sequence1_ids)).logits) <span class="hljs-built_in">print</span>(model(torch.tensor(sequence2_ids)).logits) <span class="hljs-built_in">print</span>(model(torch.tensor(batched_ids)).logits)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tensor([[ <span class="hljs-number">1.5694</span>, -<span class="hljs-number">1.3895</span>]], grad_fn=&lt;AddmmBackward&gt;) tensor([[ <span class="hljs-number">0.5803</span>, -<span class="hljs-number">0.4125</span>]], grad_fn=&lt;AddmmBackward&gt;) tensor([[ <span class="hljs-number">1.5694</span>, -<span class="hljs-number">1.3895</span>], [ <span class="hljs-number">1.3373</span>, -<span class="hljs-number">1.2163</span>]], grad_fn=&lt;AddmmBackward&gt;)</pre></div> <p>There’s something wrong with the logits in our batched predictions: the second row should be the same as the logits for the second sentence, but we’ve got completely different values!</p> <p>This is because the key feature of Transformer models is attention layers that <em>contextualize</em> each token. These will take into account the padding tokens since they attend to all of the tokens of a sequence. To get the same result when passing individual sentences of different lengths through the model or when passing a batch with the same sentences and padding applied, we need to tell those attention layers to ignore the padding tokens. This is done by using an attention mask.</p> <h2 class="relative group"><a id="attention-masks" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#attention-masks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Attention masks</span></h2> <p><em>Attention masks</em> are tensors with the exact same shape as the input IDs tensor, filled with 0s and 1s: 1s indicate the corresponding tokens should be attended to, and 0s indicate the corresponding tokens should not be attended to (i.e., they should be ignored by the attention layers of the model).</p> <p>Let’s complete the previous example with an attention mask:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>batched_ids = [ [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>], [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, tokenizer.pad_token_id], ] attention_mask = [ [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>], ] outputs = model(torch.tensor(batched_ids), attention_mask=torch.tensor(attention_mask)) <span class="hljs-built_in">print</span>(outputs.logits)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tensor([[ <span class="hljs-number">1.5694</span>, -<span class="hljs-number">1.3895</span>], [ <span class="hljs-number">0.5803</span>, -<span class="hljs-number">0.4125</span>]], grad_fn=&lt;AddmmBackward&gt;)</pre></div> <p>Now we get the same logits for the second sentence in the batch.</p> <p>Notice how the last value of the second sequence is a padding ID, which is a 0 value in the attention mask.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Apply the tokenization manually on the two sentences used in section 2 (“I’ve been waiting for a HuggingFace course my whole life.” and “I hate this so much!”). Pass them through the model and check that you get the same logits as in section 2. Now batch them together using the padding token, then create the proper attention mask. Check that you obtain the same results when going through the model!</p></div> <h2 class="relative group"><a id="longer-sequences" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#longer-sequences"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Longer sequences</span></h2> <p>With Transformer models, there is a limit to the lengths of the sequences we can pass the models. Most models handle sequences of up to 512 or 1024 tokens, and will crash when asked to process longer sequences. There are two solutions to this problem:</p> <ul><li>Use a model with a longer supported sequence length.</li> <li>Truncate your sequences.</li></ul> <p>Models have different supported sequence lengths, and some specialize in handling very long sequences. <a href="https://huggingface.co/transformers/model_doc/longformer.html" rel="nofollow">Longformer</a> is one example, and another is <a href="https://huggingface.co/transformers/model_doc/led.html" rel="nofollow">LED</a>. If you’re working on a task that requires very long sequences, we recommend you take a look at those models.</p> <p>Otherwise, we recommend you truncate your sequences by specifying the <code>max_sequence_length</code> parameter:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sequence = sequence[:max_sequence_length]</pre></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter2/4?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Tokenizers</a> <a href="/learn/nlp-course/chapter2/6?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Putting it all together<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;handling-multiple-sequences&quot;,&quot;url&quot;:&quot;#handling-multiple-sequences&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Models expect a batch of inputs&quot;,&quot;id&quot;:&quot;models-expect-a-batch-of-inputs&quot;,&quot;url&quot;:&quot;#models-expect-a-batch-of-inputs&quot;},{&quot;title&quot;:&quot;Padding the inputs&quot;,&quot;id&quot;:&quot;padding-the-inputs&quot;,&quot;url&quot;:&quot;#padding-the-inputs&quot;},{&quot;title&quot;:&quot;Attention masks&quot;,&quot;id&quot;:&quot;attention-masks&quot;,&quot;url&quot;:&quot;#attention-masks&quot;},{&quot;title&quot;:&quot;Longer sequences&quot;,&quot;id&quot;:&quot;longer-sequences&quot;,&quot;url&quot;:&quot;#longer-sequences&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#handling-multiple-sequences" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-handling-multiple-sequences"><wbr>Handling multiple sequences</a> <a href="#models-expect-a-batch-of-inputs" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-models-expect-a-batch-of-inputs"><wbr>Models expect a batch of inputs</a> <a href="#padding-the-inputs" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-padding-the-inputs"><wbr>Padding the inputs</a> <a href="#attention-masks" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-attention-masks"><wbr>Attention masks</a> <a href="#longer-sequences" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-longer-sequences"><wbr>Longer sequences</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter2/5" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter2/5"); } </script> <iframe name="__privateStripeMetricsController8290" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter2%2F5%3Ffw%3Dpt&amp;title=Handling%20multiple%20sequences%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:09.652Z
Putting it all together - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter2/6?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#putting-it-all-together)Putting it all together [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-2-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter2/section6_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter2/section6_pt.ipynb) In the last few sections, we’ve been trying our best to do most of the work by hand. We’ve explored how tokenizers work and looked at tokenization, conversion to input IDs, padding, truncation, and attention masks. However, as we saw in section 2, the 🤗 Transformers API can handle all of this for us with a high-level function that we’ll dive into here. When you call your `tokenizer` directly on the sentence, you get back inputs that are ready to pass through your model: ``` from transformers import AutoTokenizer checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(checkpoint) sequence = "I've been waiting for a HuggingFace course my whole life." model_inputs = tokenizer(sequence)``` Here, the `model_inputs` variable contains everything that’s necessary for a model to operate well. For DistilBERT, that includes the input IDs as well as the attention mask. Other models that accept additional inputs will also have those output by the `tokenizer` object. As we’ll see in some examples below, this method is very powerful. First, it can tokenize a single sequence: ``` sequence = "I've been waiting for a HuggingFace course my whole life." model_inputs = tokenizer(sequence)``` It also handles multiple sequences at a time, with no change in the API: ``` sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] model_inputs = tokenizer(sequences)``` It can pad according to several objectives: ``` model_inputs = tokenizer(sequences, padding="longest") model_inputs = tokenizer(sequences, padding="max_length") model_inputs = tokenizer(sequences, padding="max_length", max_length=8)``` It can also truncate sequences: ``` sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] model_inputs = tokenizer(sequences, truncation=True) model_inputs = tokenizer(sequences, max_length=8, truncation=True)``` The `tokenizer` object can handle the conversion to specific framework tensors, which can then be directly sent to the model. For example, in the following code sample we are prompting the tokenizer to return tensors from the different frameworks — `"pt"` returns PyTorch tensors, `"tf"` returns TensorFlow tensors, and `"np"` returns NumPy arrays: ``` sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] model_inputs = tokenizer(sequences, padding=True, return_tensors="pt") model_inputs = tokenizer(sequences, padding=True, return_tensors="tf") model_inputs = tokenizer(sequences, padding=True, return_tensors="np")``` ## [](#special-tokens)Special tokens If we take a look at the input IDs returned by the tokenizer, we will see they are a tiny bit different from what we had earlier: ``` sequence = "I've been waiting for a HuggingFace course my whole life." model_inputs = tokenizer(sequence) print(model_inputs["input_ids"]) tokens = tokenizer.tokenize(sequence) ids = tokenizer.convert_tokens_to_ids(tokens) print(ids)``` ``` [101, 1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012, 102] [1045, 1005, 2310, 2042, 3403, 2005, 1037, 17662, 12172, 2607, 2026, 2878, 2166, 1012]``` One token ID was added at the beginning, and one at the end. Let’s decode the two sequences of IDs above to see what this is about: ``` print(tokenizer.decode(model_inputs["input_ids"])) print(tokenizer.decode(ids))``` ``` "[CLS] i've been waiting for a huggingface course my whole life. [SEP]" "i've been waiting for a huggingface course my whole life."``` The tokenizer added the special word `[CLS]` at the beginning and the special word `[SEP]` at the end. This is because the model was pretrained with those, so to get the same results for inference we need to add them as well. Note that some models don’t add special words, or add different ones; models may also add these special words only at the beginning, or only at the end. In any case, the tokenizer knows which ones are expected and will deal with this for you. ## [](#wrapping-up-from-tokenizer-to-model)Wrapping up: From tokenizer to model Now that we’ve seen all the individual steps the `tokenizer` object uses when applied on texts, let’s see one final time how it can handle multiple sequences (padding!), very long sequences (truncation!), and multiple types of tensors with its main API: ``` import torch from transformers import AutoTokenizer, AutoModelForSequenceClassification checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequences = ["I've been waiting for a HuggingFace course my whole life.", "So have I!"] tokens = tokenizer(sequences, padding=True, truncation=True, return_tensors="pt") output = model(**tokens)```
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Putting it all together - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter2/6"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Putting it all together - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;putting-it-all-together&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;special-tokens&quot;,&quot;title&quot;:&quot;Special tokens&quot;},{&quot;local&quot;:&quot;wrapping-up-from-tokenizer-to-model&quot;,&quot;title&quot;:&quot;Wrapping up: From tokenizer to model&quot;}],&quot;title&quot;:&quot;Putting it all together&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter2/6&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Putting it all together&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Putting it all together</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/2?fw=pt">Behind the pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/3?fw=pt">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/4?fw=pt">Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/5?fw=pt">Handling multiple sequences </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter2/6?fw=pt">Putting it all together </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/7?fw=pt">Basic usage completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="putting-it-all-together" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#putting-it-all-together"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Putting it all together</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-2-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter2/section6_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter2/section6_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>In the last few sections, we’ve been trying our best to do most of the work by hand. We’ve explored how tokenizers work and looked at tokenization, conversion to input IDs, padding, truncation, and attention masks.</p> <p>However, as we saw in section 2, the 🤗 Transformers API can handle all of this for us with a high-level function that we’ll dive into here. When you call your <code>tokenizer</code> directly on the sentence, you get back inputs that are ready to pass through your model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer checkpoint = <span class="hljs-string">"distilbert-base-uncased-finetuned-sst-2-english"</span> tokenizer = AutoTokenizer.from_pretrained(checkpoint) sequence = <span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span> model_inputs = tokenizer(sequence)</pre></div> <p>Here, the <code>model_inputs</code> variable contains everything that’s necessary for a model to operate well. For DistilBERT, that includes the input IDs as well as the attention mask. Other models that accept additional inputs will also have those output by the <code>tokenizer</code> object.</p> <p>As we’ll see in some examples below, this method is very powerful. First, it can tokenize a single sequence:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sequence = <span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span> model_inputs = tokenizer(sequence)</pre></div> <p>It also handles multiple sequences at a time, with no change in the API:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sequences = [<span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span>, <span class="hljs-string">"So have I!"</span>] model_inputs = tokenizer(sequences)</pre></div> <p>It can pad according to several objectives:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># Will pad the sequences up to the maximum sequence length</span> model_inputs = tokenizer(sequences, padding=<span class="hljs-string">"longest"</span>) <span class="hljs-comment"># Will pad the sequences up to the model max length</span> <span class="hljs-comment"># (512 for BERT or DistilBERT)</span> model_inputs = tokenizer(sequences, padding=<span class="hljs-string">"max_length"</span>) <span class="hljs-comment"># Will pad the sequences up to the specified max length</span> model_inputs = tokenizer(sequences, padding=<span class="hljs-string">"max_length"</span>, max_length=<span class="hljs-number">8</span>)</pre></div> <p>It can also truncate sequences:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sequences = [<span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span>, <span class="hljs-string">"So have I!"</span>] <span class="hljs-comment"># Will truncate the sequences that are longer than the model max length</span> <span class="hljs-comment"># (512 for BERT or DistilBERT)</span> model_inputs = tokenizer(sequences, truncation=<span class="hljs-literal">True</span>) <span class="hljs-comment"># Will truncate the sequences that are longer than the specified max length</span> model_inputs = tokenizer(sequences, max_length=<span class="hljs-number">8</span>, truncation=<span class="hljs-literal">True</span>)</pre></div> <p>The <code>tokenizer</code> object can handle the conversion to specific framework tensors, which can then be directly sent to the model. For example, in the following code sample we are prompting the tokenizer to return tensors from the different frameworks — <code>"pt"</code> returns PyTorch tensors, <code>"tf"</code> returns TensorFlow tensors, and <code>"np"</code> returns NumPy arrays:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sequences = [<span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span>, <span class="hljs-string">"So have I!"</span>] <span class="hljs-comment"># Returns PyTorch tensors</span> model_inputs = tokenizer(sequences, padding=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-comment"># Returns TensorFlow tensors</span> model_inputs = tokenizer(sequences, padding=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">"tf"</span>) <span class="hljs-comment"># Returns NumPy arrays</span> model_inputs = tokenizer(sequences, padding=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">"np"</span>)</pre></div> <h2 class="relative group"><a id="special-tokens" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#special-tokens"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Special tokens</span></h2> <p>If we take a look at the input IDs returned by the tokenizer, we will see they are a tiny bit different from what we had earlier:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sequence = <span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span> model_inputs = tokenizer(sequence) <span class="hljs-built_in">print</span>(model_inputs[<span class="hljs-string">"input_ids"</span>]) tokens = tokenizer.tokenize(sequence) ids = tokenizer.convert_tokens_to_ids(tokens) <span class="hljs-built_in">print</span>(ids)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">101</span>, <span class="hljs-number">1045</span>, <span class="hljs-number">1005</span>, <span class="hljs-number">2310</span>, <span class="hljs-number">2042</span>, <span class="hljs-number">3403</span>, <span class="hljs-number">2005</span>, <span class="hljs-number">1037</span>, <span class="hljs-number">17662</span>, <span class="hljs-number">12172</span>, <span class="hljs-number">2607</span>, <span class="hljs-number">2026</span>, <span class="hljs-number">2878</span>, <span class="hljs-number">2166</span>, <span class="hljs-number">1012</span>, <span class="hljs-number">102</span>] [<span class="hljs-number">1045</span>, <span class="hljs-number">1005</span>, <span class="hljs-number">2310</span>, <span class="hljs-number">2042</span>, <span class="hljs-number">3403</span>, <span class="hljs-number">2005</span>, <span class="hljs-number">1037</span>, <span class="hljs-number">17662</span>, <span class="hljs-number">12172</span>, <span class="hljs-number">2607</span>, <span class="hljs-number">2026</span>, <span class="hljs-number">2878</span>, <span class="hljs-number">2166</span>, <span class="hljs-number">1012</span>]</pre></div> <p>One token ID was added at the beginning, and one at the end. Let’s decode the two sequences of IDs above to see what this is about:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(tokenizer.decode(model_inputs[<span class="hljs-string">"input_ids"</span>])) <span class="hljs-built_in">print</span>(tokenizer.decode(ids))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">"[CLS] i've been waiting for a huggingface course my whole life. [SEP]"</span> <span class="hljs-string">"i've been waiting for a huggingface course my whole life."</span></pre></div> <p>The tokenizer added the special word <code>[CLS]</code> at the beginning and the special word <code>[SEP]</code> at the end. This is because the model was pretrained with those, so to get the same results for inference we need to add them as well. Note that some models don’t add special words, or add different ones; models may also add these special words only at the beginning, or only at the end. In any case, the tokenizer knows which ones are expected and will deal with this for you.</p> <h2 class="relative group"><a id="wrapping-up-from-tokenizer-to-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#wrapping-up-from-tokenizer-to-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Wrapping up: From tokenizer to model</span></h2> <p>Now that we’ve seen all the individual steps the <code>tokenizer</code> object uses when applied on texts, let’s see one final time how it can handle multiple sequences (padding!), very long sequences (truncation!), and multiple types of tensors with its main API:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForSequenceClassification checkpoint = <span class="hljs-string">"distilbert-base-uncased-finetuned-sst-2-english"</span> tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequences = [<span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span>, <span class="hljs-string">"So have I!"</span>] tokens = tokenizer(sequences, padding=<span class="hljs-literal">True</span>, truncation=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">"pt"</span>) output = model(**tokens)</pre></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter2/5?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Handling multiple sequences</a> <a href="/learn/nlp-course/chapter2/7?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Basic usage completed!<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;putting-it-all-together&quot;,&quot;url&quot;:&quot;#putting-it-all-together&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Special tokens&quot;,&quot;id&quot;:&quot;special-tokens&quot;,&quot;url&quot;:&quot;#special-tokens&quot;},{&quot;title&quot;:&quot;Wrapping up: From tokenizer to model&quot;,&quot;id&quot;:&quot;wrapping-up-from-tokenizer-to-model&quot;,&quot;url&quot;:&quot;#wrapping-up-from-tokenizer-to-model&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#putting-it-all-together" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-putting-it-all-together"><wbr>Putting it all together</a> <a href="#special-tokens" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-special-tokens"><wbr>Special tokens</a> <a href="#wrapping-up-from-tokenizer-to-model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-wrapping-up-from-tokenizer-to-model"><wbr>Wrapping up: <wbr>From tokenizer to model</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter2/6" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter2/6"); } </script> <iframe name="__privateStripeMetricsController4000" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter2%2F6%3Ffw%3Dpt&amp;title=Putting%20it%20all%20together%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:10.262Z
Basic usage completed! - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter2/7?fw=pt
## [](#basic-usage-completed)Basic usage completed! [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-2-questions) Great job following the course up to here! To recap, in this chapter you: - Learned the basic building blocks of a Transformer model. - Learned what makes up a tokenization pipeline. - Saw how to use a Transformer model in practice. - Learned how to leverage a tokenizer to convert text to tensors that are understandable by the model. - Set up a tokenizer and a model together to get from text to predictions. - Learned the limitations of input IDs, and learned about attention masks. - Played around with versatile and configurable tokenizer methods. From now on, you should be able to freely navigate the 🤗 Transformers docs: the vocabulary will sound familiar, and you’ve already seen the methods that you’ll use the majority of the time.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Basic usage completed! - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter2/7"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Basic usage completed! - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;basic-usage-completed&quot;,&quot;title&quot;:&quot;Basic usage completed!&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter2/7&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Basic usage completed!&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Basic usage completed!</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/2?fw=pt">Behind the pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/3?fw=pt">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/4?fw=pt">Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/5?fw=pt">Handling multiple sequences </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/6?fw=pt">Putting it all together </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter2/7?fw=pt">Basic usage completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="basic-usage-completed" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#basic-usage-completed"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Basic usage completed!</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-2-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>Great job following the course up to here! To recap, in this chapter you:</p> <ul><li>Learned the basic building blocks of a Transformer model.</li> <li>Learned what makes up a tokenization pipeline.</li> <li>Saw how to use a Transformer model in practice.</li> <li>Learned how to leverage a tokenizer to convert text to tensors that are understandable by the model.</li> <li>Set up a tokenizer and a model together to get from text to predictions.</li> <li>Learned the limitations of input IDs, and learned about attention masks.</li> <li>Played around with versatile and configurable tokenizer methods.</li></ul> <p>From now on, you should be able to freely navigate the 🤗 Transformers docs: the vocabulary will sound familiar, and you’ve already seen the methods that you’ll use the majority of the time.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter2/6?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Putting it all together</a> <a href="/learn/nlp-course/chapter2/8?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">End-of-chapter quiz<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;basic-usage-completed&quot;,&quot;url&quot;:&quot;#basic-usage-completed&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#basic-usage-completed" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-basic-usage-completed"><wbr>Basic usage completed!</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter2/7" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter2/7"); } </script> <iframe name="__privateStripeMetricsController5520" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter2%2F7%3Ffw%3Dpt&amp;title=Basic%20usage%20completed!%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:10.408Z
End-of-chapter quiz - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter2/8?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#end-of-chapter-quiz)End-of-chapter quiz [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-2-questions) ### [](#1.-what-is-the-order-of-the-language-modeling-pipeline?)1\. What is the order of the language modeling pipeline? ### [](#2.-how-many-dimensions-does-the-tensor-output-by-the-base-transformer-model-have,-and-what-are-they?)2\. How many dimensions does the tensor output by the base Transformer model have, and what are they? ### [](#3.-which-of-the-following-is-an-example-of-subword-tokenization?)3\. Which of the following is an example of subword tokenization? ### [](#4.-what-is-a-model-head?)4\. What is a model head? ### [](#5.-what-is-an-automodel?)5\. What is an AutoModel? ### [](#6.-what-are-the-techniques-to-be-aware-of-when-batching-sequences-of-different-lengths-together?)6\. What are the techniques to be aware of when batching sequences of different lengths together? ### [](#7.-what-is-the-point-of-applying-a-softmax-function-to-the-logits-output-by-a-sequence-classification-model?)7\. What is the point of applying a SoftMax function to the logits output by a sequence classification model? ### [](#8.-what-method-is-most-of-the-tokenizer-api-centered-around?)8\. What method is most of the tokenizer API centered around? ### [](#9.-what-does-the-<code>result</code>-variable-contain-in-this-code-sample?)9\. What does the `result` variable contain in this code sample? ``` from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") result = tokenizer.tokenize("Hello!")``` ### [](#10.-is-there-something-wrong-with-the-following-code?)10\. Is there something wrong with the following code? ``` from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") model = AutoModel.from_pretrained("gpt2") encoded = tokenizer("Hey!", return_tensors="pt") result = model(**encoded)```
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="End-of-chapter quiz - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter2/8"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>End-of-chapter quiz - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;end-of-chapter-quiz&quot;,&quot;title&quot;:&quot;End-of-chapter quiz&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter2/8&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;End-of-chapter quiz&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">End-of-chapter quiz</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/2?fw=pt">Behind the pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/3?fw=pt">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/4?fw=pt">Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/5?fw=pt">Handling multiple sequences </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/6?fw=pt">Putting it all together </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter2/7?fw=pt">Basic usage completed! </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter2/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="end-of-chapter-quiz" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#end-of-chapter-quiz"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>End-of-chapter quiz</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-2-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <h3 class="relative group"><a id="1.-what-is-the-order-of-the-language-modeling-pipeline?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#1.-what-is-the-order-of-the-language-modeling-pipeline?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>1. What is the order of the language modeling pipeline?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> First, the model, which handles text and returns raw predictions. The tokenizer then makes sense of these predictions and converts them back to text when needed.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> First, the tokenizer, which handles text and returns IDs. The model handles these IDs and outputs a prediction, which can be some text.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> The tokenizer handles text and returns IDs. The model handles these IDs and outputs a prediction. The tokenizer can then be used once again to convert these predictions back to some text.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="2.-how-many-dimensions-does-the-tensor-output-by-the-base-transformer-model-have,-and-what-are-they?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#2.-how-many-dimensions-does-the-tensor-output-by-the-base-transformer-model-have,-and-what-are-they?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>2. How many dimensions does the tensor output by the base Transformer model have, and what are they?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> 2: The sequence length and the batch size</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> 2: The sequence length and the hidden size</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> 3: The sequence length, the batch size, and the hidden size</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="3.-which-of-the-following-is-an-example-of-subword-tokenization?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#3.-which-of-the-following-is-an-example-of-subword-tokenization?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>3. Which of the following is an example of subword tokenization?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> WordPiece</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Character-based tokenization</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Splitting on whitespace and punctuation</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> BPE</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="4"> Unigram</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="5"> None of the above</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="4.-what-is-a-model-head?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#4.-what-is-a-model-head?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>4. What is a model head?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> A component of the base Transformer network that redirects tensors to their correct layers</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Also known as the self-attention mechanism, it adapts the representation of a token according to the other tokens of the sequence</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> An additional component, usually made up of one or a few layers, to convert the transformer predictions to a task-specific output</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="5.-what-is-an-automodel?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#5.-what-is-an-automodel?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>5. What is an AutoModel?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> A model that automatically trains on your data</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> An object that returns the correct architecture based on the checkpoint</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A model that automatically detects the language used for its inputs to load the correct weights</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="6.-what-are-the-techniques-to-be-aware-of-when-batching-sequences-of-different-lengths-together?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#6.-what-are-the-techniques-to-be-aware-of-when-batching-sequences-of-different-lengths-together?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>6. What are the techniques to be aware of when batching sequences of different lengths together?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Truncating</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Returning tensors</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Padding</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Attention masking</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="7.-what-is-the-point-of-applying-a-softmax-function-to-the-logits-output-by-a-sequence-classification-model?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#7.-what-is-the-point-of-applying-a-softmax-function-to-the-logits-output-by-a-sequence-classification-model?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>7. What is the point of applying a SoftMax function to the logits output by a sequence classification model?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It softens the logits so that they're more reliable.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It applies a lower and upper bound so that they're understandable.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> The total sum of the output is then 1, resulting in a possible probabilistic interpretation.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="8.-what-method-is-most-of-the-tokenizer-api-centered-around?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#8.-what-method-is-most-of-the-tokenizer-api-centered-around?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>8. What method is most of the tokenizer API centered around?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> <code>encode</code>, as it can encode text into IDs and IDs into predictions</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Calling the tokenizer object directly.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> <code>pad</code></label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> <code>tokenize</code></label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="9.-what-does-the-<code>result</code>-variable-contain-in-this-code-sample?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#9.-what-does-the-<code>result</code>-variable-contain-in-this-code-sample?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>9. What does the <code>result</code> variable contain in this code sample?</span></h3> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>) result = tokenizer.tokenize(<span class="hljs-string">"Hello!"</span>)</pre></div> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> A list of strings, each string being a token</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> A list of IDs</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A string containing all of the tokens</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="10.-is-there-something-wrong-with-the-following-code?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#10.-is-there-something-wrong-with-the-following-code?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>10. Is there something wrong with the following code?</span></h3> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>) model = AutoModel.from_pretrained(<span class="hljs-string">"gpt2"</span>) encoded = tokenizer(<span class="hljs-string">"Hey!"</span>, return_tensors=<span class="hljs-string">"pt"</span>) result = model(**encoded)</pre></div> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> No, it seems correct.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> The tokenizer and model should always be from the same checkpoint.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It's good practice to pad and truncate with the tokenizer as every input is a batch.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter2/7?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Basic usage completed!</a> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isLoggedIn&quot;:false,&quot;nextChapterUrl&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;,&quot;quizChapter&quot;:2}" data-target="CourseCompleteButton"><div class="relative ml-auto"> <button class="btn btn-lg" disabled=""><svg class="mr-2" width="1em" height="1em" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0 16C0 7.16344 7.16344 0 16 0V0C24.8366 0 32 7.16344 32 16V16C32 24.8366 24.8366 32 16 32V32C7.16344 32 0 24.8366 0 16V16Z" fill="#D9FFD2"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M23.5607 10.9393C24.1464 11.5251 24.1464 12.4749 23.5607 13.0607L14.5 22.1213L8.93934 16.5607C8.35355 15.9749 8.35355 15.0251 8.93934 14.4393C9.52513 13.8536 10.4749 13.8536 11.0607 14.4393L14.5 17.8787L21.4393 10.9393C22.0251 10.3536 22.9749 10.3536 23.5607 10.9393Z" fill="#188B3F"></path></svg> Complete Chapter</button></div></div> <a href="/learn/nlp-course/chapter3/1?fw=pt" class="hidden">Next chapter</a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;id&quot;:&quot;end-of-chapter-quiz&quot;,&quot;url&quot;:&quot;#end-of-chapter-quiz&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#end-of-chapter-quiz" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-end-of-chapter-quiz"><wbr>End-of-chapter quiz</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter2/8" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter2/8"); } </script> <iframe name="__privateStripeMetricsController0840" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter2%2F8%3Ffw%3Dpt&amp;title=End-of-chapter%20quiz%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:10.914Z
Introduction - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter3/1?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#introduction)Introduction [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-3-questions) In [Chapter 2](/course/chapter2) we explored how to use tokenizers and pretrained models to make predictions. But what if you want to fine-tune a pretrained model for your own dataset? That’s the topic of this chapter! You will learn: - How to prepare a large dataset from the Hub - How to use the high-level `Trainer` API to fine-tune a model - How to use a custom training loop - How to leverage the 🤗 Accelerate library to easily run that custom training loop on any distributed setup In order to upload your trained checkpoints to the Hugging Face Hub, you will need a huggingface.co account: [create an account](https://huggingface.co/join)
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Introduction - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter3/1"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Introduction - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;introduction&quot;,&quot;title&quot;:&quot;Introduction&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter3/1&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Introduction&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Introduction</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter3/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/2?fw=pt">Processing the data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/3?fw=pt">Fine-tuning a model with the Trainer API or Keras </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/4?fw=pt">A full training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/5?fw=pt">Fine-tuning, Check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/6?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="introduction" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#introduction"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Introduction</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-3-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>In <a href="/course/chapter2">Chapter 2</a> we explored how to use tokenizers and pretrained models to make predictions. But what if you want to fine-tune a pretrained model for your own dataset? That’s the topic of this chapter! You will learn:</p> <ul><li>How to prepare a large dataset from the Hub</li> <li>How to use the high-level <code>Trainer</code> API to fine-tune a model</li> <li>How to use a custom training loop</li> <li>How to leverage the 🤗 Accelerate library to easily run that custom training loop on any distributed setup</li></ul> <p>In order to upload your trained checkpoints to the Hugging Face Hub, you will need a huggingface.co account: <a href="https://huggingface.co/join" rel="nofollow">create an account</a></p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter2/8?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>End-of-chapter quiz</a> <a href="/learn/nlp-course/chapter3/2?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Processing the data<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;introduction&quot;,&quot;url&quot;:&quot;#introduction&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#introduction" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-introduction"><wbr>Introduction</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter3/1" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter3/1"); } </script> <iframe name="__privateStripeMetricsController9600" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter3%2F1%3Ffw%3Dpt&amp;title=Introduction%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:11.451Z
Processing the data - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter3/2?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#processing-the-data)Processing the data [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-3-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter3/section2_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter3/section2_pt.ipynb) Continuing with the example from the [previous chapter](/course/chapter2), here is how we would train a sequence classifier on one batch in PyTorch: ``` import torch from transformers import AdamW, AutoTokenizer, AutoModelForSequenceClassification checkpoint = "bert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequences = [ "I've been waiting for a HuggingFace course my whole life.", "This course is amazing!", ] batch = tokenizer(sequences, padding=True, truncation=True, return_tensors="pt") batch["labels"] = torch.tensor([1, 1]) optimizer = AdamW(model.parameters()) loss = model(**batch).loss loss.backward() optimizer.step()``` Of course, just training the model on two sentences is not going to yield very good results. To get better results, you will need to prepare a bigger dataset. In this section we will use as an example the MRPC (Microsoft Research Paraphrase Corpus) dataset, introduced in a [paper](https://www.aclweb.org/anthology/I05-5002.pdf) by William B. Dolan and Chris Brockett. The dataset consists of 5,801 pairs of sentences, with a label indicating if they are paraphrases or not (i.e., if both sentences mean the same thing). We’ve selected it for this chapter because it’s a small dataset, so it’s easy to experiment with training on it. ### [](#loading-a-dataset-from-the-hub)Loading a dataset from the Hub The Hub doesn’t just contain models; it also has multiple datasets in lots of different languages. You can browse the datasets [here](https://huggingface.co/datasets), and we recommend you try to load and process a new dataset once you have gone through this section (see the general documentation [here](https://huggingface.co/docs/datasets/loading_datasets.html#from-the-huggingface-hub)). But for now, let’s focus on the MRPC dataset! This is one of the 10 datasets composing the [GLUE benchmark](https://gluebenchmark.com/), which is an academic benchmark that is used to measure the performance of ML models across 10 different text classification tasks. The 🤗 Datasets library provides a very simple command to download and cache a dataset on the Hub. We can download the MRPC dataset like this: ``` from datasets import load_dataset raw_datasets = load_dataset("glue", "mrpc") raw_datasets``` ``` DatasetDict({ train: Dataset({ features: ['sentence1', 'sentence2', 'label', 'idx'], num_rows: 3668 }) validation: Dataset({ features: ['sentence1', 'sentence2', 'label', 'idx'], num_rows: 408 }) test: Dataset({ features: ['sentence1', 'sentence2', 'label', 'idx'], num_rows: 1725 }) })``` As you can see, we get a `DatasetDict` object which contains the training set, the validation set, and the test set. Each of those contains several columns (`sentence1`, `sentence2`, `label`, and `idx`) and a variable number of rows, which are the number of elements in each set (so, there are 3,668 pairs of sentences in the training set, 408 in the validation set, and 1,725 in the test set). This command downloads and caches the dataset, by default in _~/.cache/huggingface/datasets_. Recall from Chapter 2 that you can customize your cache folder by setting the `HF_HOME` environment variable. We can access each pair of sentences in our `raw_datasets` object by indexing, like with a dictionary: ``` raw_train_dataset = raw_datasets["train"] raw_train_dataset[0]``` ``` {'idx': 0, 'label': 1, 'sentence1': 'Amrozi accused his brother , whom he called " the witness " , of deliberately distorting his evidence .', 'sentence2': 'Referring to him as only " the witness " , Amrozi accused his brother of deliberately distorting his evidence .'}``` We can see the labels are already integers, so we won’t have to do any preprocessing there. To know which integer corresponds to which label, we can inspect the `features` of our `raw_train_dataset`. This will tell us the type of each column: ``` raw_train_dataset.features``` ``` {'sentence1': Value(dtype='string', id=None), 'sentence2': Value(dtype='string', id=None), 'label': ClassLabel(num_classes=2, names=['not_equivalent', 'equivalent'], names_file=None, id=None), 'idx': Value(dtype='int32', id=None)}``` Behind the scenes, `label` is of type `ClassLabel`, and the mapping of integers to label name is stored in the _names_ folder. `0` corresponds to `not_equivalent`, and `1` corresponds to `equivalent`. ✏️ **Try it out!** Look at element 15 of the training set and element 87 of the validation set. What are their labels? ### [](#preprocessing-a-dataset)Preprocessing a dataset To preprocess the dataset, we need to convert the text to numbers the model can make sense of. As you saw in the [previous chapter](/course/chapter2), this is done with a tokenizer. We can feed the tokenizer one sentence or a list of sentences, so we can directly tokenize all the first sentences and all the second sentences of each pair like this: ``` from transformers import AutoTokenizer checkpoint = "bert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(checkpoint) tokenized_sentences_1 = tokenizer(raw_datasets["train"]["sentence1"]) tokenized_sentences_2 = tokenizer(raw_datasets["train"]["sentence2"])``` However, we can’t just pass two sequences to the model and get a prediction of whether the two sentences are paraphrases or not. We need to handle the two sequences as a pair, and apply the appropriate preprocessing. Fortunately, the tokenizer can also take a pair of sequences and prepare it the way our BERT model expects: ``` inputs = tokenizer("This is the first sentence.", "This is the second one.") inputs``` ``` { 'input_ids': [101, 2023, 2003, 1996, 2034, 6251, 1012, 102, 2023, 2003, 1996, 2117, 2028, 1012, 102], 'token_type_ids': [0, 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, 1] }``` We discussed the `input_ids` and `attention_mask` keys in [Chapter 2](/course/chapter2), but we put off talking about `token_type_ids`. In this example, this is what tells the model which part of the input is the first sentence and which is the second sentence. ✏️ **Try it out!** Take element 15 of the training set and tokenize the two sentences separately and as a pair. What’s the difference between the two results? If we decode the IDs inside `input_ids` back to words: ``` tokenizer.convert_ids_to_tokens(inputs["input_ids"])``` we will get: ``` ['[CLS]', 'this', 'is', 'the', 'first', 'sentence', '.', '[SEP]', 'this', 'is', 'the', 'second', 'one', '.', '[SEP]']``` So we see the model expects the inputs to be of the form `[CLS] sentence1 [SEP] sentence2 [SEP]` when there are two sentences. Aligning this with the `token_type_ids` gives us: ``` ['[CLS]', 'this', 'is', 'the', 'first', 'sentence', '.', '[SEP]', 'this', 'is', 'the', 'second', 'one', '.', '[SEP]'] [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]``` As you can see, the parts of the input corresponding to `[CLS] sentence1 [SEP]` all have a token type ID of `0`, while the other parts, corresponding to `sentence2 [SEP]`, all have a token type ID of `1`. Note that if you select a different checkpoint, you won’t necessarily have the `token_type_ids` in your tokenized inputs (for instance, they’re not returned if you use a DistilBERT model). They are only returned when the model will know what to do with them, because it has seen them during its pretraining. Here, BERT is pretrained with token type IDs, and on top of the masked language modeling objective we talked about in [Chapter 1](/course/chapter1), it has an additional objective called _next sentence prediction_. The goal with this task is to model the relationship between pairs of sentences. With next sentence prediction, the model is provided pairs of sentences (with randomly masked tokens) and asked to predict whether the second sentence follows the first. To make the task non-trivial, half of the time the sentences follow each other in the original document they were extracted from, and the other half of the time the two sentences come from two different documents. In general, you don’t need to worry about whether or not there are `token_type_ids` in your tokenized inputs: as long as you use the same checkpoint for the tokenizer and the model, everything will be fine as the tokenizer knows what to provide to its model. Now that we have seen how our tokenizer can deal with one pair of sentences, we can use it to tokenize our whole dataset: like in the [previous chapter](/course/chapter2), we can feed the tokenizer a list of pairs of sentences by giving it the list of first sentences, then the list of second sentences. This is also compatible with the padding and truncation options we saw in [Chapter 2](/course/chapter2). So, one way to preprocess the training dataset is: ``` tokenized_dataset = tokenizer( raw_datasets["train"]["sentence1"], raw_datasets["train"]["sentence2"], padding=True, truncation=True, )``` This works well, but it has the disadvantage of returning a dictionary (with our keys, `input_ids`, `attention_mask`, and `token_type_ids`, and values that are lists of lists). It will also only work if you have enough RAM to store your whole dataset during the tokenization (whereas the datasets from the 🤗 Datasets library are [Apache Arrow](https://arrow.apache.org/) files stored on the disk, so you only keep the samples you ask for loaded in memory). To keep the data as a dataset, we will use the [`Dataset.map()`](https://huggingface.co/docs/datasets/package_reference/main_classes.html#datasets.Dataset.map) method. This also allows us some extra flexibility, if we need more preprocessing done than just tokenization. The `map()` method works by applying a function on each element of the dataset, so let’s define a function that tokenizes our inputs: ``` def tokenize_function(example): return tokenizer(example["sentence1"], example["sentence2"], truncation=True)``` This function takes a dictionary (like the items of our dataset) and returns a new dictionary with the keys `input_ids`, `attention_mask`, and `token_type_ids`. Note that it also works if the `example` dictionary contains several samples (each key as a list of sentences) since the `tokenizer` works on lists of pairs of sentences, as seen before. This will allow us to use the option `batched=True` in our call to `map()`, which will greatly speed up the tokenization. The `tokenizer` is backed by a tokenizer written in Rust from the [🤗 Tokenizers](https://github.com/huggingface/tokenizers) library. This tokenizer can be very fast, but only if we give it lots of inputs at once. Note that we’ve left the `padding` argument out in our tokenization function for now. This is because padding all the samples to the maximum length is not efficient: it’s better to pad the samples when we’re building a batch, as then we only need to pad to the maximum length in that batch, and not the maximum length in the entire dataset. This can save a lot of time and processing power when the inputs have very variable lengths! Here is how we apply the tokenization function on all our datasets at once. We’re using `batched=True` in our call to `map` so the function is applied to multiple elements of our dataset at once, and not on each element separately. This allows for faster preprocessing. ``` tokenized_datasets = raw_datasets.map(tokenize_function, batched=True) tokenized_datasets``` The way the 🤗 Datasets library applies this processing is by adding new fields to the datasets, one for each key in the dictionary returned by the preprocessing function: ``` DatasetDict({ train: Dataset({ features: ['attention_mask', 'idx', 'input_ids', 'label', 'sentence1', 'sentence2', 'token_type_ids'], num_rows: 3668 }) validation: Dataset({ features: ['attention_mask', 'idx', 'input_ids', 'label', 'sentence1', 'sentence2', 'token_type_ids'], num_rows: 408 }) test: Dataset({ features: ['attention_mask', 'idx', 'input_ids', 'label', 'sentence1', 'sentence2', 'token_type_ids'], num_rows: 1725 }) })``` You can even use multiprocessing when applying your preprocessing function with `map()` by passing along a `num_proc` argument. We didn’t do this here because the 🤗 Tokenizers library already uses multiple threads to tokenize our samples faster, but if you are not using a fast tokenizer backed by this library, this could speed up your preprocessing. Our `tokenize_function` returns a dictionary with the keys `input_ids`, `attention_mask`, and `token_type_ids`, so those three fields are added to all splits of our dataset. Note that we could also have changed existing fields if our preprocessing function returned a new value for an existing key in the dataset to which we applied `map()`. The last thing we will need to do is pad all the examples to the length of the longest element when we batch elements together — a technique we refer to as _dynamic padding_. ### [](#dynamic-padding)Dynamic padding The function that is responsible for putting together samples inside a batch is called a _collate function_. It’s an argument you can pass when you build a `DataLoader`, the default being a function that will just convert your samples to PyTorch tensors and concatenate them (recursively if your elements are lists, tuples, or dictionaries). This won’t be possible in our case since the inputs we have won’t all be of the same size. We have deliberately postponed the padding, to only apply it as necessary on each batch and avoid having over-long inputs with a lot of padding. This will speed up training by quite a bit, but note that if you’re training on a TPU it can cause problems — TPUs prefer fixed shapes, even when that requires extra padding. To do this in practice, we have to define a collate function that will apply the correct amount of padding to the items of the dataset we want to batch together. Fortunately, the 🤗 Transformers library provides us with such a function via `DataCollatorWithPadding`. It takes a tokenizer when you instantiate it (to know which padding token to use, and whether the model expects padding to be on the left or on the right of the inputs) and will do everything you need: ``` from transformers import DataCollatorWithPadding data_collator = DataCollatorWithPadding(tokenizer=tokenizer)``` To test this new toy, let’s grab a few samples from our training set that we would like to batch together. Here, we remove the columns `idx`, `sentence1`, and `sentence2` as they won’t be needed and contain strings (and we can’t create tensors with strings) and have a look at the lengths of each entry in the batch: ``` samples = tokenized_datasets["train"][:8] samples = {k: v for k, v in samples.items() if k not in ["idx", "sentence1", "sentence2"]} [len(x) for x in samples["input_ids"]]``` ``` [50, 59, 47, 67, 59, 50, 62, 32]``` No surprise, we get samples of varying length, from 32 to 67. Dynamic padding means the samples in this batch should all be padded to a length of 67, the maximum length inside the batch. Without dynamic padding, all of the samples would have to be padded to the maximum length in the whole dataset, or the maximum length the model can accept. Let’s double-check that our `data_collator` is dynamically padding the batch properly: ``` batch = data_collator(samples) {k: v.shape for k, v in batch.items()}``` ``` {'attention_mask': torch.Size([8, 67]), 'input_ids': torch.Size([8, 67]), 'token_type_ids': torch.Size([8, 67]), 'labels': torch.Size([8])}``` Looking good! Now that we’ve gone from raw text to batches our model can deal with, we’re ready to fine-tune it! ✏️ **Try it out!** Replicate the preprocessing on the GLUE SST-2 dataset. It’s a little bit different since it’s composed of single sentences instead of pairs, but the rest of what we did should look the same. For a harder challenge, try to write a preprocessing function that works on any of the GLUE tasks.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Processing the data - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter3/2"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Processing the data - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;processing-the-data&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;loading-a-dataset-from-the-hub&quot;,&quot;title&quot;:&quot;Loading a dataset from the Hub&quot;},{&quot;local&quot;:&quot;preprocessing-a-dataset&quot;,&quot;title&quot;:&quot;Preprocessing a dataset&quot;},{&quot;local&quot;:&quot;dynamic-padding&quot;,&quot;title&quot;:&quot;Dynamic padding&quot;}],&quot;title&quot;:&quot;Processing the data&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter3/2&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Processing the data&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Processing the data</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/1?fw=pt">Introduction </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter3/2?fw=pt">Processing the data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/3?fw=pt">Fine-tuning a model with the Trainer API or Keras </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/4?fw=pt">A full training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/5?fw=pt">Fine-tuning, Check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/6?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="processing-the-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#processing-the-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Processing the data</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-3-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter3/section2_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter3/section2_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>Continuing with the example from the <a href="/course/chapter2">previous chapter</a>, here is how we would train a sequence classifier on one batch in PyTorch:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AdamW, AutoTokenizer, AutoModelForSequenceClassification <span class="hljs-comment"># Same as before</span> checkpoint = <span class="hljs-string">"bert-base-uncased"</span> tokenizer = AutoTokenizer.from_pretrained(checkpoint) model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequences = [ <span class="hljs-string">"I've been waiting for a HuggingFace course my whole life."</span>, <span class="hljs-string">"This course is amazing!"</span>, ] batch = tokenizer(sequences, padding=<span class="hljs-literal">True</span>, truncation=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-comment"># This is new</span> batch[<span class="hljs-string">"labels"</span>] = torch.tensor([<span class="hljs-number">1</span>, <span class="hljs-number">1</span>]) optimizer = AdamW(model.parameters()) loss = model(**batch).loss loss.backward() optimizer.step()</pre></div> <p>Of course, just training the model on two sentences is not going to yield very good results. To get better results, you will need to prepare a bigger dataset.</p> <p>In this section we will use as an example the MRPC (Microsoft Research Paraphrase Corpus) dataset, introduced in a <a href="https://www.aclweb.org/anthology/I05-5002.pdf" rel="nofollow">paper</a> by William B. Dolan and Chris Brockett. The dataset consists of 5,801 pairs of sentences, with a label indicating if they are paraphrases or not (i.e., if both sentences mean the same thing). We’ve selected it for this chapter because it’s a small dataset, so it’s easy to experiment with training on it.</p> <h3 class="relative group"><a id="loading-a-dataset-from-the-hub" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-a-dataset-from-the-hub"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Loading a dataset from the Hub</span></h3> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/_BZearw7f0w" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The Hub doesn’t just contain models; it also has multiple datasets in lots of different languages. You can browse the datasets <a href="https://huggingface.co/datasets" rel="nofollow">here</a>, and we recommend you try to load and process a new dataset once you have gone through this section (see the general documentation <a href="https://huggingface.co/docs/datasets/loading_datasets.html#from-the-huggingface-hub" rel="nofollow">here</a>). But for now, let’s focus on the MRPC dataset! This is one of the 10 datasets composing the <a href="https://gluebenchmark.com/" rel="nofollow">GLUE benchmark</a>, which is an academic benchmark that is used to measure the performance of ML models across 10 different text classification tasks.</p> <p>The 🤗 Datasets library provides a very simple command to download and cache a dataset on the Hub. We can download the MRPC dataset like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset raw_datasets = load_dataset(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mrpc"</span>) raw_datasets</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'sentence1'</span>, <span class="hljs-string">'sentence2'</span>, <span class="hljs-string">'label'</span>, <span class="hljs-string">'idx'</span>], num_rows: <span class="hljs-number">3668</span> }) validation: Dataset({ features: [<span class="hljs-string">'sentence1'</span>, <span class="hljs-string">'sentence2'</span>, <span class="hljs-string">'label'</span>, <span class="hljs-string">'idx'</span>], num_rows: <span class="hljs-number">408</span> }) test: Dataset({ features: [<span class="hljs-string">'sentence1'</span>, <span class="hljs-string">'sentence2'</span>, <span class="hljs-string">'label'</span>, <span class="hljs-string">'idx'</span>], num_rows: <span class="hljs-number">1725</span> }) })</pre></div> <p>As you can see, we get a <code>DatasetDict</code> object which contains the training set, the validation set, and the test set. Each of those contains several columns (<code>sentence1</code>, <code>sentence2</code>, <code>label</code>, and <code>idx</code>) and a variable number of rows, which are the number of elements in each set (so, there are 3,668 pairs of sentences in the training set, 408 in the validation set, and 1,725 in the test set).</p> <p>This command downloads and caches the dataset, by default in <em>~/.cache/huggingface/datasets</em>. Recall from Chapter 2 that you can customize your cache folder by setting the <code>HF_HOME</code> environment variable.</p> <p>We can access each pair of sentences in our <code>raw_datasets</code> object by indexing, like with a dictionary:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>raw_train_dataset = raw_datasets[<span class="hljs-string">"train"</span>] raw_train_dataset[<span class="hljs-number">0</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'idx'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'label'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'sentence1'</span>: <span class="hljs-string">'Amrozi accused his brother , whom he called " the witness " , of deliberately distorting his evidence .'</span>, <span class="hljs-string">'sentence2'</span>: <span class="hljs-string">'Referring to him as only " the witness " , Amrozi accused his brother of deliberately distorting his evidence .'</span>}</pre></div> <p>We can see the labels are already integers, so we won’t have to do any preprocessing there. To know which integer corresponds to which label, we can inspect the <code>features</code> of our <code>raw_train_dataset</code>. This will tell us the type of each column:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>raw_train_dataset.features</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'sentence1'</span>: Value(dtype=<span class="hljs-string">'string'</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), <span class="hljs-string">'sentence2'</span>: Value(dtype=<span class="hljs-string">'string'</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), <span class="hljs-string">'label'</span>: ClassLabel(num_classes=<span class="hljs-number">2</span>, names=[<span class="hljs-string">'not_equivalent'</span>, <span class="hljs-string">'equivalent'</span>], names_file=<span class="hljs-literal">None</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), <span class="hljs-string">'idx'</span>: Value(dtype=<span class="hljs-string">'int32'</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>)}</pre></div> <p>Behind the scenes, <code>label</code> is of type <code>ClassLabel</code>, and the mapping of integers to label name is stored in the <em>names</em> folder. <code>0</code> corresponds to <code>not_equivalent</code>, and <code>1</code> corresponds to <code>equivalent</code>.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Look at element 15 of the training set and element 87 of the validation set. What are their labels?</p></div> <h3 class="relative group"><a id="preprocessing-a-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preprocessing-a-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preprocessing a dataset</span></h3> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/0u3ioSwev3s" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>To preprocess the dataset, we need to convert the text to numbers the model can make sense of. As you saw in the <a href="/course/chapter2">previous chapter</a>, this is done with a tokenizer. We can feed the tokenizer one sentence or a list of sentences, so we can directly tokenize all the first sentences and all the second sentences of each pair like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer checkpoint = <span class="hljs-string">"bert-base-uncased"</span> tokenizer = AutoTokenizer.from_pretrained(checkpoint) tokenized_sentences_1 = tokenizer(raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-string">"sentence1"</span>]) tokenized_sentences_2 = tokenizer(raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-string">"sentence2"</span>])</pre></div> <p>However, we can’t just pass two sequences to the model and get a prediction of whether the two sentences are paraphrases or not. We need to handle the two sequences as a pair, and apply the appropriate preprocessing. Fortunately, the tokenizer can also take a pair of sequences and prepare it the way our BERT model expects:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs = tokenizer(<span class="hljs-string">"This is the first sentence."</span>, <span class="hljs-string">"This is the second one."</span>) inputs</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{ <span class="hljs-string">'input_ids'</span>: [<span class="hljs-number">101</span>, <span class="hljs-number">2023</span>, <span class="hljs-number">2003</span>, <span class="hljs-number">1996</span>, <span class="hljs-number">2034</span>, <span class="hljs-number">6251</span>, <span class="hljs-number">1012</span>, <span class="hljs-number">102</span>, <span class="hljs-number">2023</span>, <span class="hljs-number">2003</span>, <span class="hljs-number">1996</span>, <span class="hljs-number">2117</span>, <span class="hljs-number">2028</span>, <span class="hljs-number">1012</span>, <span class="hljs-number">102</span>], <span class="hljs-string">'token_type_ids'</span>: [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>], <span class="hljs-string">'attention_mask'</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>] }</pre></div> <p>We discussed the <code>input_ids</code> and <code>attention_mask</code> keys in <a href="/course/chapter2">Chapter 2</a>, but we put off talking about <code>token_type_ids</code>. In this example, this is what tells the model which part of the input is the first sentence and which is the second sentence.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Take element 15 of the training set and tokenize the two sentences separately and as a pair. What’s the difference between the two results?</p></div> <p>If we decode the IDs inside <code>input_ids</code> back to words:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.convert_ids_to_tokens(inputs[<span class="hljs-string">"input_ids"</span>])</pre></div> <p>we will get:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'[CLS]'</span>, <span class="hljs-string">'this'</span>, <span class="hljs-string">'is'</span>, <span class="hljs-string">'the'</span>, <span class="hljs-string">'first'</span>, <span class="hljs-string">'sentence'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'[SEP]'</span>, <span class="hljs-string">'this'</span>, <span class="hljs-string">'is'</span>, <span class="hljs-string">'the'</span>, <span class="hljs-string">'second'</span>, <span class="hljs-string">'one'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'[SEP]'</span>]</pre></div> <p>So we see the model expects the inputs to be of the form <code>[CLS] sentence1 [SEP] sentence2 [SEP]</code> when there are two sentences. Aligning this with the <code>token_type_ids</code> gives us:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'[CLS]'</span>, <span class="hljs-string">'this'</span>, <span class="hljs-string">'is'</span>, <span class="hljs-string">'the'</span>, <span class="hljs-string">'first'</span>, <span class="hljs-string">'sentence'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'[SEP]'</span>, <span class="hljs-string">'this'</span>, <span class="hljs-string">'is'</span>, <span class="hljs-string">'the'</span>, <span class="hljs-string">'second'</span>, <span class="hljs-string">'one'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'[SEP]'</span>] [ <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]</pre></div> <p>As you can see, the parts of the input corresponding to <code>[CLS] sentence1 [SEP]</code> all have a token type ID of <code>0</code>, while the other parts, corresponding to <code>sentence2 [SEP]</code>, all have a token type ID of <code>1</code>.</p> <p>Note that if you select a different checkpoint, you won’t necessarily have the <code>token_type_ids</code> in your tokenized inputs (for instance, they’re not returned if you use a DistilBERT model). They are only returned when the model will know what to do with them, because it has seen them during its pretraining.</p> <p>Here, BERT is pretrained with token type IDs, and on top of the masked language modeling objective we talked about in <a href="/course/chapter1">Chapter 1</a>, it has an additional objective called <em>next sentence prediction</em>. The goal with this task is to model the relationship between pairs of sentences.</p> <p>With next sentence prediction, the model is provided pairs of sentences (with randomly masked tokens) and asked to predict whether the second sentence follows the first. To make the task non-trivial, half of the time the sentences follow each other in the original document they were extracted from, and the other half of the time the two sentences come from two different documents.</p> <p>In general, you don’t need to worry about whether or not there are <code>token_type_ids</code> in your tokenized inputs: as long as you use the same checkpoint for the tokenizer and the model, everything will be fine as the tokenizer knows what to provide to its model.</p> <p>Now that we have seen how our tokenizer can deal with one pair of sentences, we can use it to tokenize our whole dataset: like in the <a href="/course/chapter2">previous chapter</a>, we can feed the tokenizer a list of pairs of sentences by giving it the list of first sentences, then the list of second sentences. This is also compatible with the padding and truncation options we saw in <a href="/course/chapter2">Chapter 2</a>. So, one way to preprocess the training dataset is:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_dataset = tokenizer( raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-string">"sentence1"</span>], raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-string">"sentence2"</span>], padding=<span class="hljs-literal">True</span>, truncation=<span class="hljs-literal">True</span>, )</pre></div> <p>This works well, but it has the disadvantage of returning a dictionary (with our keys, <code>input_ids</code>, <code>attention_mask</code>, and <code>token_type_ids</code>, and values that are lists of lists). It will also only work if you have enough RAM to store your whole dataset during the tokenization (whereas the datasets from the 🤗 Datasets library are <a href="https://arrow.apache.org/" rel="nofollow">Apache Arrow</a> files stored on the disk, so you only keep the samples you ask for loaded in memory).</p> <p>To keep the data as a dataset, we will use the <a href="https://huggingface.co/docs/datasets/package_reference/main_classes.html#datasets.Dataset.map" rel="nofollow"><code>Dataset.map()</code></a> method. This also allows us some extra flexibility, if we need more preprocessing done than just tokenization. The <code>map()</code> method works by applying a function on each element of the dataset, so let’s define a function that tokenizes our inputs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_function</span>(<span class="hljs-params">example</span>): <span class="hljs-keyword">return</span> tokenizer(example[<span class="hljs-string">"sentence1"</span>], example[<span class="hljs-string">"sentence2"</span>], truncation=<span class="hljs-literal">True</span>)</pre></div> <p>This function takes a dictionary (like the items of our dataset) and returns a new dictionary with the keys <code>input_ids</code>, <code>attention_mask</code>, and <code>token_type_ids</code>. Note that it also works if the <code>example</code> dictionary contains several samples (each key as a list of sentences) since the <code>tokenizer</code> works on lists of pairs of sentences, as seen before. This will allow us to use the option <code>batched=True</code> in our call to <code>map()</code>, which will greatly speed up the tokenization. The <code>tokenizer</code> is backed by a tokenizer written in Rust from the <a href="https://github.com/huggingface/tokenizers" rel="nofollow">🤗 Tokenizers</a> library. This tokenizer can be very fast, but only if we give it lots of inputs at once.</p> <p>Note that we’ve left the <code>padding</code> argument out in our tokenization function for now. This is because padding all the samples to the maximum length is not efficient: it’s better to pad the samples when we’re building a batch, as then we only need to pad to the maximum length in that batch, and not the maximum length in the entire dataset. This can save a lot of time and processing power when the inputs have very variable lengths!</p> <p>Here is how we apply the tokenization function on all our datasets at once. We’re using <code>batched=True</code> in our call to <code>map</code> so the function is applied to multiple elements of our dataset at once, and not on each element separately. This allows for faster preprocessing.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>(tokenize_function, batched=<span class="hljs-literal">True</span>) tokenized_datasets</pre></div> <p>The way the 🤗 Datasets library applies this processing is by adding new fields to the datasets, one for each key in the dictionary returned by the preprocessing function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'idx'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'label'</span>, <span class="hljs-string">'sentence1'</span>, <span class="hljs-string">'sentence2'</span>, <span class="hljs-string">'token_type_ids'</span>], num_rows: <span class="hljs-number">3668</span> }) validation: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'idx'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'label'</span>, <span class="hljs-string">'sentence1'</span>, <span class="hljs-string">'sentence2'</span>, <span class="hljs-string">'token_type_ids'</span>], num_rows: <span class="hljs-number">408</span> }) test: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'idx'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'label'</span>, <span class="hljs-string">'sentence1'</span>, <span class="hljs-string">'sentence2'</span>, <span class="hljs-string">'token_type_ids'</span>], num_rows: <span class="hljs-number">1725</span> }) })</pre></div> <p>You can even use multiprocessing when applying your preprocessing function with <code>map()</code> by passing along a <code>num_proc</code> argument. We didn’t do this here because the 🤗 Tokenizers library already uses multiple threads to tokenize our samples faster, but if you are not using a fast tokenizer backed by this library, this could speed up your preprocessing.</p> <p>Our <code>tokenize_function</code> returns a dictionary with the keys <code>input_ids</code>, <code>attention_mask</code>, and <code>token_type_ids</code>, so those three fields are added to all splits of our dataset. Note that we could also have changed existing fields if our preprocessing function returned a new value for an existing key in the dataset to which we applied <code>map()</code>.</p> <p>The last thing we will need to do is pad all the examples to the length of the longest element when we batch elements together — a technique we refer to as <em>dynamic padding</em>.</p> <h3 class="relative group"><a id="dynamic-padding" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#dynamic-padding"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Dynamic padding</span></h3> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/7q5NyFT8REg" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The function that is responsible for putting together samples inside a batch is called a <em>collate function</em>. It’s an argument you can pass when you build a <code>DataLoader</code>, the default being a function that will just convert your samples to PyTorch tensors and concatenate them (recursively if your elements are lists, tuples, or dictionaries). This won’t be possible in our case since the inputs we have won’t all be of the same size. We have deliberately postponed the padding, to only apply it as necessary on each batch and avoid having over-long inputs with a lot of padding. This will speed up training by quite a bit, but note that if you’re training on a TPU it can cause problems — TPUs prefer fixed shapes, even when that requires extra padding.</p> <p>To do this in practice, we have to define a collate function that will apply the correct amount of padding to the items of the dataset we want to batch together. Fortunately, the 🤗 Transformers library provides us with such a function via <code>DataCollatorWithPadding</code>. It takes a tokenizer when you instantiate it (to know which padding token to use, and whether the model expects padding to be on the left or on the right of the inputs) and will do everything you need:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorWithPadding data_collator = DataCollatorWithPadding(tokenizer=tokenizer)</pre></div> <p>To test this new toy, let’s grab a few samples from our training set that we would like to batch together. Here, we remove the columns <code>idx</code>, <code>sentence1</code>, and <code>sentence2</code> as they won’t be needed and contain strings (and we can’t create tensors with strings) and have a look at the lengths of each entry in the batch:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>samples = tokenized_datasets[<span class="hljs-string">"train"</span>][:<span class="hljs-number">8</span>] samples = {k: v <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> samples.items() <span class="hljs-keyword">if</span> k <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> [<span class="hljs-string">"idx"</span>, <span class="hljs-string">"sentence1"</span>, <span class="hljs-string">"sentence2"</span>]} [<span class="hljs-built_in">len</span>(x) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> samples[<span class="hljs-string">"input_ids"</span>]]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">50</span>, <span class="hljs-number">59</span>, <span class="hljs-number">47</span>, <span class="hljs-number">67</span>, <span class="hljs-number">59</span>, <span class="hljs-number">50</span>, <span class="hljs-number">62</span>, <span class="hljs-number">32</span>]</pre></div> <p>No surprise, we get samples of varying length, from 32 to 67. Dynamic padding means the samples in this batch should all be padded to a length of 67, the maximum length inside the batch. Without dynamic padding, all of the samples would have to be padded to the maximum length in the whole dataset, or the maximum length the model can accept. Let’s double-check that our <code>data_collator</code> is dynamically padding the batch properly:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>batch = data_collator(samples) {k: v.shape <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()}</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'attention_mask'</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">67</span>]), <span class="hljs-string">'input_ids'</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">67</span>]), <span class="hljs-string">'token_type_ids'</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">67</span>]), <span class="hljs-string">'labels'</span>: torch.Size([<span class="hljs-number">8</span>])}</pre></div> <p>Looking good! Now that we’ve gone from raw text to batches our model can deal with, we’re ready to fine-tune it!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Replicate the preprocessing on the GLUE SST-2 dataset. It’s a little bit different since it’s composed of single sentences instead of pairs, but the rest of what we did should look the same. For a harder challenge, try to write a preprocessing function that works on any of the GLUE tasks.</p></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter3/1?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Introduction</a> <a href="/learn/nlp-course/chapter3/3?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Fine-tuning a model with the Trainer API or Keras<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Processing the data&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;processing-the-data&quot;,&quot;url&quot;:&quot;#processing-the-data&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Loading a dataset from the Hub&quot;,&quot;id&quot;:&quot;loading-a-dataset-from-the-hub&quot;,&quot;url&quot;:&quot;#loading-a-dataset-from-the-hub&quot;},{&quot;title&quot;:&quot;Preprocessing a dataset&quot;,&quot;id&quot;:&quot;preprocessing-a-dataset&quot;,&quot;url&quot;:&quot;#preprocessing-a-dataset&quot;},{&quot;title&quot;:&quot;Dynamic padding&quot;,&quot;id&quot;:&quot;dynamic-padding&quot;,&quot;url&quot;:&quot;#dynamic-padding&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#processing-the-data" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-processing-the-data"><wbr>Processing the data</a> <a href="#loading-a-dataset-from-the-hub" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-loading-a-dataset-from-the-hub"><wbr>Loading a dataset from the <wbr>Hub</a> <a href="#preprocessing-a-dataset" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preprocessing-a-dataset"><wbr>Preprocessing a dataset</a> <a href="#dynamic-padding" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-dynamic-padding"><wbr>Dynamic padding</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter3/2" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter3/2"); } </script> <iframe name="__privateStripeMetricsController6030" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter3%2F2%3Ffw%3Dpt&amp;title=Processing%20the%20data%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:12.418Z
Fine-tuning a model with the Trainer API - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter3/3?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#fine-tuning-a-model-with-the-trainer-api)Fine-tuning a model with the Trainer API [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-3-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter3/section3.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter3/section3.ipynb) 🤗 Transformers provides a `Trainer` class to help you fine-tune any of the pretrained models it provides on your dataset. Once you’ve done all the data preprocessing work in the last section, you have just a few steps left to define the `Trainer`. The hardest part is likely to be preparing the environment to run `Trainer.train()`, as it will run very slowly on a CPU. If you don’t have a GPU set up, you can get access to free GPUs or TPUs on [Google Colab](https://colab.research.google.com/). The code examples below assume you have already executed the examples in the previous section. Here is a short summary recapping what you need: ``` from datasets import load_dataset from transformers import AutoTokenizer, DataCollatorWithPadding raw_datasets = load_dataset("glue", "mrpc") checkpoint = "bert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(checkpoint) def tokenize_function(example): return tokenizer(example["sentence1"], example["sentence2"], truncation=True) tokenized_datasets = raw_datasets.map(tokenize_function, batched=True) data_collator = DataCollatorWithPadding(tokenizer=tokenizer)``` ### [](#training)Training The first step before we can define our `Trainer` is to define a `TrainingArguments` class that will contain all the hyperparameters the `Trainer` will use for training and evaluation. The only argument you have to provide is a directory where the trained model will be saved, as well as the checkpoints along the way. For all the rest, you can leave the defaults, which should work pretty well for a basic fine-tuning. ``` from transformers import TrainingArguments training_args = TrainingArguments("test-trainer")``` 💡 If you want to automatically upload your model to the Hub during training, pass along `push_to_hub=True` in the `TrainingArguments`. We will learn more about this in [Chapter 4](/course/chapter4/3) The second step is to define our model. As in the [previous chapter](/course/chapter2), we will use the `AutoModelForSequenceClassification` class, with two labels: ``` from transformers import AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)``` You will notice that unlike in [Chapter 2](/course/chapter2), you get a warning after instantiating this pretrained model. This is because BERT has not been pretrained on classifying pairs of sentences, so the head of the pretrained model has been discarded and a new head suitable for sequence classification has been added instead. The warnings indicate that some weights were not used (the ones corresponding to the dropped pretraining head) and that some others were randomly initialized (the ones for the new head). It concludes by encouraging you to train the model, which is exactly what we are going to do now. Once we have our model, we can define a `Trainer` by passing it all the objects constructed up to now — the `model`, the `training_args`, the training and validation datasets, our `data_collator`, and our `tokenizer`: ``` from transformers import Trainer trainer = Trainer( model, training_args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=data_collator, tokenizer=tokenizer, )``` Note that when you pass the `tokenizer` as we did here, the default `data_collator` used by the `Trainer` will be a `DataCollatorWithPadding` as defined previously, so you can skip the line `data_collator=data_collator` in this call. It was still important to show you this part of the processing in section 2! To fine-tune the model on our dataset, we just have to call the `train()` method of our `Trainer`: This will start the fine-tuning (which should take a couple of minutes on a GPU) and report the training loss every 500 steps. It won’t, however, tell you how well (or badly) your model is performing. This is because: 1. We didn’t tell the `Trainer` to evaluate during training by setting `evaluation_strategy` to either `"steps"` (evaluate every `eval_steps`) or `"epoch"` (evaluate at the end of each epoch). 2. We didn’t provide the `Trainer` with a `compute_metrics()` function to calculate a metric during said evaluation (otherwise the evaluation would just have printed the loss, which is not a very intuitive number). ### [](#evaluation)Evaluation Let’s see how we can build a useful `compute_metrics()` function and use it the next time we train. The function must take an `EvalPrediction` object (which is a named tuple with a `predictions` field and a `label_ids` field) and will return a dictionary mapping strings to floats (the strings being the names of the metrics returned, and the floats their values). To get some predictions from our model, we can use the `Trainer.predict()` command: ``` predictions = trainer.predict(tokenized_datasets["validation"]) print(predictions.predictions.shape, predictions.label_ids.shape)``` The output of the `predict()` method is another named tuple with three fields: `predictions`, `label_ids`, and `metrics`. The `metrics` field will just contain the loss on the dataset passed, as well as some time metrics (how long it took to predict, in total and on average). Once we complete our `compute_metrics()` function and pass it to the `Trainer`, that field will also contain the metrics returned by `compute_metrics()`. As you can see, `predictions` is a two-dimensional array with shape 408 x 2 (408 being the number of elements in the dataset we used). Those are the logits for each element of the dataset we passed to `predict()` (as you saw in the [previous chapter](/course/chapter2), all Transformer models return logits). To transform them into predictions that we can compare to our labels, we need to take the index with the maximum value on the second axis: ``` import numpy as np preds = np.argmax(predictions.predictions, axis=-1)``` We can now compare those `preds` to the labels. To build our `compute_metric()` function, we will rely on the metrics from the 🤗 [Evaluate](https://github.com/huggingface/evaluate/) library. We can load the metrics associated with the MRPC dataset as easily as we loaded the dataset, this time with the `evaluate.load()` function. The object returned has a `compute()` method we can use to do the metric calculation: ``` import evaluate metric = evaluate.load("glue", "mrpc") metric.compute(predictions=preds, references=predictions.label_ids)``` ``` {'accuracy': 0.8578431372549019, 'f1': 0.8996539792387542}``` The exact results you get may vary, as the random initialization of the model head might change the metrics it achieved. Here, we can see our model has an accuracy of 85.78% on the validation set and an F1 score of 89.97. Those are the two metrics used to evaluate results on the MRPC dataset for the GLUE benchmark. The table in the [BERT paper](https://arxiv.org/pdf/1810.04805.pdf) reported an F1 score of 88.9 for the base model. That was the `uncased` model while we are currently using the `cased` model, which explains the better result. Wrapping everything together, we get our `compute_metrics()` function: ``` def compute_metrics(eval_preds): metric = evaluate.load("glue", "mrpc") logits, labels = eval_preds predictions = np.argmax(logits, axis=-1) return metric.compute(predictions=predictions, references=labels)``` And to see it used in action to report metrics at the end of each epoch, here is how we define a new `Trainer` with this `compute_metrics()` function: ``` training_args = TrainingArguments("test-trainer", evaluation_strategy="epoch") model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) trainer = Trainer( model, training_args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=data_collator, tokenizer=tokenizer, compute_metrics=compute_metrics, )``` Note that we create a new `TrainingArguments` with its `evaluation_strategy` set to `"epoch"` and a new model — otherwise, we would just be continuing the training of the model we have already trained. To launch a new training run, we execute: This time, it will report the validation loss and metrics at the end of each epoch on top of the training loss. Again, the exact accuracy/F1 score you reach might be a bit different from what we found, because of the random head initialization of the model, but it should be in the same ballpark. The `Trainer` will work out of the box on multiple GPUs or TPUs and provides lots of options, like mixed-precision training (use `fp16 = True` in your training arguments). We will go over everything it supports in Chapter 10. This concludes the introduction to fine-tuning using the `Trainer` API. An example of doing this for most common NLP tasks will be given in [Chapter 7](/course/chapter7), but for now let’s look at how to do the same thing in pure PyTorch. ✏️ **Try it out!** Fine-tune a model on the GLUE SST-2 dataset, using the data processing you did in section 2.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Fine-tuning a model with the Trainer API - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter3/3"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Fine-tuning a model with the Trainer API - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;fine-tuning-a-model-with-the-trainer-api&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;training&quot;,&quot;title&quot;:&quot;Training&quot;},{&quot;local&quot;:&quot;evaluation&quot;,&quot;title&quot;:&quot;Evaluation&quot;}],&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter3/3&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Fine-tuning a model with the Trainer API</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/2?fw=pt">Processing the data </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter3/3?fw=pt">Fine-tuning a model with the Trainer API or Keras </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/4?fw=pt">A full training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/5?fw=pt">Fine-tuning, Check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/6?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="fine-tuning-a-model-with-the-trainer-api" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-a-model-with-the-trainer-api"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning a model with the Trainer API</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-3-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter3/section3.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter3/section3.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/nvBXf7s7vTI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>🤗 Transformers provides a <code>Trainer</code> class to help you fine-tune any of the pretrained models it provides on your dataset. Once you’ve done all the data preprocessing work in the last section, you have just a few steps left to define the <code>Trainer</code>. The hardest part is likely to be preparing the environment to run <code>Trainer.train()</code>, as it will run very slowly on a CPU. If you don’t have a GPU set up, you can get access to free GPUs or TPUs on <a href="https://colab.research.google.com/" rel="nofollow">Google Colab</a>.</p> <p>The code examples below assume you have already executed the examples in the previous section. Here is a short summary recapping what you need:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, DataCollatorWithPadding raw_datasets = load_dataset(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mrpc"</span>) checkpoint = <span class="hljs-string">"bert-base-uncased"</span> tokenizer = AutoTokenizer.from_pretrained(checkpoint) <span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_function</span>(<span class="hljs-params">example</span>): <span class="hljs-keyword">return</span> tokenizer(example[<span class="hljs-string">"sentence1"</span>], example[<span class="hljs-string">"sentence2"</span>], truncation=<span class="hljs-literal">True</span>) tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>(tokenize_function, batched=<span class="hljs-literal">True</span>) data_collator = DataCollatorWithPadding(tokenizer=tokenizer)</pre></div> <h3 class="relative group"><a id="training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training</span></h3> <p>The first step before we can define our <code>Trainer</code> is to define a <code>TrainingArguments</code> class that will contain all the hyperparameters the <code>Trainer</code> will use for training and evaluation. The only argument you have to provide is a directory where the trained model will be saved, as well as the checkpoints along the way. For all the rest, you can leave the defaults, which should work pretty well for a basic fine-tuning.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments training_args = TrainingArguments(<span class="hljs-string">"test-trainer"</span>)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 If you want to automatically upload your model to the Hub during training, pass along <code>push_to_hub=True</code> in the <code>TrainingArguments</code>. We will learn more about this in <a href="/course/chapter4/3">Chapter 4</a></p></div> <p>The second step is to define our model. As in the <a href="/course/chapter2">previous chapter</a>, we will use the <code>AutoModelForSequenceClassification</code> class, with two labels:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>)</pre></div> <p>You will notice that unlike in <a href="/course/chapter2">Chapter 2</a>, you get a warning after instantiating this pretrained model. This is because BERT has not been pretrained on classifying pairs of sentences, so the head of the pretrained model has been discarded and a new head suitable for sequence classification has been added instead. The warnings indicate that some weights were not used (the ones corresponding to the dropped pretraining head) and that some others were randomly initialized (the ones for the new head). It concludes by encouraging you to train the model, which is exactly what we are going to do now.</p> <p>Once we have our model, we can define a <code>Trainer</code> by passing it all the objects constructed up to now — the <code>model</code>, the <code>training_args</code>, the training and validation datasets, our <code>data_collator</code>, and our <code>tokenizer</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Trainer trainer = Trainer( model, training_args, train_dataset=tokenized_datasets[<span class="hljs-string">"train"</span>], eval_dataset=tokenized_datasets[<span class="hljs-string">"validation"</span>], data_collator=data_collator, tokenizer=tokenizer, )</pre></div> <p>Note that when you pass the <code>tokenizer</code> as we did here, the default <code>data_collator</code> used by the <code>Trainer</code> will be a <code>DataCollatorWithPadding</code> as defined previously, so you can skip the line <code>data_collator=data_collator</code> in this call. It was still important to show you this part of the processing in section 2!</p> <p>To fine-tune the model on our dataset, we just have to call the <code>train()</code> method of our <code>Trainer</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.train()</pre></div> <p>This will start the fine-tuning (which should take a couple of minutes on a GPU) and report the training loss every 500 steps. It won’t, however, tell you how well (or badly) your model is performing. This is because:</p> <ol><li>We didn’t tell the <code>Trainer</code> to evaluate during training by setting <code>evaluation_strategy</code> to either <code>"steps"</code> (evaluate every <code>eval_steps</code>) or <code>"epoch"</code> (evaluate at the end of each epoch).</li> <li>We didn’t provide the <code>Trainer</code> with a <code>compute_metrics()</code> function to calculate a metric during said evaluation (otherwise the evaluation would just have printed the loss, which is not a very intuitive number).</li></ol> <h3 class="relative group"><a id="evaluation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#evaluation"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Evaluation</span></h3> <p>Let’s see how we can build a useful <code>compute_metrics()</code> function and use it the next time we train. The function must take an <code>EvalPrediction</code> object (which is a named tuple with a <code>predictions</code> field and a <code>label_ids</code> field) and will return a dictionary mapping strings to floats (the strings being the names of the metrics returned, and the floats their values). To get some predictions from our model, we can use the <code>Trainer.predict()</code> command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>predictions = trainer.predict(tokenized_datasets[<span class="hljs-string">"validation"</span>]) <span class="hljs-built_in">print</span>(predictions.predictions.shape, predictions.label_ids.shape)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-number">408</span>, <span class="hljs-number">2</span>) (<span class="hljs-number">408</span>,)</pre></div> <p>The output of the <code>predict()</code> method is another named tuple with three fields: <code>predictions</code>, <code>label_ids</code>, and <code>metrics</code>. The <code>metrics</code> field will just contain the loss on the dataset passed, as well as some time metrics (how long it took to predict, in total and on average). Once we complete our <code>compute_metrics()</code> function and pass it to the <code>Trainer</code>, that field will also contain the metrics returned by <code>compute_metrics()</code>.</p> <p>As you can see, <code>predictions</code> is a two-dimensional array with shape 408 x 2 (408 being the number of elements in the dataset we used). Those are the logits for each element of the dataset we passed to <code>predict()</code> (as you saw in the <a href="/course/chapter2">previous chapter</a>, all Transformer models return logits). To transform them into predictions that we can compare to our labels, we need to take the index with the maximum value on the second axis:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np preds = np.argmax(predictions.predictions, axis=-<span class="hljs-number">1</span>)</pre></div> <p>We can now compare those <code>preds</code> to the labels. To build our <code>compute_metric()</code> function, we will rely on the metrics from the 🤗 <a href="https://github.com/huggingface/evaluate/" rel="nofollow">Evaluate</a> library. We can load the metrics associated with the MRPC dataset as easily as we loaded the dataset, this time with the <code>evaluate.load()</code> function. The object returned has a <code>compute()</code> method we can use to do the metric calculation:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> evaluate metric = evaluate.load(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mrpc"</span>) metric.compute(predictions=preds, references=predictions.label_ids)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'accuracy'</span>: <span class="hljs-number">0.8578431372549019</span>, <span class="hljs-string">'f1'</span>: <span class="hljs-number">0.8996539792387542</span>}</pre></div> <p>The exact results you get may vary, as the random initialization of the model head might change the metrics it achieved. Here, we can see our model has an accuracy of 85.78% on the validation set and an F1 score of 89.97. Those are the two metrics used to evaluate results on the MRPC dataset for the GLUE benchmark. The table in the <a href="https://arxiv.org/pdf/1810.04805.pdf" rel="nofollow">BERT paper</a> reported an F1 score of 88.9 for the base model. That was the <code>uncased</code> model while we are currently using the <code>cased</code> model, which explains the better result.</p> <p>Wrapping everything together, we get our <code>compute_metrics()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_preds</span>): metric = evaluate.load(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mrpc"</span>) logits, labels = eval_preds predictions = np.argmax(logits, axis=-<span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> metric.compute(predictions=predictions, references=labels)</pre></div> <p>And to see it used in action to report metrics at the end of each epoch, here is how we define a new <code>Trainer</code> with this <code>compute_metrics()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>training_args = TrainingArguments(<span class="hljs-string">"test-trainer"</span>, evaluation_strategy=<span class="hljs-string">"epoch"</span>) model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>) trainer = Trainer( model, training_args, train_dataset=tokenized_datasets[<span class="hljs-string">"train"</span>], eval_dataset=tokenized_datasets[<span class="hljs-string">"validation"</span>], data_collator=data_collator, tokenizer=tokenizer, compute_metrics=compute_metrics, )</pre></div> <p>Note that we create a new <code>TrainingArguments</code> with its <code>evaluation_strategy</code> set to <code>"epoch"</code> and a new model — otherwise, we would just be continuing the training of the model we have already trained. To launch a new training run, we execute:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.train()</pre></div> <p>This time, it will report the validation loss and metrics at the end of each epoch on top of the training loss. Again, the exact accuracy/F1 score you reach might be a bit different from what we found, because of the random head initialization of the model, but it should be in the same ballpark.</p> <p>The <code>Trainer</code> will work out of the box on multiple GPUs or TPUs and provides lots of options, like mixed-precision training (use <code>fp16 = True</code> in your training arguments). We will go over everything it supports in Chapter 10.</p> <p>This concludes the introduction to fine-tuning using the <code>Trainer</code> API. An example of doing this for most common NLP tasks will be given in <a href="/course/chapter7">Chapter 7</a>, but for now let’s look at how to do the same thing in pure PyTorch.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Fine-tune a model on the GLUE SST-2 dataset, using the data processing you did in section 2.</p></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter3/2?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Processing the data</a> <a href="/learn/nlp-course/chapter3/4?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">A full training<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;fine-tuning-a-model-with-the-trainer-api&quot;,&quot;url&quot;:&quot;#fine-tuning-a-model-with-the-trainer-api&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Training&quot;,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;#training&quot;},{&quot;title&quot;:&quot;Evaluation&quot;,&quot;id&quot;:&quot;evaluation&quot;,&quot;url&quot;:&quot;#evaluation&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#fine-tuning-a-model-with-the-trainer-api" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-a-model-with-the-trainer-api"><wbr>Fine-tuning a model with the <wbr>Trainer API</a> <a href="#training" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training"><wbr>Training</a> <a href="#evaluation" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-evaluation"><wbr>Evaluation</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter3/3" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter3/3"); } </script> <iframe name="__privateStripeMetricsController7070" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter3%2F3%3Ffw%3Dpt&amp;title=Fine-tuning%20a%20model%20with%20the%20Trainer%20API%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:12.502Z
A full training - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter3/4?fw=pt
## [](#a-full-training)A full training [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-3-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter3/section4.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter3/section4.ipynb) Now we’ll see how to achieve the same results as we did in the last section without using the `Trainer` class. Again, we assume you have done the data processing in section 2. Here is a short summary covering everything you will need: ``` from datasets import load_dataset from transformers import AutoTokenizer, DataCollatorWithPadding raw_datasets = load_dataset("glue", "mrpc") checkpoint = "bert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(checkpoint) def tokenize_function(example): return tokenizer(example["sentence1"], example["sentence2"], truncation=True) tokenized_datasets = raw_datasets.map(tokenize_function, batched=True) data_collator = DataCollatorWithPadding(tokenizer=tokenizer)``` ### [](#prepare-for-training)Prepare for training Before actually writing our training loop, we will need to define a few objects. The first ones are the dataloaders we will use to iterate over batches. But before we can define those dataloaders, we need to apply a bit of postprocessing to our `tokenized_datasets`, to take care of some things that the `Trainer` did for us automatically. Specifically, we need to: - Remove the columns corresponding to values the model does not expect (like the `sentence1` and `sentence2` columns). - Rename the column `label` to `labels` (because the model expects the argument to be named `labels`). - Set the format of the datasets so they return PyTorch tensors instead of lists. Our `tokenized_datasets` has one method for each of those steps: ``` tokenized_datasets = tokenized_datasets.remove_columns(["sentence1", "sentence2", "idx"]) tokenized_datasets = tokenized_datasets.rename_column("label", "labels") tokenized_datasets.set_format("torch") tokenized_datasets["train"].column_names``` We can then check that the result only has columns that our model will accept: ``` ["attention_mask", "input_ids", "labels", "token_type_ids"]``` Now that this is done, we can easily define our dataloaders: ``` from torch.utils.data import DataLoader train_dataloader = DataLoader( tokenized_datasets["train"], shuffle=True, batch_size=8, collate_fn=data_collator ) eval_dataloader = DataLoader( tokenized_datasets["validation"], batch_size=8, collate_fn=data_collator )``` To quickly check there is no mistake in the data processing, we can inspect a batch like this: ``` for batch in train_dataloader: break {k: v.shape for k, v in batch.items()}``` ``` {'attention_mask': torch.Size([8, 65]), 'input_ids': torch.Size([8, 65]), 'labels': torch.Size([8]), 'token_type_ids': torch.Size([8, 65])}``` Note that the actual shapes will probably be slightly different for you since we set `shuffle=True` for the training dataloader and we are padding to the maximum length inside the batch. Now that we’re completely finished with data preprocessing (a satisfying yet elusive goal for any ML practitioner), let’s turn to the model. We instantiate it exactly as we did in the previous section: ``` from transformers import AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)``` To make sure that everything will go smoothly during training, we pass our batch to this model: ``` outputs = model(**batch) print(outputs.loss, outputs.logits.shape)``` ``` tensor(0.5441, grad_fn=<NllLossBackward>) torch.Size([8, 2])``` All 🤗 Transformers models will return the loss when `labels` are provided, and we also get the logits (two for each input in our batch, so a tensor of size 8 x 2). We’re almost ready to write our training loop! We’re just missing two things: an optimizer and a learning rate scheduler. Since we are trying to replicate what the `Trainer` was doing by hand, we will use the same defaults. The optimizer used by the `Trainer` is `AdamW`, which is the same as Adam, but with a twist for weight decay regularization (see [“Decoupled Weight Decay Regularization”](https://arxiv.org/abs/1711.05101) by Ilya Loshchilov and Frank Hutter): ``` from transformers import AdamW optimizer = AdamW(model.parameters(), lr=5e-5)``` Finally, the learning rate scheduler used by default is just a linear decay from the maximum value (5e-5) to 0. To properly define it, we need to know the number of training steps we will take, which is the number of epochs we want to run multiplied by the number of training batches (which is the length of our training dataloader). The `Trainer` uses three epochs by default, so we will follow that: ``` from transformers import get_scheduler num_epochs = 3 num_training_steps = num_epochs * len(train_dataloader) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, ) print(num_training_steps)``` ### [](#the-training-loop)The training loop One last thing: we will want to use the GPU if we have access to one (on a CPU, training might take several hours instead of a couple of minutes). To do this, we define a `device` we will put our model and our batches on: ``` import torch device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") model.to(device) device``` We are now ready to train! To get some sense of when training will be finished, we add a progress bar over our number of training steps, using the `tqdm` library: ``` from tqdm.auto import tqdm progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dataloader: batch = {k: v.to(device) for k, v in batch.items()} outputs = model(**batch) loss = outputs.loss loss.backward() optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1)``` You can see that the core of the training loop looks a lot like the one in the introduction. We didn’t ask for any reporting, so this training loop will not tell us anything about how the model fares. We need to add an evaluation loop for that. ### [](#the-evaluation-loop)The evaluation loop As we did earlier, we will use a metric provided by the 🤗 Evaluate library. We’ve already seen the `metric.compute()` method, but metrics can actually accumulate batches for us as we go over the prediction loop with the method `add_batch()`. Once we have accumulated all the batches, we can get the final result with `metric.compute()`. Here’s how to implement all of this in an evaluation loop: ``` import evaluate metric = evaluate.load("glue", "mrpc") model.eval() for batch in eval_dataloader: batch = {k: v.to(device) for k, v in batch.items()} with torch.no_grad(): outputs = model(**batch) logits = outputs.logits predictions = torch.argmax(logits, dim=-1) metric.add_batch(predictions=predictions, references=batch["labels"]) metric.compute()``` ``` {'accuracy': 0.8431372549019608, 'f1': 0.8907849829351535}``` Again, your results will be slightly different because of the randomness in the model head initialization and the data shuffling, but they should be in the same ballpark. ✏️ **Try it out!** Modify the previous training loop to fine-tune your model on the SST-2 dataset. ### [](#supercharge-your-training-loop-with-accelerate)Supercharge your training loop with 🤗 Accelerate The training loop we defined earlier works fine on a single CPU or GPU. But using the [🤗 Accelerate](https://github.com/huggingface/accelerate) library, with just a few adjustments we can enable distributed training on multiple GPUs or TPUs. Starting from the creation of the training and validation dataloaders, here is what our manual training loop looks like: ``` from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) optimizer = AdamW(model.parameters(), lr=3e-5) device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") model.to(device) num_epochs = 3 num_training_steps = num_epochs * len(train_dataloader) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, ) progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dataloader: batch = {k: v.to(device) for k, v in batch.items()} outputs = model(**batch) loss = outputs.loss loss.backward() optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1)``` And here are the changes: ``` + from accelerate import Accelerator from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler + accelerator = Accelerator() model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) optimizer = AdamW(model.parameters(), lr=3e-5) - device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") - model.to(device) + train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( + train_dataloader, eval_dataloader, model, optimizer + ) num_epochs = 3 num_training_steps = num_epochs * len(train_dataloader) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps ) progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dataloader: - batch = {k: v.to(device) for k, v in batch.items()} outputs = model(**batch) loss = outputs.loss - loss.backward() + accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1)``` The first line to add is the import line. The second line instantiates an `Accelerator` object that will look at the environment and initialize the proper distributed setup. 🤗 Accelerate handles the device placement for you, so you can remove the lines that put the model on the device (or, if you prefer, change them to use `accelerator.device` instead of `device`). Then the main bulk of the work is done in the line that sends the dataloaders, the model, and the optimizer to `accelerator.prepare()`. This will wrap those objects in the proper container to make sure your distributed training works as intended. The remaining changes to make are removing the line that puts the batch on the `device` (again, if you want to keep this you can just change it to use `accelerator.device`) and replacing `loss.backward()` with `accelerator.backward(loss)`. ⚠️ In order to benefit from the speed-up offered by Cloud TPUs, we recommend padding your samples to a fixed length with the \`padding="max\_length"\` and \`max\_length\` arguments of the tokenizer. If you’d like to copy and paste it to play around, here’s what the complete training loop looks like with 🤗 Accelerate: ``` from accelerate import Accelerator from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler accelerator = Accelerator() model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) optimizer = AdamW(model.parameters(), lr=3e-5) train_dl, eval_dl, model, optimizer = accelerator.prepare( train_dataloader, eval_dataloader, model, optimizer ) num_epochs = 3 num_training_steps = num_epochs * len(train_dl) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, ) progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dl: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1)``` Putting this in a `train.py` script will make that script runnable on any kind of distributed setup. To try it out in your distributed setup, run the command: which will prompt you to answer a few questions and dump your answers in a configuration file used by this command: ``` accelerate launch train.py``` which will launch the distributed training. If you want to try this in a Notebook (for instance, to test it with TPUs on Colab), just paste the code in a `training_function()` and run a last cell with: ``` from accelerate import notebook_launcher notebook_launcher(training_function)``` You can find more examples in the [🤗 Accelerate repo](https://github.com/huggingface/accelerate/tree/main/examples).
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="A full training - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter3/4"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>A full training - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;a-full-training&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;prepare-for-training&quot;,&quot;title&quot;:&quot;Prepare for training&quot;},{&quot;local&quot;:&quot;the-training-loop&quot;,&quot;title&quot;:&quot;The training loop&quot;},{&quot;local&quot;:&quot;the-evaluation-loop&quot;,&quot;title&quot;:&quot;The evaluation loop&quot;},{&quot;local&quot;:&quot;supercharge-your-training-loop-with-accelerate&quot;,&quot;title&quot;:&quot;Supercharge your training loop with 🤗 Accelerate&quot;}],&quot;title&quot;:&quot;A full training&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter3/4&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;A full training&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">A full training</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/2?fw=pt">Processing the data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/3?fw=pt">Fine-tuning a model with the Trainer API or Keras </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter3/4?fw=pt">A full training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/5?fw=pt">Fine-tuning, Check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/6?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="a-full-training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#a-full-training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>A full training</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-3-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter3/section4.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter3/section4.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/Dh9CL8fyG80" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Now we’ll see how to achieve the same results as we did in the last section without using the <code>Trainer</code> class. Again, we assume you have done the data processing in section 2. Here is a short summary covering everything you will need:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, DataCollatorWithPadding raw_datasets = load_dataset(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mrpc"</span>) checkpoint = <span class="hljs-string">"bert-base-uncased"</span> tokenizer = AutoTokenizer.from_pretrained(checkpoint) <span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_function</span>(<span class="hljs-params">example</span>): <span class="hljs-keyword">return</span> tokenizer(example[<span class="hljs-string">"sentence1"</span>], example[<span class="hljs-string">"sentence2"</span>], truncation=<span class="hljs-literal">True</span>) tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>(tokenize_function, batched=<span class="hljs-literal">True</span>) data_collator = DataCollatorWithPadding(tokenizer=tokenizer)</pre></div> <h3 class="relative group"><a id="prepare-for-training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#prepare-for-training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Prepare for training</span></h3> <p>Before actually writing our training loop, we will need to define a few objects. The first ones are the dataloaders we will use to iterate over batches. But before we can define those dataloaders, we need to apply a bit of postprocessing to our <code>tokenized_datasets</code>, to take care of some things that the <code>Trainer</code> did for us automatically. Specifically, we need to:</p> <ul><li>Remove the columns corresponding to values the model does not expect (like the <code>sentence1</code> and <code>sentence2</code> columns).</li> <li>Rename the column <code>label</code> to <code>labels</code> (because the model expects the argument to be named <code>labels</code>).</li> <li>Set the format of the datasets so they return PyTorch tensors instead of lists.</li></ul> <p>Our <code>tokenized_datasets</code> has one method for each of those steps:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_datasets = tokenized_datasets.remove_columns([<span class="hljs-string">"sentence1"</span>, <span class="hljs-string">"sentence2"</span>, <span class="hljs-string">"idx"</span>]) tokenized_datasets = tokenized_datasets.rename_column(<span class="hljs-string">"label"</span>, <span class="hljs-string">"labels"</span>) tokenized_datasets.set_format(<span class="hljs-string">"torch"</span>) tokenized_datasets[<span class="hljs-string">"train"</span>].column_names</pre></div> <p>We can then check that the result only has columns that our model will accept:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">"attention_mask"</span>, <span class="hljs-string">"input_ids"</span>, <span class="hljs-string">"labels"</span>, <span class="hljs-string">"token_type_ids"</span>]</pre></div> <p>Now that this is done, we can easily define our dataloaders:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader train_dataloader = DataLoader( tokenized_datasets[<span class="hljs-string">"train"</span>], shuffle=<span class="hljs-literal">True</span>, batch_size=<span class="hljs-number">8</span>, collate_fn=data_collator ) eval_dataloader = DataLoader( tokenized_datasets[<span class="hljs-string">"validation"</span>], batch_size=<span class="hljs-number">8</span>, collate_fn=data_collator )</pre></div> <p>To quickly check there is no mistake in the data processing, we can inspect a batch like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader: <span class="hljs-keyword">break</span> {k: v.shape <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()}</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'attention_mask'</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">65</span>]), <span class="hljs-string">'input_ids'</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">65</span>]), <span class="hljs-string">'labels'</span>: torch.Size([<span class="hljs-number">8</span>]), <span class="hljs-string">'token_type_ids'</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">65</span>])}</pre></div> <p>Note that the actual shapes will probably be slightly different for you since we set <code>shuffle=True</code> for the training dataloader and we are padding to the maximum length inside the batch.</p> <p>Now that we’re completely finished with data preprocessing (a satisfying yet elusive goal for any ML practitioner), let’s turn to the model. We instantiate it exactly as we did in the previous section:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>)</pre></div> <p>To make sure that everything will go smoothly during training, we pass our batch to this model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>outputs = model(**batch) <span class="hljs-built_in">print</span>(outputs.loss, outputs.logits.shape)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tensor(<span class="hljs-number">0.5441</span>, grad_fn=&lt;NllLossBackward&gt;) torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">2</span>])</pre></div> <p>All 🤗 Transformers models will return the loss when <code>labels</code> are provided, and we also get the logits (two for each input in our batch, so a tensor of size 8 x 2).</p> <p>We’re almost ready to write our training loop! We’re just missing two things: an optimizer and a learning rate scheduler. Since we are trying to replicate what the <code>Trainer</code> was doing by hand, we will use the same defaults. The optimizer used by the <code>Trainer</code> is <code>AdamW</code>, which is the same as Adam, but with a twist for weight decay regularization (see <a href="https://arxiv.org/abs/1711.05101" rel="nofollow">“Decoupled Weight Decay Regularization”</a> by Ilya Loshchilov and Frank Hutter):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AdamW optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">5e-5</span>)</pre></div> <p>Finally, the learning rate scheduler used by default is just a linear decay from the maximum value (5e-5) to 0. To properly define it, we need to know the number of training steps we will take, which is the number of epochs we want to run multiplied by the number of training batches (which is the length of our training dataloader). The <code>Trainer</code> uses three epochs by default, so we will follow that:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler num_epochs = <span class="hljs-number">3</span> num_training_steps = num_epochs * <span class="hljs-built_in">len</span>(train_dataloader) lr_scheduler = get_scheduler( <span class="hljs-string">"linear"</span>, optimizer=optimizer, num_warmup_steps=<span class="hljs-number">0</span>, num_training_steps=num_training_steps, ) <span class="hljs-built_in">print</span>(num_training_steps)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">1377</span></pre></div> <h3 class="relative group"><a id="the-training-loop" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-training-loop"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The training loop</span></h3> <p>One last thing: we will want to use the GPU if we have access to one (on a CPU, training might take several hours instead of a couple of minutes). To do this, we define a <code>device</code> we will put our model and our batches on:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch device = torch.device(<span class="hljs-string">"cuda"</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">"cpu"</span>) model.to(device) device</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>device(<span class="hljs-built_in">type</span>=<span class="hljs-string">'cuda'</span>)</pre></div> <p>We are now ready to train! To get some sense of when training will be finished, we add a progress bar over our number of training steps, using the <code>tqdm</code> library:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps)) model.train() <span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs): <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader: batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()} outputs = model(**batch) loss = outputs.loss loss.backward() optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(<span class="hljs-number">1</span>)</pre></div> <p>You can see that the core of the training loop looks a lot like the one in the introduction. We didn’t ask for any reporting, so this training loop will not tell us anything about how the model fares. We need to add an evaluation loop for that.</p> <h3 class="relative group"><a id="the-evaluation-loop" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-evaluation-loop"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The evaluation loop</span></h3> <p>As we did earlier, we will use a metric provided by the 🤗 Evaluate library. We’ve already seen the <code>metric.compute()</code> method, but metrics can actually accumulate batches for us as we go over the prediction loop with the method <code>add_batch()</code>. Once we have accumulated all the batches, we can get the final result with <code>metric.compute()</code>. Here’s how to implement all of this in an evaluation loop:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> evaluate metric = evaluate.load(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mrpc"</span>) model.<span class="hljs-built_in">eval</span>() <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> eval_dataloader: batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()} <span class="hljs-keyword">with</span> torch.no_grad(): outputs = model(**batch) logits = outputs.logits predictions = torch.argmax(logits, dim=-<span class="hljs-number">1</span>) metric.add_batch(predictions=predictions, references=batch[<span class="hljs-string">"labels"</span>]) metric.compute()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'accuracy'</span>: <span class="hljs-number">0.8431372549019608</span>, <span class="hljs-string">'f1'</span>: <span class="hljs-number">0.8907849829351535</span>}</pre></div> <p>Again, your results will be slightly different because of the randomness in the model head initialization and the data shuffling, but they should be in the same ballpark.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Modify the previous training loop to fine-tune your model on the SST-2 dataset.</p></div> <h3 class="relative group"><a id="supercharge-your-training-loop-with-accelerate" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#supercharge-your-training-loop-with-accelerate"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Supercharge your training loop with 🤗 Accelerate</span></h3> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/s7dy8QRgjJ0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The training loop we defined earlier works fine on a single CPU or GPU. But using the <a href="https://github.com/huggingface/accelerate" rel="nofollow">🤗 Accelerate</a> library, with just a few adjustments we can enable distributed training on multiple GPUs or TPUs. Starting from the creation of the training and validation dataloaders, here is what our manual training loop looks like:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AdamW, AutoModelForSequenceClassification, get_scheduler model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>) optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">3e-5</span>) device = torch.device(<span class="hljs-string">"cuda"</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">"cpu"</span>) model.to(device) num_epochs = <span class="hljs-number">3</span> num_training_steps = num_epochs * <span class="hljs-built_in">len</span>(train_dataloader) lr_scheduler = get_scheduler( <span class="hljs-string">"linear"</span>, optimizer=optimizer, num_warmup_steps=<span class="hljs-number">0</span>, num_training_steps=num_training_steps, ) progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps)) model.train() <span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs): <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader: batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()} outputs = model(**batch) loss = outputs.loss loss.backward() optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(<span class="hljs-number">1</span>)</pre></div> <p>And here are the changes:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-addition">+ from accelerate import Accelerator</span> from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler <span class="hljs-addition">+ accelerator = Accelerator()</span> model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) optimizer = AdamW(model.parameters(), lr=3e-5) <span class="hljs-deletion">- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")</span> <span class="hljs-deletion">- model.to(device)</span> <span class="hljs-addition">+ train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare(</span> <span class="hljs-addition">+ train_dataloader, eval_dataloader, model, optimizer</span> <span class="hljs-addition">+ )</span> num_epochs = 3 num_training_steps = num_epochs * len(train_dataloader) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps ) progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dataloader: <span class="hljs-deletion">- batch = {k: v.to(device) for k, v in batch.items()}</span> outputs = model(**batch) loss = outputs.loss <span class="hljs-deletion">- loss.backward()</span> <span class="hljs-addition">+ accelerator.backward(loss)</span> optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1)</pre></div> <p>The first line to add is the import line. The second line instantiates an <code>Accelerator</code> object that will look at the environment and initialize the proper distributed setup. 🤗 Accelerate handles the device placement for you, so you can remove the lines that put the model on the device (or, if you prefer, change them to use <code>accelerator.device</code> instead of <code>device</code>).</p> <p>Then the main bulk of the work is done in the line that sends the dataloaders, the model, and the optimizer to <code>accelerator.prepare()</code>. This will wrap those objects in the proper container to make sure your distributed training works as intended. The remaining changes to make are removing the line that puts the batch on the <code>device</code> (again, if you want to keep this you can just change it to use <code>accelerator.device</code>) and replacing <code>loss.backward()</code> with <code>accelerator.backward(loss)</code>.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400">⚠️ In order to benefit from the speed-up offered by Cloud TPUs, we recommend padding your samples to a fixed length with the `padding="max_length"` and `max_length` arguments of the tokenizer.</div> <p>If you’d like to copy and paste it to play around, here’s what the complete training loop looks like with 🤗 Accelerate:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AdamW, AutoModelForSequenceClassification, get_scheduler accelerator = Accelerator() model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>) optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">3e-5</span>) train_dl, eval_dl, model, optimizer = accelerator.prepare( train_dataloader, eval_dataloader, model, optimizer ) num_epochs = <span class="hljs-number">3</span> num_training_steps = num_epochs * <span class="hljs-built_in">len</span>(train_dl) lr_scheduler = get_scheduler( <span class="hljs-string">"linear"</span>, optimizer=optimizer, num_warmup_steps=<span class="hljs-number">0</span>, num_training_steps=num_training_steps, ) progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps)) model.train() <span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs): <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dl: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(<span class="hljs-number">1</span>)</pre></div> <p>Putting this in a <code>train.py</code> script will make that script runnable on any kind of distributed setup. To try it out in your distributed setup, run the command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>accelerate config</pre></div> <p>which will prompt you to answer a few questions and dump your answers in a configuration file used by this command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>accelerate <span class="hljs-built_in">launch</span> train.py</pre></div> <p>which will launch the distributed training.</p> <p>If you want to try this in a Notebook (for instance, to test it with TPUs on Colab), just paste the code in a <code>training_function()</code> and run a last cell with:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> notebook_launcher notebook_launcher(training_function)</pre></div> <p>You can find more examples in the <a href="https://github.com/huggingface/accelerate/tree/main/examples" rel="nofollow">🤗 Accelerate repo</a>.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter3/3?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Fine-tuning a model with the Trainer API or Keras</a> <a href="/learn/nlp-course/chapter3/5?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Fine-tuning, Check!<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;A full training&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;a-full-training&quot;,&quot;url&quot;:&quot;#a-full-training&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Prepare for training&quot;,&quot;id&quot;:&quot;prepare-for-training&quot;,&quot;url&quot;:&quot;#prepare-for-training&quot;},{&quot;title&quot;:&quot;The training loop&quot;,&quot;id&quot;:&quot;the-training-loop&quot;,&quot;url&quot;:&quot;#the-training-loop&quot;},{&quot;title&quot;:&quot;The evaluation loop&quot;,&quot;id&quot;:&quot;the-evaluation-loop&quot;,&quot;url&quot;:&quot;#the-evaluation-loop&quot;},{&quot;title&quot;:&quot;Supercharge your training loop with 🤗 Accelerate&quot;,&quot;id&quot;:&quot;supercharge-your-training-loop-with-accelerate&quot;,&quot;url&quot;:&quot;#supercharge-your-training-loop-with-accelerate&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#a-full-training" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-a-full-training"><wbr>A full training</a> <a href="#prepare-for-training" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-prepare-for-training"><wbr>Prepare for training</a> <a href="#the-training-loop" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-training-loop"><wbr>The training loop</a> <a href="#the-evaluation-loop" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-evaluation-loop"><wbr>The evaluation loop</a> <a href="#supercharge-your-training-loop-with-accelerate" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-supercharge-your-training-loop-with-accelerate"><wbr>Supercharge your training loop with 🤗 <wbr>Accelerate</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter3/4" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter3/4"); } </script> <iframe name="__privateStripeMetricsController8030" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter3%2F4%3Ffw%3Dpt&amp;title=A%20full%20training%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:12.683Z
Fine-tuning, Check! - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter3/5?fw=pt
![Hugging Face's logo](/front/assets/huggingface_logo-noborder.svg) 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 [Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#fine-tuning-check)Fine-tuning, Check! [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-3-questions) That was fun! In the first two chapters you learned about models and tokenizers, and now you know how to fine-tune them for your own data. To recap, in this chapter you: - Learned about datasets in the [Hub](https://huggingface.co/datasets) - Learned how to load and preprocess datasets, including using dynamic padding and collators - Implemented your own fine-tuning and evaluation of a model - Implemented a lower-level training loop - Used 🤗 Accelerate to easily adapt your training loop so it works for multiple GPUs or TPUs
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Fine-tuning, Check! - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter3/5"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Fine-tuning, Check! - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;fine-tuning-check&quot;,&quot;title&quot;:&quot;Fine-tuning, Check!&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter3/5&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Fine-tuning, Check!&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Fine-tuning, Check!</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/2?fw=pt">Processing the data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/3?fw=pt">Fine-tuning a model with the Trainer API or Keras </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/4?fw=pt">A full training </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter3/5?fw=pt">Fine-tuning, Check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/6?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="fine-tuning-check" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-check"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning, Check!</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-3-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>That was fun! In the first two chapters you learned about models and tokenizers, and now you know how to fine-tune them for your own data. To recap, in this chapter you:</p> <ul><li>Learned about datasets in the <a href="https://huggingface.co/datasets" rel="nofollow">Hub</a></li> <li>Learned how to load and preprocess datasets, including using dynamic padding and collators</li> <li>Implemented your own fine-tuning and evaluation of a model</li> <li>Implemented a lower-level training loop</li> <li>Used 🤗 Accelerate to easily adapt your training loop so it works for multiple GPUs or TPUs</li></ul> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter3/4?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>A full training</a> <a href="/learn/nlp-course/chapter3/6?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">End-of-chapter quiz<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;fine-tuning-check&quot;,&quot;url&quot;:&quot;#fine-tuning-check&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#fine-tuning-check" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-check"><wbr>Fine-tuning, <wbr>Check!</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter3/5" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter3/5"); } </script> <iframe name="__privateStripeMetricsController5050" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter3%2F5%3Ffw%3Dpt&amp;title=Fine-tuning%2C%20Check!%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:13.154Z
End-of-chapter quiz - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter3/6?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#end-of-chapter-quiz)End-of-chapter quiz [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-3-questions) Test what you learned in this chapter! ### 1\. The `emotion` dataset contains Twitter messages labeled with emotions. Search for it in the [Hub](https://huggingface.co/datasets), and read the dataset card. Which of these is not one of its basic emotions? ### [](#2.-search-for-the-<code>ar_sarcasm</code>-dataset-in-the-hub.-which-task-does-it-support?)2\. Search for the `ar_sarcasm` dataset in the [Hub](https://huggingface.co/datasets). Which task does it support? ### [](#3.-how-does-the-bert-model-expect-a-pair-of-sentences-to-be-processed?)3\. How does the BERT model expect a pair of sentences to be processed? ### [](#4.-what-are-the-benefits-of-the-<code>dataset.map()</code>-method?)4\. What are the benefits of the `Dataset.map()` method? ### [](#5.-what-does-dynamic-padding-mean?)5\. What does dynamic padding mean? ### [](#6.-what-is-the-purpose-of-a-collate-function?)6\. What is the purpose of a collate function? ### [](#7.-what-happens-when-you-instantiate-one-of-the-<code>automodelforxxx</code>-classes-with-a-pretrained-language-model-(such-as-<code>bert-base-uncased</code>)-that-corresponds-to-a-different-task-than-the-one-for-which-it-was-trained?)7\. What happens when you instantiate one of the `AutoModelForXxx` classes with a pretrained language model (such as `bert-base-uncased`) that corresponds to a different task than the one for which it was trained? ### [](#8.-what’s-the-purpose-of-<code>trainingarguments</code>?)8\. What’s the purpose of `TrainingArguments`? ### [](#9.-why-should-you-use-the-🤗-accelerate-library?)9\. Why should you use the 🤗 Accelerate library?
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="End-of-chapter quiz - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter3/6"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>End-of-chapter quiz - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;end-of-chapter-quiz&quot;,&quot;title&quot;:&quot;End-of-chapter quiz&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter3/6&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;End-of-chapter quiz&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">End-of-chapter quiz</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/2?fw=pt">Processing the data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/3?fw=pt">Fine-tuning a model with the Trainer API or Keras </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/4?fw=pt">A full training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter3/5?fw=pt">Fine-tuning, Check! </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter3/6?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="end-of-chapter-quiz" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#end-of-chapter-quiz"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>End-of-chapter quiz</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-3-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>Test what you learned in this chapter!</p> <h3>1. The <code>emotion</code> dataset contains Twitter messages labeled with emotions. Search for it in the <a href="https://huggingface.co/datasets" rel="nofollow">Hub</a>, and read the dataset card. Which of these is not one of its basic emotions?</h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Joy</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Love</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Confusion</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Surprise</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="2.-search-for-the-<code>ar_sarcasm</code>-dataset-in-the-hub.-which-task-does-it-support?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#2.-search-for-the-<code>ar_sarcasm</code>-dataset-in-the-hub.-which-task-does-it-support?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>2. Search for the <code>ar_sarcasm</code> dataset in the <a href="https://huggingface.co/datasets" rel="nofollow">Hub</a>. Which task does it support?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Sentiment classification</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Machine translation</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Named entity recognition</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Question answering</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="3.-how-does-the-bert-model-expect-a-pair-of-sentences-to-be-processed?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#3.-how-does-the-bert-model-expect-a-pair-of-sentences-to-be-processed?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>3. How does the BERT model expect a pair of sentences to be processed?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Tokens_of_sentence_1 [SEP] Tokens_of_sentence_2</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> [CLS] Tokens_of_sentence_1 Tokens_of_sentence_2</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> [CLS] Tokens_of_sentence_1 [SEP] Tokens_of_sentence_2 [SEP]</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> [CLS] Tokens_of_sentence_1 [SEP] Tokens_of_sentence_2</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="4.-what-are-the-benefits-of-the-<code>dataset.map()</code>-method?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#4.-what-are-the-benefits-of-the-<code>dataset.map()</code>-method?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>4. What are the benefits of the <code>Dataset.map()</code> method?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> The results of the function are cached, so it won't take any time if we re-execute the code.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It can apply multiprocessing to go faster than applying the function on each element of the dataset.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It does not load the whole dataset into memory, saving the results as soon as one element is processed.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="5.-what-does-dynamic-padding-mean?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#5.-what-does-dynamic-padding-mean?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>5. What does dynamic padding mean?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It's when you pad the inputs for each batch to the maximum length in the whole dataset.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It's when you pad your inputs when the batch is created, to the maximum length of the sentences inside that batch.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It's when you pad your inputs so that each sentence has the same number of tokens as the previous one in the dataset.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="6.-what-is-the-purpose-of-a-collate-function?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#6.-what-is-the-purpose-of-a-collate-function?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>6. What is the purpose of a collate function?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It ensures all the sequences in the dataset have the same length.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It puts together all the samples in a batch.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It preprocesses the whole dataset.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> It truncates the sequences in the dataset.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="7.-what-happens-when-you-instantiate-one-of-the-<code>automodelforxxx</code>-classes-with-a-pretrained-language-model-(such-as-<code>bert-base-uncased</code>)-that-corresponds-to-a-different-task-than-the-one-for-which-it-was-trained?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#7.-what-happens-when-you-instantiate-one-of-the-<code>automodelforxxx</code>-classes-with-a-pretrained-language-model-(such-as-<code>bert-base-uncased</code>)-that-corresponds-to-a-different-task-than-the-one-for-which-it-was-trained?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>7. What happens when you instantiate one of the <code>AutoModelForXxx</code> classes with a pretrained language model (such as <code>bert-base-uncased</code>) that corresponds to a different task than the one for which it was trained?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Nothing, but you get a warning.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> The head of the pretrained model is discarded and a new head suitable for the task is inserted instead.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> The head of the pretrained model is discarded.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Nothing, since the model can still be fine-tuned for the different task.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="8.-what’s-the-purpose-of-<code>trainingarguments</code>?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#8.-what’s-the-purpose-of-<code>trainingarguments</code>?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>8. What’s the purpose of <code>TrainingArguments</code>?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It contains all the hyperparameters used for training and evaluation with the <code>Trainer</code>.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It specifies the size of the model.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It just contains the hyperparameters used for evaluation.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> It just contains the hyperparameters used for training.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="9.-why-should-you-use-the-🤗-accelerate-library?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#9.-why-should-you-use-the-🤗-accelerate-library?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>9. Why should you use the 🤗 Accelerate library?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It provides access to faster models.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It provides a high-level API so I don't have to implement my own training loop.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It makes our training loops work on distributed strategies</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> It provides more optimization functions.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter3/5?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Fine-tuning, Check!</a> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isLoggedIn&quot;:false,&quot;nextChapterUrl&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;,&quot;quizChapter&quot;:3}" data-target="CourseCompleteButton"><div class="relative ml-auto"> <button class="btn btn-lg" disabled=""><svg class="mr-2" width="1em" height="1em" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0 16C0 7.16344 7.16344 0 16 0V0C24.8366 0 32 7.16344 32 16V16C32 24.8366 24.8366 32 16 32V32C7.16344 32 0 24.8366 0 16V16Z" fill="#D9FFD2"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M23.5607 10.9393C24.1464 11.5251 24.1464 12.4749 23.5607 13.0607L14.5 22.1213L8.93934 16.5607C8.35355 15.9749 8.35355 15.0251 8.93934 14.4393C9.52513 13.8536 10.4749 13.8536 11.0607 14.4393L14.5 17.8787L21.4393 10.9393C22.0251 10.3536 22.9749 10.3536 23.5607 10.9393Z" fill="#188B3F"></path></svg> Complete Chapter</button></div></div> <a href="/learn/nlp-course/chapter4/1?fw=pt" class="hidden">Next chapter</a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;id&quot;:&quot;end-of-chapter-quiz&quot;,&quot;url&quot;:&quot;#end-of-chapter-quiz&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#end-of-chapter-quiz" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-end-of-chapter-quiz"><wbr>End-of-chapter quiz</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter3/6" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter3/6"); } </script> <iframe name="__privateStripeMetricsController6840" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter3%2F6%3Ffw%3Dpt&amp;title=End-of-chapter%20quiz%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=b18234d3-6427-47e7-9e92-f46fda4d6d57763574&amp;sid=12934d06-8571-4c1a-80e5-1801128cfc01bc5052&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:13.348Z
The Hugging Face Hub - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter4/1?fw=pt
3\. Fine-tuning a pretrained model 4\. Sharing models and tokenizers 5\. The 🤗 Datasets library 6\. The 🤗 Tokenizers library 9\. Building and sharing demos new ## [](#the-hugging-face-hub)The Hugging Face Hub [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-4-questions) The [Hugging Face Hub](https://huggingface.co/) –- our main website –- is a central platform that enables anyone to discover, use, and contribute new state-of-the-art models and datasets. It hosts a wide variety of models, with more than 10,000 publicly available. We’ll focus on the models in this chapter, and take a look at the datasets in Chapter 5. The models in the Hub are not limited to 🤗 Transformers or even NLP. There are models from [Flair](https://github.com/flairNLP/flair) and [AllenNLP](https://github.com/allenai/allennlp) for NLP, [Asteroid](https://github.com/asteroid-team/asteroid) and [pyannote](https://github.com/pyannote/pyannote-audio) for speech, and [timm](https://github.com/rwightman/pytorch-image-models) for vision, to name a few. Each of these models is hosted as a Git repository, which allows versioning and reproducibility. Sharing a model on the Hub means opening it up to the community and making it accessible to anyone looking to easily use it, in turn eliminating their need to train a model on their own and simplifying sharing and usage. Additionally, sharing a model on the Hub automatically deploys a hosted Inference API for that model. Anyone in the community is free to test it out directly on the model’s page, with custom inputs and appropriate widgets. The best part is that sharing and using any public model on the Hub is completely free! [Paid plans](https://huggingface.co/pricing) also exist if you wish to share models privately. The video below shows how to navigate the Hub. Having a huggingface.co account is required to follow along this part, as we’ll be creating and managing repositories on the Hugging Face Hub: [create an account](https://huggingface.co/join)
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="The Hugging Face Hub - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter4/1"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>The Hugging Face Hub - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;the-hugging-face-hub&quot;,&quot;title&quot;:&quot;The Hugging Face Hub&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter4/1&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;The Hugging Face Hub&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">The Hugging Face Hub</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter4/1?fw=pt">The Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/2?fw=pt">Using pretrained models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/3?fw=pt">Sharing pretrained models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/4?fw=pt">Building a model card </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/5?fw=pt">Part 1 completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/6?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="the-hugging-face-hub" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-hugging-face-hub"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The Hugging Face Hub</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-4-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>The <a href="https://huggingface.co/" rel="nofollow">Hugging Face Hub</a> –- our main website –- is a central platform that enables anyone to discover, use, and contribute new state-of-the-art models and datasets. It hosts a wide variety of models, with more than 10,000 publicly available. We’ll focus on the models in this chapter, and take a look at the datasets in Chapter 5.</p> <p>The models in the Hub are not limited to 🤗 Transformers or even NLP. There are models from <a href="https://github.com/flairNLP/flair" rel="nofollow">Flair</a> and <a href="https://github.com/allenai/allennlp" rel="nofollow">AllenNLP</a> for NLP, <a href="https://github.com/asteroid-team/asteroid" rel="nofollow">Asteroid</a> and <a href="https://github.com/pyannote/pyannote-audio" rel="nofollow">pyannote</a> for speech, and <a href="https://github.com/rwightman/pytorch-image-models" rel="nofollow">timm</a> for vision, to name a few.</p> <p>Each of these models is hosted as a Git repository, which allows versioning and reproducibility. Sharing a model on the Hub means opening it up to the community and making it accessible to anyone looking to easily use it, in turn eliminating their need to train a model on their own and simplifying sharing and usage.</p> <p>Additionally, sharing a model on the Hub automatically deploys a hosted Inference API for that model. Anyone in the community is free to test it out directly on the model’s page, with custom inputs and appropriate widgets.</p> <p>The best part is that sharing and using any public model on the Hub is completely free! <a href="https://huggingface.co/pricing" rel="nofollow">Paid plans</a> also exist if you wish to share models privately.</p> <p>The video below shows how to navigate the Hub.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/XvSGPZFEjDY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Having a huggingface.co account is required to follow along this part, as we’ll be creating and managing repositories on the Hugging Face Hub: <a href="https://huggingface.co/join" rel="nofollow">create an account</a></p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter3/6?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>End-of-chapter quiz</a> <a href="/learn/nlp-course/chapter4/2?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Using pretrained models<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;the-hugging-face-hub&quot;,&quot;url&quot;:&quot;#the-hugging-face-hub&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#the-hugging-face-hub" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-hugging-face-hub"><wbr>The <wbr>Hugging <wbr>Face <wbr>Hub</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter4/1" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter4/1"); } </script> <iframe name="__privateStripeMetricsController6540" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter4%2F1%3Ffw%3Dpt&amp;title=The%20Hugging%20Face%20Hub%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:14.814Z
Using pretrained models - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter4/2?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#using-pretrained-models)Using pretrained models [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-4-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter4/section2_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter4/section2_pt.ipynb) The Model Hub makes selecting the appropriate model simple, so that using it in any downstream library can be done in a few lines of code. Let’s take a look at how to actually use one of these models, and how to contribute back to the community. Let’s say we’re looking for a French-based model that can perform mask filling. ![Selecting the Camembert model.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/camembert.gif) We select the `camembert-base` checkpoint to try it out. The identifier `camembert-base` is all we need to start using it! As you’ve seen in previous chapters, we can instantiate it using the `pipeline()` function: ``` from transformers import pipeline camembert_fill_mask = pipeline("fill-mask", model="camembert-base") results = camembert_fill_mask("Le camembert est <mask> :)")``` ``` [ {'sequence': 'Le camembert est délicieux :)', 'score': 0.49091005325317383, 'token': 7200, 'token_str': 'délicieux'}, {'sequence': 'Le camembert est excellent :)', 'score': 0.1055697426199913, 'token': 2183, 'token_str': 'excellent'}, {'sequence': 'Le camembert est succulent :)', 'score': 0.03453313186764717, 'token': 26202, 'token_str': 'succulent'}, {'sequence': 'Le camembert est meilleur :)', 'score': 0.0330314114689827, 'token': 528, 'token_str': 'meilleur'}, {'sequence': 'Le camembert est parfait :)', 'score': 0.03007650189101696, 'token': 1654, 'token_str': 'parfait'} ]``` As you can see, loading a model within a pipeline is extremely simple. The only thing you need to watch out for is that the chosen checkpoint is suitable for the task it’s going to be used for. For example, here we are loading the `camembert-base` checkpoint in the `fill-mask` pipeline, which is completely fine. But if we were to load this checkpoint in the `text-classification` pipeline, the results would not make any sense because the head of `camembert-base` is not suitable for this task! We recommend using the task selector in the Hugging Face Hub interface in order to select the appropriate checkpoints: ![The task selector on the web interface.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/tasks.png) You can also instantiate the checkpoint using the model architecture directly: ``` from transformers import CamembertTokenizer, CamembertForMaskedLM tokenizer = CamembertTokenizer.from_pretrained("camembert-base") model = CamembertForMaskedLM.from_pretrained("camembert-base")``` However, we recommend using the [`Auto*` classes](https://huggingface.co/transformers/model_doc/auto.html?highlight=auto#auto-classes) instead, as these are by design architecture-agnostic. While the previous code sample limits users to checkpoints loadable in the CamemBERT architecture, using the `Auto*` classes makes switching checkpoints simple: ``` from transformers import AutoTokenizer, AutoModelForMaskedLM tokenizer = AutoTokenizer.from_pretrained("camembert-base") model = AutoModelForMaskedLM.from_pretrained("camembert-base")``` When using a pretrained model, make sure to check how it was trained, on which datasets, its limits, and its biases. All of this information should be indicated on its model card.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Using pretrained models - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter4/2"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Using pretrained models - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;using-pretrained-models&quot;,&quot;title&quot;:&quot;Using pretrained models&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter4/2&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Using pretrained models&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Using pretrained models</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/1?fw=pt">The Hugging Face Hub </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter4/2?fw=pt">Using pretrained models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/3?fw=pt">Sharing pretrained models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/4?fw=pt">Building a model card </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/5?fw=pt">Part 1 completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/6?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="using-pretrained-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-pretrained-models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using pretrained models</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-4-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter4/section2_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter4/section2_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>The Model Hub makes selecting the appropriate model simple, so that using it in any downstream library can be done in a few lines of code. Let’s take a look at how to actually use one of these models, and how to contribute back to the community.</p> <p>Let’s say we’re looking for a French-based model that can perform mask filling.</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/camembert.gif" alt="Selecting the Camembert model." width="80%"></div> <p>We select the <code>camembert-base</code> checkpoint to try it out. The identifier <code>camembert-base</code> is all we need to start using it! As you’ve seen in previous chapters, we can instantiate it using the <code>pipeline()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline camembert_fill_mask = pipeline(<span class="hljs-string">"fill-mask"</span>, model=<span class="hljs-string">"camembert-base"</span>) results = camembert_fill_mask(<span class="hljs-string">"Le camembert est &lt;mask&gt; :)"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[ {<span class="hljs-string">'sequence'</span>: <span class="hljs-string">'Le camembert est délicieux :)'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.49091005325317383</span>, <span class="hljs-string">'token'</span>: <span class="hljs-number">7200</span>, <span class="hljs-string">'token_str'</span>: <span class="hljs-string">'délicieux'</span>}, {<span class="hljs-string">'sequence'</span>: <span class="hljs-string">'Le camembert est excellent :)'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.1055697426199913</span>, <span class="hljs-string">'token'</span>: <span class="hljs-number">2183</span>, <span class="hljs-string">'token_str'</span>: <span class="hljs-string">'excellent'</span>}, {<span class="hljs-string">'sequence'</span>: <span class="hljs-string">'Le camembert est succulent :)'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.03453313186764717</span>, <span class="hljs-string">'token'</span>: <span class="hljs-number">26202</span>, <span class="hljs-string">'token_str'</span>: <span class="hljs-string">'succulent'</span>}, {<span class="hljs-string">'sequence'</span>: <span class="hljs-string">'Le camembert est meilleur :)'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.0330314114689827</span>, <span class="hljs-string">'token'</span>: <span class="hljs-number">528</span>, <span class="hljs-string">'token_str'</span>: <span class="hljs-string">'meilleur'</span>}, {<span class="hljs-string">'sequence'</span>: <span class="hljs-string">'Le camembert est parfait :)'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.03007650189101696</span>, <span class="hljs-string">'token'</span>: <span class="hljs-number">1654</span>, <span class="hljs-string">'token_str'</span>: <span class="hljs-string">'parfait'</span>} ]</pre></div> <p>As you can see, loading a model within a pipeline is extremely simple. The only thing you need to watch out for is that the chosen checkpoint is suitable for the task it’s going to be used for. For example, here we are loading the <code>camembert-base</code> checkpoint in the <code>fill-mask</code> pipeline, which is completely fine. But if we were to load this checkpoint in the <code>text-classification</code> pipeline, the results would not make any sense because the head of <code>camembert-base</code> is not suitable for this task! We recommend using the task selector in the Hugging Face Hub interface in order to select the appropriate checkpoints:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/tasks.png" alt="The task selector on the web interface." width="80%"></div> <p>You can also instantiate the checkpoint using the model architecture directly:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> CamembertTokenizer, CamembertForMaskedLM tokenizer = CamembertTokenizer.from_pretrained(<span class="hljs-string">"camembert-base"</span>) model = CamembertForMaskedLM.from_pretrained(<span class="hljs-string">"camembert-base"</span>)</pre></div> <p>However, we recommend using the <a href="https://huggingface.co/transformers/model_doc/auto.html?highlight=auto#auto-classes" rel="nofollow"><code>Auto*</code> classes</a> instead, as these are by design architecture-agnostic. While the previous code sample limits users to checkpoints loadable in the CamemBERT architecture, using the <code>Auto*</code> classes makes switching checkpoints simple:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForMaskedLM tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"camembert-base"</span>) model = AutoModelForMaskedLM.from_pretrained(<span class="hljs-string">"camembert-base"</span>)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400">When using a pretrained model, make sure to check how it was trained, on which datasets, its limits, and its biases. All of this information should be indicated on its model card.</div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter4/1?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>The Hugging Face Hub</a> <a href="/learn/nlp-course/chapter4/3?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Sharing pretrained models<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;using-pretrained-models&quot;,&quot;url&quot;:&quot;#using-pretrained-models&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#using-pretrained-models" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-pretrained-models"><wbr>Using pretrained models</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter4/2" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter4/2"); } </script> <iframe name="__privateStripeMetricsController9490" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter4%2F2%3Ffw%3Dpt&amp;title=Using%20pretrained%20models%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:15.954Z
Sharing pretrained models - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter4/3?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#sharing-pretrained-models)Sharing pretrained models [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-4-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter4/section3_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter4/section3_pt.ipynb) In the steps below, we’ll take a look at the easiest ways to share pretrained models to the 🤗 Hub. There are tools and utilities available that make it simple to share and update models directly on the Hub, which we will explore below. We encourage all users that train models to contribute by sharing them with the community — sharing models, even when trained on very specific datasets, will help others, saving them time and compute resources and providing access to useful trained artifacts. In turn, you can benefit from the work that others have done! There are three ways to go about creating new model repositories: - Using the `push_to_hub` API - Using the `huggingface_hub` Python library - Using the web interface Once you’ve created a repository, you can upload files to it via git and git-lfs. We’ll walk you through creating model repositories and uploading files to them in the following sections. ## [](#using-the-pushtohub-api)Using the `push_to_hub` API The simplest way to upload files to the Hub is by leveraging the `push_to_hub` API. Before going further, you’ll need to generate an authentication token so that the `huggingface_hub` API knows who you are and what namespaces you have write access to. Make sure you are in an environment where you have `transformers` installed (see [Setup](/course/chapter0)). If you are in a notebook, you can use the following function to login: ``` from huggingface_hub import notebook_login notebook_login()``` In a terminal, you can run: In both cases, you should be prompted for your username and password, which are the same ones you use to log in to the Hub. If you do not have a Hub profile yet, you should create one [here](https://huggingface.co/join). Great! You now have your authentication token stored in your cache folder. Let’s create some repositories! If you have played around with the `Trainer` API to train a model, the easiest way to upload it to the Hub is to set `push_to_hub=True` when you define your `TrainingArguments`: ``` from transformers import TrainingArguments training_args = TrainingArguments( "bert-finetuned-mrpc", save_strategy="epoch", push_to_hub=True )``` When you call `trainer.train()`, the `Trainer` will then upload your model to the Hub each time it is saved (here every epoch) in a repository in your namespace. That repository will be named like the output directory you picked (here `bert-finetuned-mrpc`) but you can choose a different name with `hub_model_id = "a_different_name"`. To upload your model to an organization you are a member of, just pass it with `hub_model_id = "my_organization/my_repo_name"`. Once your training is finished, you should do a final `trainer.push_to_hub()` to upload the last version of your model. It will also generate a model card with all the relevant metadata, reporting the hyperparameters used and the evaluation results! Here is an example of the content you might find in a such a model card: ![An example of an auto-generated model card.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/model_card.png) At a lower level, accessing the Model Hub can be done directly on models, tokenizers, and configuration objects via their `push_to_hub()` method. This method takes care of both the repository creation and pushing the model and tokenizer files directly to the repository. No manual handling is required, unlike with the API we’ll see below. To get an idea of how it works, let’s first initialize a model and a tokenizer: ``` from transformers import AutoModelForMaskedLM, AutoTokenizer checkpoint = "camembert-base" model = AutoModelForMaskedLM.from_pretrained(checkpoint) tokenizer = AutoTokenizer.from_pretrained(checkpoint)``` You’re free to do whatever you want with these — add tokens to the tokenizer, train the model, fine-tune it. Once you’re happy with the resulting model, weights, and tokenizer, you can leverage the `push_to_hub()` method directly available on the `model` object: ``` model.push_to_hub("dummy-model")``` This will create the new repository `dummy-model` in your profile, and populate it with your model files. Do the same with the tokenizer, so that all the files are now available in this repository: ``` tokenizer.push_to_hub("dummy-model")``` If you belong to an organization, simply specify the `organization` argument to upload to that organization’s namespace: ``` tokenizer.push_to_hub("dummy-model", organization="huggingface")``` If you wish to use a specific Hugging Face token, you’re free to specify it to the `push_to_hub()` method as well: ``` tokenizer.push_to_hub("dummy-model", organization="huggingface", use_auth_token="<TOKEN>")``` Now head to the Model Hub to find your newly uploaded model: _[https://huggingface.co/user-or-organization/dummy-model](https://huggingface.co/user-or-organization/dummy-model)_. Click on the “Files and versions” tab, and you should see the files visible in the following screenshot: ![Dummy model containing both the tokenizer and model files.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/push_to_hub_dummy_model.png) ✏️ **Try it out!** Take the model and tokenizer associated with the `bert-base-cased` checkpoint and upload them to a repo in your namespace using the `push_to_hub()` method. Double-check that the repo appears properly on your page before deleting it. As you’ve seen, the `push_to_hub()` method accepts several arguments, making it possible to upload to a specific repository or organization namespace, or to use a different API token. We recommend you take a look at the method specification available directly in the [🤗 Transformers documentation](https://huggingface.co/transformers/model_sharing.html) to get an idea of what is possible. The `push_to_hub()` method is backed by the [`huggingface_hub`](https://github.com/huggingface/huggingface_hub) Python package, which offers a direct API to the Hugging Face Hub. It’s integrated within 🤗 Transformers and several other machine learning libraries, like [`allenlp`](https://github.com/allenai/allennlp). Although we focus on the 🤗 Transformers integration in this chapter, integrating it into your own code or library is simple. Jump to the last section to see how to upload files to your newly created repository! ## [](#using-the-huggingfacehub-python-library)Using the `huggingface_hub` Python library The `huggingface_hub` Python library is a package which offers a set of tools for the model and datasets hubs. It provides simple methods and classes for common tasks like getting information about repositories on the hub and managing them. It provides simple APIs that work on top of git to manage those repositories’ content and to integrate the Hub in your projects and libraries. Similarly to using the `push_to_hub` API, this will require you to have your API token saved in your cache. In order to do this, you will need to use the `login` command from the CLI, as mentioned in the previous section (again, make sure to prepend these commands with the `!` character if running in Google Colab): The `huggingface_hub` package offers several methods and classes which are useful for our purpose. Firstly, there are a few methods to manage repository creation, deletion, and others: ``` from huggingface_hub import ( login, logout, whoami, create_repo, delete_repo, update_repo_visibility, list_models, list_datasets, list_metrics, list_repo_files, upload_file, delete_file, )``` Additionally, it offers the very powerful `Repository` class to manage a local repository. We will explore these methods and that class in the next few section to understand how to leverage them. The `create_repo` method can be used to create a new repository on the hub: ``` from huggingface_hub import create_repo create_repo("dummy-model")``` This will create the repository `dummy-model` in your namespace. If you like, you can specify which organization the repository should belong to using the `organization` argument: ``` from huggingface_hub import create_repo create_repo("dummy-model", organization="huggingface")``` This will create the `dummy-model` repository in the `huggingface` namespace, assuming you belong to that organization. Other arguments which may be useful are: - `private`, in order to specify if the repository should be visible from others or not. - `token`, if you would like to override the token stored in your cache by a given token. - `repo_type`, if you would like to create a `dataset` or a `space` instead of a model. Accepted values are `"dataset"` and `"space"`. Once the repository is created, we should add files to it! Jump to the next section to see the three ways this can be handled. ## [](#using-the-web-interface)Using the web interface The web interface offers tools to manage repositories directly in the Hub. Using the interface, you can easily create repositories, add files (even large ones!), explore models, visualize diffs, and much more. To create a new repository, visit [huggingface.co/new](https://huggingface.co/new): ![Page showcasing the model used for the creation of a new model repository.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/new_model.png) First, specify the owner of the repository: this can be either you or any of the organizations you’re affiliated with. If you choose an organization, the model will be featured on the organization’s page and every member of the organization will have the ability to contribute to the repository. Next, enter your model’s name. This will also be the name of the repository. Finally, you can specify whether you want your model to be public or private. Private models are hidden from public view. After creating your model repository, you should see a page like this: ![An empty model page after creating a new repository.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/empty_model.png) This is where your model will be hosted. To start populating it, you can add a README file directly from the web interface. ![The README file showing the Markdown capabilities.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/dummy_model.png) The README file is in Markdown — feel free to go wild with it! The third part of this chapter is dedicated to building a model card. These are of prime importance in bringing value to your model, as they’re where you tell others what it can do. If you look at the “Files and versions” tab, you’ll see that there aren’t many files there yet — just the _README.md_ you just created and the _.gitattributes_ file that keeps track of large files. ![The 'Files and versions' tab only shows the .gitattributes and README.md files.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/files.png) We’ll take a look at how to add some new files next. ## [](#uploading-the-model-files)Uploading the model files The system to manage files on the Hugging Face Hub is based on git for regular files, and git-lfs (which stands for [Git Large File Storage](https://git-lfs.github.com/)) for larger files. In the next section, we go over three different ways of uploading files to the Hub: through `huggingface_hub` and through git commands. ### [](#the-uploadfile-approach)The `upload_file` approach Using `upload_file` does not require git and git-lfs to be installed on your system. It pushes files directly to the 🤗 Hub using HTTP POST requests. A limitation of this approach is that it doesn’t handle files that are larger than 5GB in size. If your files are larger than 5GB, please follow the two other methods detailed below. The API may be used as follows: ``` from huggingface_hub import upload_file upload_file( "<path_to_file>/config.json", path_in_repo="config.json", repo_id="<namespace>/dummy-model", )``` This will upload the file `config.json` available at `<path_to_file>` to the root of the repository as `config.json`, to the `dummy-model` repository. Other arguments which may be useful are: - `token`, if you would like to override the token stored in your cache by a given token. - `repo_type`, if you would like to upload to a `dataset` or a `space` instead of a model. Accepted values are `"dataset"` and `"space"`. ### [](#the-repository-class)The `Repository` class The `Repository` class manages a local repository in a git-like manner. It abstracts most of the pain points one may have with git to provide all features that we require. Using this class requires having git and git-lfs installed, so make sure you have git-lfs installed (see [here](https://git-lfs.github.com/) for installation instructions) and set up before you begin. In order to start playing around with the repository we have just created, we can start by initialising it into a local folder by cloning the remote repository: ``` from huggingface_hub import Repository repo = Repository("<path_to_dummy_folder>", clone_from="<namespace>/dummy-model")``` This created the folder `<path_to_dummy_folder>` in our working directory. This folder only contains the `.gitattributes` file as that’s the only file created when instantiating the repository through `create_repo`. From this point on, we may leverage several of the traditional git methods: ``` repo.git_pull() repo.git_add() repo.git_commit() repo.git_push() repo.git_tag()``` And others! We recommend taking a look at the `Repository` documentation available [here](https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub#advanced-programmatic-repository-management) for an overview of all available methods. At present, we have a model and a tokenizer that we would like to push to the hub. We have successfully cloned the repository, we can therefore save the files within that repository. We first make sure that our local clone is up to date by pulling the latest changes: Once that is done, we save the model and tokenizer files: ``` model.save_pretrained("<path_to_dummy_folder>") tokenizer.save_pretrained("<path_to_dummy_folder>")``` The `<path_to_dummy_folder>` now contains all the model and tokenizer files. We follow the usual git workflow by adding files to the staging area, committing them and pushing them to the hub: ``` repo.git_add() repo.git_commit("Add model and tokenizer files") repo.git_push()``` Congratulations! You just pushed your first files on the hub. ### [](#the-git-based-approach)The git-based approach This is the very barebones approach to uploading files: we’ll do so with git and git-lfs directly. Most of the difficulty is abstracted away by previous approaches, but there are a few caveats with the following method so we’ll follow a more complex use-case. Using this class requires having git and git-lfs installed, so make sure you have [git-lfs](https://git-lfs.github.com/) installed (see here for installation instructions) and set up before you begin. First start by initializing git-lfs: ``` Updated git hooks. Git LFS initialized.``` Once that’s done, the first step is to clone your model repository: ``` git clone https://huggingface.co/<namespace>/<your-model-id>``` My username is `lysandre` and I’ve used the model name `dummy`, so for me the command ends up looking like the following: ``` git clone https://huggingface.co/lysandre/dummy``` I now have a folder named _dummy_ in my working directory. I can `cd` into the folder and have a look at the contents: If you just created your repository using Hugging Face Hub’s `create_repo` method, this folder should only contain a hidden `.gitattributes` file. If you followed the instructions in the previous section to create a repository using the web interface, the folder should contain a single _README.md_ file alongside the hidden `.gitattributes` file, as shown here. Adding a regular-sized file, such as a configuration file, a vocabulary file, or basically any file under a few megabytes, is done exactly as one would do it in any git-based system. However, bigger files must be registered through git-lfs in order to push them to _huggingface.co_. Let’s go back to Python for a bit to generate a model and tokenizer that we’d like to commit to our dummy repository: ``` from transformers import AutoModelForMaskedLM, AutoTokenizer checkpoint = "camembert-base" model = AutoModelForMaskedLM.from_pretrained(checkpoint) tokenizer = AutoTokenizer.from_pretrained(checkpoint) model.save_pretrained("<path_to_dummy_folder>") tokenizer.save_pretrained("<path_to_dummy_folder>")``` Now that we’ve saved some model and tokenizer artifacts, let’s take another look at the _dummy_ folder: ``` config.json pytorch_model.bin README.md sentencepiece.bpe.model special_tokens_map.json tokenizer_config.json tokenizer.json``` If you look at the file sizes (for example, with `ls -lh`), you should see that the model state dict file (_pytorch\_model.bin_) is the only outlier, at more than 400 MB. ✏️ When creating the repository from the web interface, the \*.gitattributes\* file is automatically set up to consider files with certain extensions, such as \*.bin\* and \*.h5\*, as large files, and git-lfs will track them with no necessary setup on your side. We can now go ahead and proceed like we would usually do with traditional Git repositories. We can add all the files to Git’s staging environment using the `git add` command: We can then have a look at the files that are currently staged: ``` On branch main Your branch is up to date with 'origin/main'. Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: .gitattributes new file: config.json new file: pytorch_model.bin new file: sentencepiece.bpe.model new file: special_tokens_map.json new file: tokenizer.json new file: tokenizer_config.json``` Similarly, we can make sure that git-lfs is tracking the correct files by using its `status` command: ``` On branch main Objects to be pushed to origin/main: Objects to be committed: config.json (Git: bc20ff2) pytorch_model.bin (LFS: 35686c2) sentencepiece.bpe.model (LFS: 988bc5a) special_tokens_map.json (Git: cb23931) tokenizer.json (Git: 851ff3e) tokenizer_config.json (Git: f0f7783) Objects not staged for commit: ``` We can see that all files have `Git` as a handler, except _pytorch\_model.bin_ and _sentencepiece.bpe.model_, which have `LFS`. Great! Let’s proceed to the final steps, committing and pushing to the _huggingface.co_ remote repository: ``` git commit -m "First model version"``` ``` [main b08aab1] First model version 7 files changed, 29027 insertions(+) 6 files changed, 36 insertions(+) create mode 100644 config.json create mode 100644 pytorch_model.bin create mode 100644 sentencepiece.bpe.model create mode 100644 special_tokens_map.json create mode 100644 tokenizer.json create mode 100644 tokenizer_config.json``` Pushing can take a bit of time, depending on the speed of your internet connection and the size of your files: ``` Uploading LFS objects: 100% (1/1), 433 MB | 1.3 MB/s, done. Enumerating objects: 11, done. Counting objects: 100% (11/11), done. Delta compression using up to 12 threads Compressing objects: 100% (9/9), done. Writing objects: 100% (9/9), 288.27 KiB | 6.27 MiB/s, done. Total 9 (delta 1), reused 0 (delta 0), pack-reused 0 To https://huggingface.co/lysandre/dummy 891b41d..b08aab1 main -> main``` If we take a look at the model repository when this is finished, we can see all the recently added files: ![The 'Files and versions' tab now contains all the recently uploaded files.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/full_model.png) The UI allows you to explore the model files and commits and to see the diff introduced by each commit: ![The diff introduced by the recent commit.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/diffs.gif)
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Sharing pretrained models - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter4/3"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Sharing pretrained models - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;sharing-pretrained-models&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;using-the-pushtohub-api&quot;,&quot;title&quot;:&quot;Using the `push_to_hub` API&quot;},{&quot;local&quot;:&quot;using-the-huggingfacehub-python-library&quot;,&quot;title&quot;:&quot;Using the `huggingface_hub` Python library&quot;},{&quot;local&quot;:&quot;using-the-web-interface&quot;,&quot;title&quot;:&quot;Using the web interface&quot;},{&quot;local&quot;:&quot;uploading-the-model-files&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;the-uploadfile-approach&quot;,&quot;title&quot;:&quot;The `upload_file` approach&quot;},{&quot;local&quot;:&quot;the-repository-class&quot;,&quot;title&quot;:&quot;The `Repository` class&quot;},{&quot;local&quot;:&quot;the-git-based-approach&quot;,&quot;title&quot;:&quot;The git-based approach&quot;}],&quot;title&quot;:&quot;Uploading the model files&quot;}],&quot;title&quot;:&quot;Sharing pretrained models&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter4/3&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Sharing pretrained models&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Sharing pretrained models</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/1?fw=pt">The Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/2?fw=pt">Using pretrained models </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter4/3?fw=pt">Sharing pretrained models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/4?fw=pt">Building a model card </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/5?fw=pt">Part 1 completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/6?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="sharing-pretrained-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#sharing-pretrained-models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Sharing pretrained models</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-4-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter4/section3_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter4/section3_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>In the steps below, we’ll take a look at the easiest ways to share pretrained models to the 🤗 Hub. There are tools and utilities available that make it simple to share and update models directly on the Hub, which we will explore below.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/9yY3RB_GSPM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>We encourage all users that train models to contribute by sharing them with the community — sharing models, even when trained on very specific datasets, will help others, saving them time and compute resources and providing access to useful trained artifacts. In turn, you can benefit from the work that others have done!</p> <p>There are three ways to go about creating new model repositories:</p> <ul><li>Using the <code>push_to_hub</code> API</li> <li>Using the <code>huggingface_hub</code> Python library</li> <li>Using the web interface</li></ul> <p>Once you’ve created a repository, you can upload files to it via git and git-lfs. We’ll walk you through creating model repositories and uploading files to them in the following sections.</p> <h2 class="relative group"><a id="using-the-pushtohub-api" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-the-pushtohub-api"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using the <code>push_to_hub</code> API</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/Zh0FfmVrKX0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The simplest way to upload files to the Hub is by leveraging the <code>push_to_hub</code> API.</p> <p>Before going further, you’ll need to generate an authentication token so that the <code>huggingface_hub</code> API knows who you are and what namespaces you have write access to. Make sure you are in an environment where you have <code>transformers</code> installed (see <a href="/course/chapter0">Setup</a>). If you are in a notebook, you can use the following function to login:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login notebook_login()</pre></div> <p>In a terminal, you can run:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-cli login</pre></div> <p>In both cases, you should be prompted for your username and password, which are the same ones you use to log in to the Hub. If you do not have a Hub profile yet, you should create one <a href="https://huggingface.co/join" rel="nofollow">here</a>.</p> <p>Great! You now have your authentication token stored in your cache folder. Let’s create some repositories!</p> <p>If you have played around with the <code>Trainer</code> API to train a model, the easiest way to upload it to the Hub is to set <code>push_to_hub=True</code> when you define your <code>TrainingArguments</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments training_args = TrainingArguments( <span class="hljs-string">"bert-finetuned-mrpc"</span>, save_strategy=<span class="hljs-string">"epoch"</span>, push_to_hub=<span class="hljs-literal">True</span> )</pre></div> <p>When you call <code>trainer.train()</code>, the <code>Trainer</code> will then upload your model to the Hub each time it is saved (here every epoch) in a repository in your namespace. That repository will be named like the output directory you picked (here <code>bert-finetuned-mrpc</code>) but you can choose a different name with <code>hub_model_id = "a_different_name"</code>.</p> <p>To upload your model to an organization you are a member of, just pass it with <code>hub_model_id = "my_organization/my_repo_name"</code>.</p> <p>Once your training is finished, you should do a final <code>trainer.push_to_hub()</code> to upload the last version of your model. It will also generate a model card with all the relevant metadata, reporting the hyperparameters used and the evaluation results! Here is an example of the content you might find in a such a model card:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/model_card.png" alt="An example of an auto-generated model card." width="100%"></div> <p>At a lower level, accessing the Model Hub can be done directly on models, tokenizers, and configuration objects via their <code>push_to_hub()</code> method. This method takes care of both the repository creation and pushing the model and tokenizer files directly to the repository. No manual handling is required, unlike with the API we’ll see below.</p> <p>To get an idea of how it works, let’s first initialize a model and a tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForMaskedLM, AutoTokenizer checkpoint = <span class="hljs-string">"camembert-base"</span> model = AutoModelForMaskedLM.from_pretrained(checkpoint) tokenizer = AutoTokenizer.from_pretrained(checkpoint)</pre></div> <p>You’re free to do whatever you want with these — add tokens to the tokenizer, train the model, fine-tune it. Once you’re happy with the resulting model, weights, and tokenizer, you can leverage the <code>push_to_hub()</code> method directly available on the <code>model</code> object:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model.push_to_hub(<span class="hljs-string">"dummy-model"</span>)</pre></div> <p>This will create the new repository <code>dummy-model</code> in your profile, and populate it with your model files. Do the same with the tokenizer, so that all the files are now available in this repository:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.push_to_hub(<span class="hljs-string">"dummy-model"</span>)</pre></div> <p>If you belong to an organization, simply specify the <code>organization</code> argument to upload to that organization’s namespace:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.push_to_hub(<span class="hljs-string">"dummy-model"</span>, organization=<span class="hljs-string">"huggingface"</span>)</pre></div> <p>If you wish to use a specific Hugging Face token, you’re free to specify it to the <code>push_to_hub()</code> method as well:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.push_to_hub(<span class="hljs-string">"dummy-model"</span>, organization=<span class="hljs-string">"huggingface"</span>, use_auth_token=<span class="hljs-string">"&lt;TOKEN&gt;"</span>)</pre></div> <p>Now head to the Model Hub to find your newly uploaded model: <em><a href="https://huggingface.co/user-or-organization/dummy-model" rel="nofollow">https://huggingface.co/user-or-organization/dummy-model</a></em>.</p> <p>Click on the “Files and versions” tab, and you should see the files visible in the following screenshot:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/push_to_hub_dummy_model.png" alt="Dummy model containing both the tokenizer and model files." width="80%"></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Take the model and tokenizer associated with the <code>bert-base-cased</code> checkpoint and upload them to a repo in your namespace using the <code>push_to_hub()</code> method. Double-check that the repo appears properly on your page before deleting it.</p></div> <p>As you’ve seen, the <code>push_to_hub()</code> method accepts several arguments, making it possible to upload to a specific repository or organization namespace, or to use a different API token. We recommend you take a look at the method specification available directly in the <a href="https://huggingface.co/transformers/model_sharing.html" rel="nofollow">🤗 Transformers documentation</a> to get an idea of what is possible.</p> <p>The <code>push_to_hub()</code> method is backed by the <a href="https://github.com/huggingface/huggingface_hub" rel="nofollow"><code>huggingface_hub</code></a> Python package, which offers a direct API to the Hugging Face Hub. It’s integrated within 🤗 Transformers and several other machine learning libraries, like <a href="https://github.com/allenai/allennlp" rel="nofollow"><code>allenlp</code></a>. Although we focus on the 🤗 Transformers integration in this chapter, integrating it into your own code or library is simple.</p> <p>Jump to the last section to see how to upload files to your newly created repository!</p> <h2 class="relative group"><a id="using-the-huggingfacehub-python-library" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-the-huggingfacehub-python-library"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using the <code>huggingface_hub</code> Python library</span></h2> <p>The <code>huggingface_hub</code> Python library is a package which offers a set of tools for the model and datasets hubs. It provides simple methods and classes for common tasks like getting information about repositories on the hub and managing them. It provides simple APIs that work on top of git to manage those repositories’ content and to integrate the Hub in your projects and libraries.</p> <p>Similarly to using the <code>push_to_hub</code> API, this will require you to have your API token saved in your cache. In order to do this, you will need to use the <code>login</code> command from the CLI, as mentioned in the previous section (again, make sure to prepend these commands with the <code>!</code> character if running in Google Colab):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-cli login</pre></div> <p>The <code>huggingface_hub</code> package offers several methods and classes which are useful for our purpose. Firstly, there are a few methods to manage repository creation, deletion, and others:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> ( <span class="hljs-comment"># User management</span> login, logout, whoami, <span class="hljs-comment"># Repository creation and management</span> create_repo, delete_repo, update_repo_visibility, <span class="hljs-comment"># And some methods to retrieve/change information about the content</span> list_models, list_datasets, list_metrics, list_repo_files, upload_file, delete_file, )</pre></div> <p>Additionally, it offers the very powerful <code>Repository</code> class to manage a local repository. We will explore these methods and that class in the next few section to understand how to leverage them.</p> <p>The <code>create_repo</code> method can be used to create a new repository on the hub:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> create_repo create_repo(<span class="hljs-string">"dummy-model"</span>)</pre></div> <p>This will create the repository <code>dummy-model</code> in your namespace. If you like, you can specify which organization the repository should belong to using the <code>organization</code> argument:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> create_repo create_repo(<span class="hljs-string">"dummy-model"</span>, organization=<span class="hljs-string">"huggingface"</span>)</pre></div> <p>This will create the <code>dummy-model</code> repository in the <code>huggingface</code> namespace, assuming you belong to that organization. Other arguments which may be useful are:</p> <ul><li><code>private</code>, in order to specify if the repository should be visible from others or not.</li> <li><code>token</code>, if you would like to override the token stored in your cache by a given token.</li> <li><code>repo_type</code>, if you would like to create a <code>dataset</code> or a <code>space</code> instead of a model. Accepted values are <code>"dataset"</code> and <code>"space"</code>.</li></ul> <p>Once the repository is created, we should add files to it! Jump to the next section to see the three ways this can be handled.</p> <h2 class="relative group"><a id="using-the-web-interface" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-the-web-interface"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using the web interface</span></h2> <p>The web interface offers tools to manage repositories directly in the Hub. Using the interface, you can easily create repositories, add files (even large ones!), explore models, visualize diffs, and much more.</p> <p>To create a new repository, visit <a href="https://huggingface.co/new" rel="nofollow">huggingface.co/new</a>:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/new_model.png" alt="Page showcasing the model used for the creation of a new model repository." width="80%"></div> <p>First, specify the owner of the repository: this can be either you or any of the organizations you’re affiliated with. If you choose an organization, the model will be featured on the organization’s page and every member of the organization will have the ability to contribute to the repository.</p> <p>Next, enter your model’s name. This will also be the name of the repository. Finally, you can specify whether you want your model to be public or private. Private models are hidden from public view.</p> <p>After creating your model repository, you should see a page like this:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/empty_model.png" alt="An empty model page after creating a new repository." width="80%"></div> <p>This is where your model will be hosted. To start populating it, you can add a README file directly from the web interface.</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/dummy_model.png" alt="The README file showing the Markdown capabilities." width="80%"></div> <p>The README file is in Markdown — feel free to go wild with it! The third part of this chapter is dedicated to building a model card. These are of prime importance in bringing value to your model, as they’re where you tell others what it can do.</p> <p>If you look at the “Files and versions” tab, you’ll see that there aren’t many files there yet — just the <em>README.md</em> you just created and the <em>.gitattributes</em> file that keeps track of large files.</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/files.png" alt="The 'Files and versions' tab only shows the .gitattributes and README.md files." width="80%"></div> <p>We’ll take a look at how to add some new files next.</p> <h2 class="relative group"><a id="uploading-the-model-files" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#uploading-the-model-files"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Uploading the model files</span></h2> <p>The system to manage files on the Hugging Face Hub is based on git for regular files, and git-lfs (which stands for <a href="https://git-lfs.github.com/" rel="nofollow">Git Large File Storage</a>) for larger files.</p> <p>In the next section, we go over three different ways of uploading files to the Hub: through <code>huggingface_hub</code> and through git commands.</p> <h3 class="relative group"><a id="the-uploadfile-approach" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-uploadfile-approach"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The <code>upload_file</code> approach</span></h3> <p>Using <code>upload_file</code> does not require git and git-lfs to be installed on your system. It pushes files directly to the 🤗 Hub using HTTP POST requests. A limitation of this approach is that it doesn’t handle files that are larger than 5GB in size. If your files are larger than 5GB, please follow the two other methods detailed below.</p> <p>The API may be used as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> upload_file upload_file( <span class="hljs-string">"&lt;path_to_file&gt;/config.json"</span>, path_in_repo=<span class="hljs-string">"config.json"</span>, repo_id=<span class="hljs-string">"&lt;namespace&gt;/dummy-model"</span>, )</pre></div> <p>This will upload the file <code>config.json</code> available at <code>&lt;path_to_file&gt;</code> to the root of the repository as <code>config.json</code>, to the <code>dummy-model</code> repository. Other arguments which may be useful are:</p> <ul><li><code>token</code>, if you would like to override the token stored in your cache by a given token.</li> <li><code>repo_type</code>, if you would like to upload to a <code>dataset</code> or a <code>space</code> instead of a model. Accepted values are <code>"dataset"</code> and <code>"space"</code>.</li></ul> <h3 class="relative group"><a id="the-repository-class" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-repository-class"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The <code>Repository</code> class</span></h3> <p>The <code>Repository</code> class manages a local repository in a git-like manner. It abstracts most of the pain points one may have with git to provide all features that we require.</p> <p>Using this class requires having git and git-lfs installed, so make sure you have git-lfs installed (see <a href="https://git-lfs.github.com/" rel="nofollow">here</a> for installation instructions) and set up before you begin.</p> <p>In order to start playing around with the repository we have just created, we can start by initialising it into a local folder by cloning the remote repository:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> Repository repo = Repository(<span class="hljs-string">"&lt;path_to_dummy_folder&gt;"</span>, clone_from=<span class="hljs-string">"&lt;namespace&gt;/dummy-model"</span>)</pre></div> <p>This created the folder <code>&lt;path_to_dummy_folder&gt;</code> in our working directory. This folder only contains the <code>.gitattributes</code> file as that’s the only file created when instantiating the repository through <code>create_repo</code>.</p> <p>From this point on, we may leverage several of the traditional git methods:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>repo.git_pull() repo.git_add() repo.git_commit() repo.git_push() repo.git_tag()</pre></div> <p>And others! We recommend taking a look at the <code>Repository</code> documentation available <a href="https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub#advanced-programmatic-repository-management" rel="nofollow">here</a> for an overview of all available methods.</p> <p>At present, we have a model and a tokenizer that we would like to push to the hub. We have successfully cloned the repository, we can therefore save the files within that repository.</p> <p>We first make sure that our local clone is up to date by pulling the latest changes:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>repo.git_pull()</pre></div> <p>Once that is done, we save the model and tokenizer files:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model.save_pretrained(<span class="hljs-string">"&lt;path_to_dummy_folder&gt;"</span>) tokenizer.save_pretrained(<span class="hljs-string">"&lt;path_to_dummy_folder&gt;"</span>)</pre></div> <p>The <code>&lt;path_to_dummy_folder&gt;</code> now contains all the model and tokenizer files. We follow the usual git workflow by adding files to the staging area, committing them and pushing them to the hub:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>repo.git_add() repo.git_commit(<span class="hljs-string">"Add model and tokenizer files"</span>) repo.git_push()</pre></div> <p>Congratulations! You just pushed your first files on the hub.</p> <h3 class="relative group"><a id="the-git-based-approach" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-git-based-approach"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The git-based approach</span></h3> <p>This is the very barebones approach to uploading files: we’ll do so with git and git-lfs directly. Most of the difficulty is abstracted away by previous approaches, but there are a few caveats with the following method so we’ll follow a more complex use-case.</p> <p>Using this class requires having git and git-lfs installed, so make sure you have <a href="https://git-lfs.github.com/" rel="nofollow">git-lfs</a> installed (see here for installation instructions) and set up before you begin.</p> <p>First start by initializing git-lfs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>git lfs install</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Updated git hooks. Git LFS initialized.</pre></div> <p>Once that’s done, the first step is to clone your model repository:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>git <span class="hljs-built_in">clone</span> https://huggingface.co/&lt;namespace&gt;/&lt;your-model-id&gt;</pre></div> <p>My username is <code>lysandre</code> and I’ve used the model name <code>dummy</code>, so for me the command ends up looking like the following:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>git clone https:<span class="hljs-regexp">//</span>huggingface.co<span class="hljs-regexp">/lysandre/</span>dummy</pre></div> <p>I now have a folder named <em>dummy</em> in my working directory. I can <code>cd</code> into the folder and have a look at the contents:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">cd</span> dummy &amp;&amp; <span class="hljs-built_in">ls</span></pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>README.md</pre></div> <p>If you just created your repository using Hugging Face Hub’s <code>create_repo</code> method, this folder should only contain a hidden <code>.gitattributes</code> file. If you followed the instructions in the previous section to create a repository using the web interface, the folder should contain a single <em>README.md</em> file alongside the hidden <code>.gitattributes</code> file, as shown here.</p> <p>Adding a regular-sized file, such as a configuration file, a vocabulary file, or basically any file under a few megabytes, is done exactly as one would do it in any git-based system. However, bigger files must be registered through git-lfs in order to push them to <em>huggingface.co</em>.</p> <p>Let’s go back to Python for a bit to generate a model and tokenizer that we’d like to commit to our dummy repository:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForMaskedLM, AutoTokenizer checkpoint = <span class="hljs-string">"camembert-base"</span> model = AutoModelForMaskedLM.from_pretrained(checkpoint) tokenizer = AutoTokenizer.from_pretrained(checkpoint) <span class="hljs-comment"># Do whatever with the model, train it, fine-tune it...</span> model.save_pretrained(<span class="hljs-string">"&lt;path_to_dummy_folder&gt;"</span>) tokenizer.save_pretrained(<span class="hljs-string">"&lt;path_to_dummy_folder&gt;"</span>)</pre></div> <p>Now that we’ve saved some model and tokenizer artifacts, let’s take another look at the <em>dummy</em> folder:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">ls</span></pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>config.json pytorch_model.bin README.md sentencepiece.bpe.model special_tokens_map.json tokenizer_config.json tokenizer.json</pre></div> <p>If you look at the file sizes (for example, with <code>ls -lh</code>), you should see that the model state dict file (<em>pytorch_model.bin</em>) is the only outlier, at more than 400 MB.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400">✏️ When creating the repository from the web interface, the *.gitattributes* file is automatically set up to consider files with certain extensions, such as *.bin* and *.h5*, as large files, and git-lfs will track them with no necessary setup on your side.</div> <p>We can now go ahead and proceed like we would usually do with traditional Git repositories. We can add all the files to Git’s staging environment using the <code>git add</code> command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>git add .</pre></div> <p>We can then have a look at the files that are currently staged:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>git status</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>On branch main Your branch is up to <span class="hljs-built_in">date</span> with <span class="hljs-string">'origin/main'</span>. Changes to be committed: (use <span class="hljs-string">"git restore --staged &lt;file&gt;..."</span> to unstage) modified: .gitattributes new file: config.json new file: pytorch_model.bin new file: sentencepiece.bpe.model new file: special_tokens_map.json new file: tokenizer.json new file: tokenizer_config.json</pre></div> <p>Similarly, we can make sure that git-lfs is tracking the correct files by using its <code>status</code> command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>git lfs status</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>On branch main Objects to be pushed to origin/main: Objects to be committed: config.json (Git: bc20ff2) pytorch_model.bin (LFS: 35686c2) sentencepiece.bpe.model (LFS: 988bc5a) special_tokens_map.json (Git: cb23931) tokenizer.json (Git: 851ff3e) tokenizer_config.json (Git: f0f7783) Objects not staged <span class="hljs-keyword">for</span> commit: </pre></div> <p>We can see that all files have <code>Git</code> as a handler, except <em>pytorch_model.bin</em> and <em>sentencepiece.bpe.model</em>, which have <code>LFS</code>. Great!</p> <p>Let’s proceed to the final steps, committing and pushing to the <em>huggingface.co</em> remote repository:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>git commit -m <span class="hljs-string">"First model version"</span></pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[main b08aab1] First model version 7 files changed, 29027 insertions(+) 6 files changed, 36 insertions(+) create mode 100644 config.json create mode 100644 pytorch_model.bin create mode 100644 sentencepiece.bpe.model create mode 100644 special_tokens_map.json create mode 100644 tokenizer.json create mode 100644 tokenizer_config.json</pre></div> <p>Pushing can take a bit of time, depending on the speed of your internet connection and the size of your files:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>git push</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Uploading LFS objects: 100% (1/1), 433 MB | 1.3 MB/s, <span class="hljs-keyword">done</span>. Enumerating objects: 11, <span class="hljs-keyword">done</span>. Counting objects: 100% (11/11), <span class="hljs-keyword">done</span>. Delta compression using up to 12 threads Compressing objects: 100% (9/9), <span class="hljs-keyword">done</span>. Writing objects: 100% (9/9), 288.27 KiB | 6.27 MiB/s, <span class="hljs-keyword">done</span>. Total 9 (delta 1), reused 0 (delta 0), pack-reused 0 To https://huggingface.co/lysandre/dummy 891b41d..b08aab1 main -&gt; main</pre></div> <p>If we take a look at the model repository when this is finished, we can see all the recently added files:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/full_model.png" alt="The 'Files and versions' tab now contains all the recently uploaded files." width="80%"></div> <p>The UI allows you to explore the model files and commits and to see the diff introduced by each commit:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter4/diffs.gif" alt="The diff introduced by the recent commit." width="80%"></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter4/2?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Using pretrained models</a> <a href="/learn/nlp-course/chapter4/4?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Building a model card<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;sharing-pretrained-models&quot;,&quot;url&quot;:&quot;#sharing-pretrained-models&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Using the `push_to_hub` API&quot;,&quot;id&quot;:&quot;using-the-pushtohub-api&quot;,&quot;url&quot;:&quot;#using-the-pushtohub-api&quot;},{&quot;title&quot;:&quot;Using the `huggingface_hub` Python library&quot;,&quot;id&quot;:&quot;using-the-huggingfacehub-python-library&quot;,&quot;url&quot;:&quot;#using-the-huggingfacehub-python-library&quot;},{&quot;title&quot;:&quot;Using the web interface&quot;,&quot;id&quot;:&quot;using-the-web-interface&quot;,&quot;url&quot;:&quot;#using-the-web-interface&quot;},{&quot;title&quot;:&quot;Uploading the model files&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;uploading-the-model-files&quot;,&quot;url&quot;:&quot;#uploading-the-model-files&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;The `upload_file` approach&quot;,&quot;id&quot;:&quot;the-uploadfile-approach&quot;,&quot;url&quot;:&quot;#the-uploadfile-approach&quot;},{&quot;title&quot;:&quot;The `Repository` class&quot;,&quot;id&quot;:&quot;the-repository-class&quot;,&quot;url&quot;:&quot;#the-repository-class&quot;},{&quot;title&quot;:&quot;The git-based approach&quot;,&quot;id&quot;:&quot;the-git-based-approach&quot;,&quot;url&quot;:&quot;#the-git-based-approach&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#sharing-pretrained-models" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-sharing-pretrained-models"><wbr>Sharing pretrained models</a> <a href="#using-the-pushtohub-api" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-the-pushtohub-api"><wbr>Using the `push_to_hub` API</a> <a href="#using-the-huggingfacehub-python-library" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-the-huggingfacehub-python-library"><wbr>Using the `huggingface_hub` <wbr>Python library</a> <a href="#using-the-web-interface" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-the-web-interface"><wbr>Using the web interface</a> <a href="#uploading-the-model-files" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-uploading-the-model-files"><wbr>Uploading the model files</a> <a href="#the-uploadfile-approach" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-uploadfile-approach"><wbr>The `upload_file` approach</a> <a href="#the-repository-class" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-repository-class"><wbr>The `<wbr>Repository` class</a> <a href="#the-git-based-approach" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-git-based-approach"><wbr>The git-based approach</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter4/3" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter4/3"); } </script> <iframe name="__privateStripeMetricsController6440" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter4%2F3%3Ffw%3Dpt&amp;title=Sharing%20pretrained%20models%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:16.174Z
Building a model card - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter4/4?fw=pt
## [](#building-a-model-card)Building a model card [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-4-questions) The model card is a file which is arguably as important as the model and tokenizer files in a model repository. It is the central definition of the model, ensuring reusability by fellow community members and reproducibility of results, and providing a platform on which other members may build their artifacts. Documenting the training and evaluation process helps others understand what to expect of a model — and providing sufficient information regarding the data that was used and the preprocessing and postprocessing that were done ensures that the limitations, biases, and contexts in which the model is and is not useful can be identified and understood. Therefore, creating a model card that clearly defines your model is a very important step. Here, we provide some tips that will help you with this. Creating the model card is done through the _README.md_ file you saw earlier, which is a Markdown file. The “model card” concept originates from a research direction from Google, first shared in the paper [“Model Cards for Model Reporting”](https://arxiv.org/abs/1810.03993) by Margaret Mitchell et al. A lot of information contained here is based on that paper, and we recommend you take a look at it to understand why model cards are so important in a world that values reproducibility, reusability, and fairness. The model card usually starts with a very brief, high-level overview of what the model is for, followed by additional details in the following sections: - Model description - Intended uses & limitations - How to use - Limitations and bias - Training data - Training procedure - Evaluation results Let’s take a look at what each of these sections should contain. ### [](#model-description)Model description The model description provides basic details about the model. This includes the architecture, version, if it was introduced in a paper, if an original implementation is available, the author, and general information about the model. Any copyright should be attributed here. General information about training procedures, parameters, and important disclaimers can also be mentioned in this section. ### [](#intended-uses-limitations)Intended uses & limitations Here you describe the use cases the model is intended for, including the languages, fields, and domains where it can be applied. This section of the model card can also document areas that are known to be out of scope for the model, or where it is likely to perform suboptimally. ### [](#how-to-use)How to use This section should include some examples of how to use the model. This can showcase usage of the `pipeline()` function, usage of the model and tokenizer classes, and any other code you think might be helpful. ### [](#training-data)Training data This part should indicate which dataset(s) the model was trained on. A brief description of the dataset(s) is also welcome. ### [](#training-procedure)Training procedure In this section you should describe all the relevant aspects of training that are useful from a reproducibility perspective. This includes any preprocessing and postprocessing that were done on the data, as well as details such as the number of epochs the model was trained for, the batch size, the learning rate, and so on. ### [](#variable-and-metrics)Variable and metrics Here you should describe the metrics you use for evaluation, and the different factors you are mesuring. Mentioning which metric(s) were used, on which dataset and which dataset split, makes it easy to compare you model’s performance compared to that of other models. These should be informed by the previous sections, such as the intended users and use cases. ### [](#evaluation-results)Evaluation results Finally, provide an indication of how well the model performs on the evaluation dataset. If the model uses a decision threshold, either provide the decision threshold used in the evaluation, or provide details on evaluation at different thresholds for the intended uses. ## [](#example)Example Check out the following for a few examples of well-crafted model cards: - [`bert-base-cased`](https://huggingface.co/bert-base-cased) - [`gpt2`](https://huggingface.co/gpt2) - [`distilbert`](https://huggingface.co/distilbert-base-uncased) More examples from different organizations and companies are available [here](https://github.com/huggingface/model_card/blob/master/examples.md). ## [](#note)Note Model cards are not a requirement when publishing models, and you don’t need to include all of the sections described above when you make one. However, explicit documentation of the model can only benefit future users, so we recommend that you fill in as many of the sections as possible to the best of your knowledge and ability. ## [](#model-card-metadata)Model card metadata If you have done a little exploring of the Hugging Face Hub, you should have seen that some models belong to certain categories: you can filter them by tasks, languages, libraries, and more. The categories a model belongs to are identified according to the metadata you add in the model card header. For example, if you take a look at the [`camembert-base` model card](https://huggingface.co/camembert-base/blob/main/README.md), you should see the following lines in the model card header: ``` --- language: fr license: mit datasets: - oscar ---``` This metadata is parsed by the Hugging Face Hub, which then identifies this model as being a French model, with an MIT license, trained on the Oscar dataset. The [full model card specification](https://github.com/huggingface/hub-docs/blame/main/modelcard.md) allows specifying languages, licenses, tags, datasets, metrics, as well as the evaluation results the model obtained when training.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Building a model card - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter4/4"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Building a model card - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;building-a-model-card&quot;,&quot;sections&quot;:[{&quot;local&quot;:null,&quot;sections&quot;:[{&quot;local&quot;:&quot;model-description&quot;,&quot;title&quot;:&quot;Model description&quot;},{&quot;local&quot;:&quot;intended-uses-limitations&quot;,&quot;title&quot;:&quot;Intended uses &amp; limitations&quot;},{&quot;local&quot;:&quot;how-to-use&quot;,&quot;title&quot;:&quot;How to use&quot;},{&quot;local&quot;:&quot;training-data&quot;,&quot;title&quot;:&quot;Training data&quot;},{&quot;local&quot;:&quot;training-procedure&quot;,&quot;title&quot;:&quot;Training procedure&quot;},{&quot;local&quot;:&quot;variable-and-metrics&quot;,&quot;title&quot;:&quot;Variable and metrics&quot;},{&quot;local&quot;:&quot;evaluation-results&quot;,&quot;title&quot;:&quot;Evaluation results&quot;}],&quot;title&quot;:null},{&quot;local&quot;:&quot;example&quot;,&quot;title&quot;:&quot;Example&quot;},{&quot;local&quot;:&quot;note&quot;,&quot;title&quot;:&quot;Note&quot;},{&quot;local&quot;:&quot;model-card-metadata&quot;,&quot;title&quot;:&quot;Model card metadata&quot;}],&quot;title&quot;:&quot;Building a model card&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter4/4&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Building a model card&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Building a model card</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/1?fw=pt">The Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/2?fw=pt">Using pretrained models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/3?fw=pt">Sharing pretrained models </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter4/4?fw=pt">Building a model card </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/5?fw=pt">Part 1 completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/6?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="building-a-model-card" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#building-a-model-card"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Building a model card</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-4-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>The model card is a file which is arguably as important as the model and tokenizer files in a model repository. It is the central definition of the model, ensuring reusability by fellow community members and reproducibility of results, and providing a platform on which other members may build their artifacts.</p> <p>Documenting the training and evaluation process helps others understand what to expect of a model — and providing sufficient information regarding the data that was used and the preprocessing and postprocessing that were done ensures that the limitations, biases, and contexts in which the model is and is not useful can be identified and understood.</p> <p>Therefore, creating a model card that clearly defines your model is a very important step. Here, we provide some tips that will help you with this. Creating the model card is done through the <em>README.md</em> file you saw earlier, which is a Markdown file.</p> <p>The “model card” concept originates from a research direction from Google, first shared in the paper <a href="https://arxiv.org/abs/1810.03993" rel="nofollow">“Model Cards for Model Reporting”</a> by Margaret Mitchell et al. A lot of information contained here is based on that paper, and we recommend you take a look at it to understand why model cards are so important in a world that values reproducibility, reusability, and fairness.</p> <p>The model card usually starts with a very brief, high-level overview of what the model is for, followed by additional details in the following sections:</p> <ul><li>Model description</li> <li>Intended uses &amp; limitations</li> <li>How to use</li> <li>Limitations and bias</li> <li>Training data</li> <li>Training procedure</li> <li>Evaluation results</li></ul> <p>Let’s take a look at what each of these sections should contain.</p> <h3 class="relative group"><a id="model-description" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#model-description"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Model description</span></h3> <p>The model description provides basic details about the model. This includes the architecture, version, if it was introduced in a paper, if an original implementation is available, the author, and general information about the model. Any copyright should be attributed here. General information about training procedures, parameters, and important disclaimers can also be mentioned in this section.</p> <h3 class="relative group"><a id="intended-uses-limitations" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#intended-uses-limitations"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Intended uses &amp; limitations</span></h3> <p>Here you describe the use cases the model is intended for, including the languages, fields, and domains where it can be applied. This section of the model card can also document areas that are known to be out of scope for the model, or where it is likely to perform suboptimally.</p> <h3 class="relative group"><a id="how-to-use" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#how-to-use"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>How to use</span></h3> <p>This section should include some examples of how to use the model. This can showcase usage of the <code>pipeline()</code> function, usage of the model and tokenizer classes, and any other code you think might be helpful.</p> <h3 class="relative group"><a id="training-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training data</span></h3> <p>This part should indicate which dataset(s) the model was trained on. A brief description of the dataset(s) is also welcome.</p> <h3 class="relative group"><a id="training-procedure" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-procedure"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training procedure</span></h3> <p>In this section you should describe all the relevant aspects of training that are useful from a reproducibility perspective. This includes any preprocessing and postprocessing that were done on the data, as well as details such as the number of epochs the model was trained for, the batch size, the learning rate, and so on.</p> <h3 class="relative group"><a id="variable-and-metrics" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#variable-and-metrics"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Variable and metrics</span></h3> <p>Here you should describe the metrics you use for evaluation, and the different factors you are mesuring. Mentioning which metric(s) were used, on which dataset and which dataset split, makes it easy to compare you model’s performance compared to that of other models. These should be informed by the previous sections, such as the intended users and use cases.</p> <h3 class="relative group"><a id="evaluation-results" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#evaluation-results"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Evaluation results</span></h3> <p>Finally, provide an indication of how well the model performs on the evaluation dataset. If the model uses a decision threshold, either provide the decision threshold used in the evaluation, or provide details on evaluation at different thresholds for the intended uses.</p> <h2 class="relative group"><a id="example" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#example"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Example</span></h2> <p>Check out the following for a few examples of well-crafted model cards:</p> <ul><li><a href="https://huggingface.co/bert-base-cased" rel="nofollow"><code>bert-base-cased</code></a></li> <li><a href="https://huggingface.co/gpt2" rel="nofollow"><code>gpt2</code></a></li> <li><a href="https://huggingface.co/distilbert-base-uncased" rel="nofollow"><code>distilbert</code></a></li></ul> <p>More examples from different organizations and companies are available <a href="https://github.com/huggingface/model_card/blob/master/examples.md" rel="nofollow">here</a>.</p> <h2 class="relative group"><a id="note" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#note"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Note</span></h2> <p>Model cards are not a requirement when publishing models, and you don’t need to include all of the sections described above when you make one. However, explicit documentation of the model can only benefit future users, so we recommend that you fill in as many of the sections as possible to the best of your knowledge and ability.</p> <h2 class="relative group"><a id="model-card-metadata" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#model-card-metadata"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Model card metadata</span></h2> <p>If you have done a little exploring of the Hugging Face Hub, you should have seen that some models belong to certain categories: you can filter them by tasks, languages, libraries, and more. The categories a model belongs to are identified according to the metadata you add in the model card header.</p> <p>For example, if you take a look at the <a href="https://huggingface.co/camembert-base/blob/main/README.md" rel="nofollow"><code>camembert-base</code> model card</a>, you should see the following lines in the model card header:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">---</span> <span class="hljs-attr">language:</span> <span class="hljs-string">fr</span> <span class="hljs-attr">license:</span> <span class="hljs-string">mit</span> <span class="hljs-attr">datasets:</span> <span class="hljs-bullet">-</span> <span class="hljs-string">oscar</span> <span class="hljs-meta">---</span></pre></div> <p>This metadata is parsed by the Hugging Face Hub, which then identifies this model as being a French model, with an MIT license, trained on the Oscar dataset.</p> <p>The <a href="https://github.com/huggingface/hub-docs/blame/main/modelcard.md" rel="nofollow">full model card specification</a> allows specifying languages, licenses, tags, datasets, metrics, as well as the evaluation results the model obtained when training.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter4/3?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Sharing pretrained models</a> <a href="/learn/nlp-course/chapter4/5?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Part 1 completed!<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Building a model card&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;building-a-model-card&quot;,&quot;url&quot;:&quot;#building-a-model-card&quot;,&quot;sections&quot;:[{&quot;title&quot;:null,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Model description&quot;,&quot;id&quot;:&quot;model-description&quot;,&quot;url&quot;:&quot;#model-description&quot;},{&quot;title&quot;:&quot;Intended uses &amp; limitations&quot;,&quot;id&quot;:&quot;intended-uses-limitations&quot;,&quot;url&quot;:&quot;#intended-uses-limitations&quot;},{&quot;title&quot;:&quot;How to use&quot;,&quot;id&quot;:&quot;how-to-use&quot;,&quot;url&quot;:&quot;#how-to-use&quot;},{&quot;title&quot;:&quot;Training data&quot;,&quot;id&quot;:&quot;training-data&quot;,&quot;url&quot;:&quot;#training-data&quot;},{&quot;title&quot;:&quot;Training procedure&quot;,&quot;id&quot;:&quot;training-procedure&quot;,&quot;url&quot;:&quot;#training-procedure&quot;},{&quot;title&quot;:&quot;Variable and metrics&quot;,&quot;id&quot;:&quot;variable-and-metrics&quot;,&quot;url&quot;:&quot;#variable-and-metrics&quot;},{&quot;title&quot;:&quot;Evaluation results&quot;,&quot;id&quot;:&quot;evaluation-results&quot;,&quot;url&quot;:&quot;#evaluation-results&quot;}]},{&quot;title&quot;:&quot;Example&quot;,&quot;id&quot;:&quot;example&quot;,&quot;url&quot;:&quot;#example&quot;},{&quot;title&quot;:&quot;Note&quot;,&quot;id&quot;:&quot;note&quot;,&quot;url&quot;:&quot;#note&quot;},{&quot;title&quot;:&quot;Model card metadata&quot;,&quot;id&quot;:&quot;model-card-metadata&quot;,&quot;url&quot;:&quot;#model-card-metadata&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#building-a-model-card" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-building-a-model-card"><wbr>Building a model card</a> <a href="#model-description" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-model-description"><wbr>Model description</a> <a href="#intended-uses-limitations" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-intended-uses-limitations"><wbr>Intended uses &amp; limitations</a> <a href="#how-to-use" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-how-to-use"><wbr>How to use</a> <a href="#training-data" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-data"><wbr>Training data</a> <a href="#training-procedure" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-procedure"><wbr>Training procedure</a> <a href="#variable-and-metrics" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-variable-and-metrics"><wbr>Variable and metrics</a> <a href="#evaluation-results" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-evaluation-results"><wbr>Evaluation results</a> <a href="#example" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-example"><wbr>Example</a> <a href="#note" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-note"><wbr>Note</a> <a href="#model-card-metadata" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-model-card-metadata"><wbr>Model card metadata</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter4/4" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter4/4"); } </script> <iframe name="__privateStripeMetricsController7130" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter4%2F4%3Ffw%3Dpt&amp;title=Building%20a%20model%20card%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:16.231Z
Part 1 completed! - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter4/5?fw=pt
## [](#part-1-completed)Part 1 completed! [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-4-questions) This is the end of the first part of the course! Part 2 will be released on November 15th with a big community event, see more information [here](https://huggingface.co/blog/course-launch-event). You should now be able to fine-tune a pretrained model on a text classification problem (single or pairs of sentences) and upload the result to the Model Hub. To make sure you mastered this first section, you should do exactly that on a problem that interests you (and not necessarily in English if you speak another language)! You can find help in the [Hugging Face forums](https://discuss.huggingface.co/) and share your project in [this topic](https://discuss.huggingface.co/t/share-your-projects/6803) once you’re finished. We can’t wait to see what you will build with this!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Part 1 completed! - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter4/5"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Part 1 completed! - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;part-1-completed&quot;,&quot;title&quot;:&quot;Part 1 completed!&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter4/5&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Part 1 completed!&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Part 1 completed!</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/1?fw=pt">The Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/2?fw=pt">Using pretrained models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/3?fw=pt">Sharing pretrained models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/4?fw=pt">Building a model card </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter4/5?fw=pt">Part 1 completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/6?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="part-1-completed" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#part-1-completed"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Part 1 completed!</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-4-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>This is the end of the first part of the course! Part 2 will be released on November 15th with a big community event, see more information <a href="https://huggingface.co/blog/course-launch-event" rel="nofollow">here</a>.</p> <p>You should now be able to fine-tune a pretrained model on a text classification problem (single or pairs of sentences) and upload the result to the Model Hub. To make sure you mastered this first section, you should do exactly that on a problem that interests you (and not necessarily in English if you speak another language)! You can find help in the <a href="https://discuss.huggingface.co/" rel="nofollow">Hugging Face forums</a> and share your project in <a href="https://discuss.huggingface.co/t/share-your-projects/6803" rel="nofollow">this topic</a> once you’re finished.</p> <p>We can’t wait to see what you will build with this!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter4/4?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Building a model card</a> <a href="/learn/nlp-course/chapter4/6?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">End-of-chapter quiz<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;part-1-completed&quot;,&quot;url&quot;:&quot;#part-1-completed&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#part-1-completed" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-part-1-completed"><wbr>Part 1 completed!</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter4/5" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter4/5"); } </script> <iframe name="__privateStripeMetricsController8100" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter4%2F5%3Ffw%3Dpt&amp;title=Part%201%20completed!%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:16.566Z
End-of-chapter quiz - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter4/6?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#end-of-chapter-quiz)End-of-chapter quiz [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-4-questions) Let’s test what you learned in this chapter! ### [](#1.-what-are-models-on-the-hub-limited-to?)1\. What are models on the Hub limited to? ### [](#2.-how-can-you-manage-models-on-the-hub?)2\. How can you manage models on the Hub? ### [](#3.-what-can-you-do-using-the-hugging-face-hub-web-interface?)3\. What can you do using the Hugging Face Hub web interface? ### [](#4.-what-is-a-model-card?)4\. What is a model card? ### [](#5.-which-of-these-objects-of-the-🤗-transformers-library-can-be-directly-shared-on-the-hub-with-<code>push_to_hub()</code>?)5\. Which of these objects of the 🤗 Transformers library can be directly shared on the Hub with `push_to_hub()`? ### [](#6.-what-is-the-first-step-when-using-the-<code>push_to_hub()</code>-method-or-the-cli-tools?)6\. What is the first step when using the `push_to_hub()` method or the CLI tools? ### [](#7.-you’re-using-a-model-and-a-tokenizer-—-how-can-you-upload-them-to-the-hub?)7\. You’re using a model and a tokenizer — how can you upload them to the Hub? ### [](#8.-which-git-operations-can-you-do-with-the-<code>repository</code>-class?)8\. Which git operations can you do with the `Repository` class?
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="End-of-chapter quiz - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter4/6"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>End-of-chapter quiz - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;end-of-chapter-quiz&quot;,&quot;title&quot;:&quot;End-of-chapter quiz&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter4/6&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;End-of-chapter quiz&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">End-of-chapter quiz</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/1?fw=pt">The Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/2?fw=pt">Using pretrained models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/3?fw=pt">Sharing pretrained models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/4?fw=pt">Building a model card </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter4/5?fw=pt">Part 1 completed! </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter4/6?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="end-of-chapter-quiz" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#end-of-chapter-quiz"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>End-of-chapter quiz</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-4-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>Let’s test what you learned in this chapter!</p> <h3 class="relative group"><a id="1.-what-are-models-on-the-hub-limited-to?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#1.-what-are-models-on-the-hub-limited-to?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>1. What are models on the Hub limited to?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Models from the 🤗 Transformers library.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> All models with a similar interface to 🤗 Transformers.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> There are no limits.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Models that are in some way related to NLP.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="2.-how-can-you-manage-models-on-the-hub?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#2.-how-can-you-manage-models-on-the-hub?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>2. How can you manage models on the Hub?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Through a GCP account.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Through peer-to-peer distribution.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Through git and git-lfs.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="3.-what-can-you-do-using-the-hugging-face-hub-web-interface?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#3.-what-can-you-do-using-the-hugging-face-hub-web-interface?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>3. What can you do using the Hugging Face Hub web interface?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Fork an existing repository.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Create a new model repository.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Manage and edit files.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Upload files.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="4"> See diffs across versions.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="4.-what-is-a-model-card?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#4.-what-is-a-model-card?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>4. What is a model card?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> A rough description of the model, therefore less important than the model and tokenizer files.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> A way to ensure reproducibility, reusability, and fairness.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A Python file that can be run to retrieve information about the model.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="5.-which-of-these-objects-of-the-🤗-transformers-library-can-be-directly-shared-on-the-hub-with-<code>push_to_hub()</code>?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#5.-which-of-these-objects-of-the-🤗-transformers-library-can-be-directly-shared-on-the-hub-with-<code>push_to_hub()</code>?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>5. Which of these objects of the 🤗 Transformers library can be directly shared on the Hub with <code>push_to_hub()</code>?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> A tokenizer</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> A model configuration</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A model</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> A Trainer</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="6.-what-is-the-first-step-when-using-the-<code>push_to_hub()</code>-method-or-the-cli-tools?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#6.-what-is-the-first-step-when-using-the-<code>push_to_hub()</code>-method-or-the-cli-tools?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>6. What is the first step when using the <code>push_to_hub()</code> method or the CLI tools?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Log in on the website.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Run 'huggingface-cli login' in a terminal.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Run 'notebook_login()' in a notebook.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="7.-you’re-using-a-model-and-a-tokenizer-—-how-can-you-upload-them-to-the-hub?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#7.-you’re-using-a-model-and-a-tokenizer-—-how-can-you-upload-them-to-the-hub?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>7. You’re using a model and a tokenizer — how can you upload them to the Hub?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> By calling the push_to_hub method directly on the model and the tokenizer.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Within the Python runtime, by wrapping them in a <code>huggingface_hub</code> utility.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> By saving them to disk and calling <code>transformers-cli upload-model</code></label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="8.-which-git-operations-can-you-do-with-the-<code>repository</code>-class?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#8.-which-git-operations-can-you-do-with-the-<code>repository</code>-class?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>8. Which git operations can you do with the <code>Repository</code> class?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> A commit.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> A pull</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A push</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> A merge</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter4/5?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Part 1 completed!</a> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isLoggedIn&quot;:false,&quot;nextChapterUrl&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;,&quot;quizChapter&quot;:4}" data-target="CourseCompleteButton"><div class="relative ml-auto"> <button class="btn btn-lg" disabled=""><svg class="mr-2" width="1em" height="1em" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0 16C0 7.16344 7.16344 0 16 0V0C24.8366 0 32 7.16344 32 16V16C32 24.8366 24.8366 32 16 32V32C7.16344 32 0 24.8366 0 16V16Z" fill="#D9FFD2"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M23.5607 10.9393C24.1464 11.5251 24.1464 12.4749 23.5607 13.0607L14.5 22.1213L8.93934 16.5607C8.35355 15.9749 8.35355 15.0251 8.93934 14.4393C9.52513 13.8536 10.4749 13.8536 11.0607 14.4393L14.5 17.8787L21.4393 10.9393C22.0251 10.3536 22.9749 10.3536 23.5607 10.9393Z" fill="#188B3F"></path></svg> Complete Chapter</button></div></div> <a href="/learn/nlp-course/chapter5/1?fw=pt" class="hidden">Next chapter</a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;id&quot;:&quot;end-of-chapter-quiz&quot;,&quot;url&quot;:&quot;#end-of-chapter-quiz&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#end-of-chapter-quiz" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-end-of-chapter-quiz"><wbr>End-of-chapter quiz</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter4/6" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter4/6"); } </script> <iframe name="__privateStripeMetricsController6990" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter4%2F6%3Ffw%3Dpt&amp;title=End-of-chapter%20quiz%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:16.904Z
Introduction - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter5/1?fw=pt
## [](#introduction)Introduction [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-5-questions) In [Chapter 3](/course/chapter3) you got your first taste of the 🤗 Datasets library and saw that there were three main steps when it came to fine-tuning a model: 1. Load a dataset from the Hugging Face Hub. 2. Preprocess the data with `Dataset.map()`. 3. Load and compute metrics. But this is just scratching the surface of what 🤗 Datasets can do! In this chapter, we will take a deep dive into the library. Along the way, we’ll find answers to the following questions: - What do you do when your dataset is not on the Hub? - How can you slice and dice a dataset? (And what if you _really_ need to use Pandas?) - What do you do when your dataset is huge and will melt your laptop’s RAM? - What the heck are “memory mapping” and Apache Arrow? - How can you create your own dataset and push it to the Hub? The techniques you learn here will prepare you for the advanced tokenization and fine-tuning tasks in [Chapter 6](/course/chapter6) and [Chapter 7](/course/chapter7) — so grab a coffee and let’s get started!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Introduction - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter5/1"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Introduction - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;introduction&quot;,&quot;title&quot;:&quot;Introduction&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter5/1&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Introduction&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Introduction</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter5/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/2?fw=pt">What if my dataset isn't on the Hub? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/3?fw=pt">Time to slice and dice </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/4?fw=pt">Big data? 🤗 Datasets to the rescue! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/5?fw=pt">Creating your own dataset </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/6?fw=pt">Semantic search with FAISS </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/7?fw=pt">🤗 Datasets, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="introduction" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#introduction"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Introduction</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-5-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>In <a href="/course/chapter3">Chapter 3</a> you got your first taste of the 🤗 Datasets library and saw that there were three main steps when it came to fine-tuning a model:</p> <ol><li>Load a dataset from the Hugging Face Hub.</li> <li>Preprocess the data with <code>Dataset.map()</code>.</li> <li>Load and compute metrics.</li></ol> <p>But this is just scratching the surface of what 🤗 Datasets can do! In this chapter, we will take a deep dive into the library. Along the way, we’ll find answers to the following questions:</p> <ul><li>What do you do when your dataset is not on the Hub?</li> <li>How can you slice and dice a dataset? (And what if you <em>really</em> need to use Pandas?)</li> <li>What do you do when your dataset is huge and will melt your laptop’s RAM?</li> <li>What the heck are “memory mapping” and Apache Arrow?</li> <li>How can you create your own dataset and push it to the Hub?</li></ul> <p>The techniques you learn here will prepare you for the advanced tokenization and fine-tuning tasks in <a href="/course/chapter6">Chapter 6</a> and <a href="/course/chapter7">Chapter 7</a> — so grab a coffee and let’s get started!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter4/6?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>End-of-chapter quiz</a> <a href="/learn/nlp-course/chapter5/2?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">What if my dataset isn't on the Hub?<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;introduction&quot;,&quot;url&quot;:&quot;#introduction&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#introduction" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-introduction"><wbr>Introduction</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter5/1" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter5/1"); } </script> <iframe name="__privateStripeMetricsController3460" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter5%2F1%3Ffw%3Dpt&amp;title=Introduction%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:17.511Z
What if my dataset isn't on the Hub? - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter5/2?fw=pt
## [](#what-if-my-dataset-isnt-on-the-hub)What if my dataset isn't on the Hub? [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-5-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter5/section2.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter5/section2.ipynb) You know how to use the [Hugging Face Hub](https://huggingface.co/datasets) to download datasets, but you’ll often find yourself working with data that is stored either on your laptop or on a remote server. In this section we’ll show you how 🤗 Datasets can be used to load datasets that aren’t available on the Hugging Face Hub. ## [](#working-with-local-and-remote-datasets)Working with local and remote datasets 🤗 Datasets provides loading scripts to handle the loading of local and remote datasets. It supports several common data formats, such as: | Data format | Loading script | Example | | --- | --- | --- | | CSV & TSV | `csv` | `load_dataset("csv", data_files="my_file.csv")` | | Text files | `text` | `load_dataset("text", data_files="my_file.txt")` | | JSON & JSON Lines | `json` | `load_dataset("json", data_files="my_file.jsonl")` | | Pickled DataFrames | `pandas` | `load_dataset("pandas", data_files="my_dataframe.pkl")` | As shown in the table, for each data format we just need to specify the type of loading script in the `load_dataset()` function, along with a `data_files` argument that specifies the path to one or more files. Let’s start by loading a dataset from local files; later we’ll see how to do the same with remote files. ## [](#loading-a-local-dataset)Loading a local dataset For this example we’ll use the [SQuAD-it dataset](https://github.com/crux82/squad-it/), which is a large-scale dataset for question answering in Italian. The training and test splits are hosted on GitHub, so we can download them with a simple `wget` command: ``` !wget https://github.com/crux82/squad-it/raw/master/SQuAD_it-train.json.gz !wget https://github.com/crux82/squad-it/raw/master/SQuAD_it-test.json.gz``` This will download two compressed files called _SQuAD\_it-train.json.gz_ and _SQuAD\_it-test.json.gz_, which we can decompress with the Linux `gzip` command: ``` !gzip -dkv SQuAD_it-*.json.gz``` ``` SQuAD_it-test.json.gz: 87.4% -- replaced with SQuAD_it-test.json SQuAD_it-train.json.gz: 82.2% -- replaced with SQuAD_it-train.json``` We can see that the compressed files have been replaced with _SQuAD\_it-train.json_ and _SQuAD\_it-test.json_, and that the data is stored in the JSON format. ✎ If you’re wondering why there’s a `!` character in the above shell commands, that’s because we’re running them within a Jupyter notebook. Simply remove the prefix if you want to download and unzip the dataset within a terminal. To load a JSON file with the `load_dataset()` function, we just need to know if we’re dealing with ordinary JSON (similar to a nested dictionary) or JSON Lines (line-separated JSON). Like many question answering datasets, SQuAD-it uses the nested format, with all the text stored in a `data` field. This means we can load the dataset by specifying the `field` argument as follows: ``` from datasets import load_dataset squad_it_dataset = load_dataset("json", data_files="SQuAD_it-train.json", field="data")``` By default, loading local files creates a `DatasetDict` object with a `train` split. We can see this by inspecting the `squad_it_dataset` object: ``` DatasetDict({ train: Dataset({ features: ['title', 'paragraphs'], num_rows: 442 }) })``` This shows us the number of rows and the column names associated with the training set. We can view one of the examples by indexing into the `train` split as follows: ``` squad_it_dataset["train"][0]``` ``` { "title": "Terremoto del Sichuan del 2008", "paragraphs": [ { "context": "Il terremoto del Sichuan del 2008 o il terremoto...", "qas": [ { "answers": [{"answer_start": 29, "text": "2008"}], "id": "56cdca7862d2951400fa6826", "question": "In quale anno si è verificato il terremoto nel Sichuan?", }, ... ], }, ... ], }``` Great, we’ve loaded our first local dataset! But while this worked for the training set, what we really want is to include both the `train` and `test` splits in a single `DatasetDict` object so we can apply `Dataset.map()` functions across both splits at once. To do this, we can provide a dictionary to the `data_files` argument that maps each split name to a file associated with that split: ``` data_files = {"train": "SQuAD_it-train.json", "test": "SQuAD_it-test.json"} squad_it_dataset = load_dataset("json", data_files=data_files, field="data") squad_it_dataset``` ``` DatasetDict({ train: Dataset({ features: ['title', 'paragraphs'], num_rows: 442 }) test: Dataset({ features: ['title', 'paragraphs'], num_rows: 48 }) })``` This is exactly what we wanted. Now, we can apply various preprocessing techniques to clean up the data, tokenize the reviews, and so on. The `data_files` argument of the `load_dataset()` function is quite flexible and can be either a single file path, a list of file paths, or a dictionary that maps split names to file paths. You can also glob files that match a specified pattern according to the rules used by the Unix shell (e.g., you can glob all the JSON files in a directory as a single split by setting `data_files="*.json"`). See the 🤗 Datasets [documentation](https://huggingface.co/docs/datasets/loading.html#local-and-remote-files) for more details. The loading scripts in 🤗 Datasets actually support automatic decompression of the input files, so we could have skipped the use of `gzip` by pointing the `data_files` argument directly to the compressed files: ``` data_files = {"train": "SQuAD_it-train.json.gz", "test": "SQuAD_it-test.json.gz"} squad_it_dataset = load_dataset("json", data_files=data_files, field="data")``` This can be useful if you don’t want to manually decompress many GZIP files. The automatic decompression also applies to other common formats like ZIP and TAR, so you just need to point `data_files` to the compressed files and you’re good to go! Now that you know how to load local files on your laptop or desktop, let’s take a look at loading remote files. ## [](#loading-a-remote-dataset)Loading a remote dataset If you’re working as a data scientist or coder in a company, there’s a good chance the datasets you want to analyze are stored on some remote server. Fortunately, loading remote files is just as simple as loading local ones! Instead of providing a path to local files, we point the `data_files` argument of `load_dataset()` to one or more URLs where the remote files are stored. For example, for the SQuAD-it dataset hosted on GitHub, we can just point `data_files` to the _SQuAD\_it-\*.json.gz_ URLs as follows: ``` url = "https://github.com/crux82/squad-it/raw/master/" data_files = { "train": url + "SQuAD_it-train.json.gz", "test": url + "SQuAD_it-test.json.gz", } squad_it_dataset = load_dataset("json", data_files=data_files, field="data")``` This returns the same `DatasetDict` object obtained above, but saves us the step of manually downloading and decompressing the _SQuAD\_it-\*.json.gz_ files. This wraps up our foray into the various ways to load datasets that aren’t hosted on the Hugging Face Hub. Now that we’ve got a dataset to play with, let’s get our hands dirty with various data-wrangling techniques! ✏️ **Try it out!** Pick another dataset hosted on GitHub or the [UCI Machine Learning Repository](https://archive.ics.uci.edu/ml/index.php) and try loading it both locally and remotely using the techniques introduced above. For bonus points, try loading a dataset that’s stored in a CSV or text format (see the [documentation](https://huggingface.co/docs/datasets/loading.html#local-and-remote-files) for more information on these formats).
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="What if my dataset isn't on the Hub? - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter5/2"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>What if my dataset isn't on the Hub? - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;what-if-my-dataset-isnt-on-the-hub&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;working-with-local-and-remote-datasets&quot;,&quot;title&quot;:&quot;Working with local and remote datasets&quot;},{&quot;local&quot;:&quot;loading-a-local-dataset&quot;,&quot;title&quot;:&quot;Loading a local dataset&quot;},{&quot;local&quot;:&quot;loading-a-remote-dataset&quot;,&quot;title&quot;:&quot;Loading a remote dataset&quot;}],&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter5/2&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">What if my dataset isn't on the Hub?</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/1?fw=pt">Introduction </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter5/2?fw=pt">What if my dataset isn't on the Hub? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/3?fw=pt">Time to slice and dice </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/4?fw=pt">Big data? 🤗 Datasets to the rescue! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/5?fw=pt">Creating your own dataset </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/6?fw=pt">Semantic search with FAISS </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/7?fw=pt">🤗 Datasets, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="what-if-my-dataset-isnt-on-the-hub" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#what-if-my-dataset-isnt-on-the-hub"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>What if my dataset isn't on the Hub?</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-5-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter5/section2.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter5/section2.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>You know how to use the <a href="https://huggingface.co/datasets" rel="nofollow">Hugging Face Hub</a> to download datasets, but you’ll often find yourself working with data that is stored either on your laptop or on a remote server. In this section we’ll show you how 🤗 Datasets can be used to load datasets that aren’t available on the Hugging Face Hub.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/HyQgpJTkRdE" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <h2 class="relative group"><a id="working-with-local-and-remote-datasets" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#working-with-local-and-remote-datasets"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Working with local and remote datasets</span></h2> <p>🤗 Datasets provides loading scripts to handle the loading of local and remote datasets. It supports several common data formats, such as:</p> <table><thead><tr><th align="center">Data format</th> <th align="center">Loading script</th> <th align="center">Example</th></tr></thead> <tbody><tr><td align="center">CSV &amp; TSV</td> <td align="center"><code>csv</code></td> <td align="center"><code>load_dataset("csv", data_files="my_file.csv")</code></td></tr> <tr><td align="center">Text files</td> <td align="center"><code>text</code></td> <td align="center"><code>load_dataset("text", data_files="my_file.txt")</code></td></tr> <tr><td align="center">JSON &amp; JSON Lines</td> <td align="center"><code>json</code></td> <td align="center"><code>load_dataset("json", data_files="my_file.jsonl")</code></td></tr> <tr><td align="center">Pickled DataFrames</td> <td align="center"><code>pandas</code></td> <td align="center"><code>load_dataset("pandas", data_files="my_dataframe.pkl")</code></td></tr></tbody></table> <p>As shown in the table, for each data format we just need to specify the type of loading script in the <code>load_dataset()</code> function, along with a <code>data_files</code> argument that specifies the path to one or more files. Let’s start by loading a dataset from local files; later we’ll see how to do the same with remote files.</p> <h2 class="relative group"><a id="loading-a-local-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-a-local-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Loading a local dataset</span></h2> <p>For this example we’ll use the <a href="https://github.com/crux82/squad-it/" rel="nofollow">SQuAD-it dataset</a>, which is a large-scale dataset for question answering in Italian.</p> <p>The training and test splits are hosted on GitHub, so we can download them with a simple <code>wget</code> command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!wget https://github.com/crux82/squad-it/raw/master/SQuAD_it-train.json.gz !wget https://github.com/crux82/squad-it/raw/master/SQuAD_it-test.json.gz</pre></div> <p>This will download two compressed files called <em>SQuAD_it-train.json.gz</em> and <em>SQuAD_it-test.json.gz</em>, which we can decompress with the Linux <code>gzip</code> command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!gzip -dkv SQuAD_it-*.json.gz</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>SQuAD_it-test.json.gz: 87.4% -- replaced with SQuAD_it-test.json SQuAD_it-train.json.gz: 82.2% -- replaced with SQuAD_it-train.json</pre></div> <p>We can see that the compressed files have been replaced with <em>SQuAD_it-train.json</em> and <em>SQuAD_it-test.json</em>, and that the data is stored in the JSON format.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✎ If you’re wondering why there’s a <code>!</code> character in the above shell commands, that’s because we’re running them within a Jupyter notebook. Simply remove the prefix if you want to download and unzip the dataset within a terminal.</p></div> <p>To load a JSON file with the <code>load_dataset()</code> function, we just need to know if we’re dealing with ordinary JSON (similar to a nested dictionary) or JSON Lines (line-separated JSON). Like many question answering datasets, SQuAD-it uses the nested format, with all the text stored in a <code>data</code> field. This means we can load the dataset by specifying the <code>field</code> argument as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset squad_it_dataset = load_dataset(<span class="hljs-string">"json"</span>, data_files=<span class="hljs-string">"SQuAD_it-train.json"</span>, field=<span class="hljs-string">"data"</span>)</pre></div> <p>By default, loading local files creates a <code>DatasetDict</code> object with a <code>train</code> split. We can see this by inspecting the <code>squad_it_dataset</code> object:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>squad_it_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'title'</span>, <span class="hljs-string">'paragraphs'</span>], num_rows: <span class="hljs-number">442</span> }) })</pre></div> <p>This shows us the number of rows and the column names associated with the training set. We can view one of the examples by indexing into the <code>train</code> split as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>squad_it_dataset[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{ <span class="hljs-string">"title"</span>: <span class="hljs-string">"Terremoto del Sichuan del 2008"</span>, <span class="hljs-string">"paragraphs"</span>: [ { <span class="hljs-string">"context"</span>: <span class="hljs-string">"Il terremoto del Sichuan del 2008 o il terremoto..."</span>, <span class="hljs-string">"qas"</span>: [ { <span class="hljs-string">"answers"</span>: [{<span class="hljs-string">"answer_start"</span>: <span class="hljs-number">29</span>, <span class="hljs-string">"text"</span>: <span class="hljs-string">"2008"</span>}], <span class="hljs-string">"id"</span>: <span class="hljs-string">"56cdca7862d2951400fa6826"</span>, <span class="hljs-string">"question"</span>: <span class="hljs-string">"In quale anno si è verificato il terremoto nel Sichuan?"</span>, }, ... ], }, ... ], }</pre></div> <p>Great, we’ve loaded our first local dataset! But while this worked for the training set, what we really want is to include both the <code>train</code> and <code>test</code> splits in a single <code>DatasetDict</code> object so we can apply <code>Dataset.map()</code> functions across both splits at once. To do this, we can provide a dictionary to the <code>data_files</code> argument that maps each split name to a file associated with that split:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>data_files = {<span class="hljs-string">"train"</span>: <span class="hljs-string">"SQuAD_it-train.json"</span>, <span class="hljs-string">"test"</span>: <span class="hljs-string">"SQuAD_it-test.json"</span>} squad_it_dataset = load_dataset(<span class="hljs-string">"json"</span>, data_files=data_files, field=<span class="hljs-string">"data"</span>) squad_it_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'title'</span>, <span class="hljs-string">'paragraphs'</span>], num_rows: <span class="hljs-number">442</span> }) test: Dataset({ features: [<span class="hljs-string">'title'</span>, <span class="hljs-string">'paragraphs'</span>], num_rows: <span class="hljs-number">48</span> }) })</pre></div> <p>This is exactly what we wanted. Now, we can apply various preprocessing techniques to clean up the data, tokenize the reviews, and so on.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>The <code>data_files</code> argument of the <code>load_dataset()</code> function is quite flexible and can be either a single file path, a list of file paths, or a dictionary that maps split names to file paths. You can also glob files that match a specified pattern according to the rules used by the Unix shell (e.g., you can glob all the JSON files in a directory as a single split by setting <code>data_files="*.json"</code>). See the 🤗 Datasets <a href="https://huggingface.co/docs/datasets/loading.html#local-and-remote-files" rel="nofollow">documentation</a> for more details.</p></div> <p>The loading scripts in 🤗 Datasets actually support automatic decompression of the input files, so we could have skipped the use of <code>gzip</code> by pointing the <code>data_files</code> argument directly to the compressed files:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>data_files = {<span class="hljs-string">"train"</span>: <span class="hljs-string">"SQuAD_it-train.json.gz"</span>, <span class="hljs-string">"test"</span>: <span class="hljs-string">"SQuAD_it-test.json.gz"</span>} squad_it_dataset = load_dataset(<span class="hljs-string">"json"</span>, data_files=data_files, field=<span class="hljs-string">"data"</span>)</pre></div> <p>This can be useful if you don’t want to manually decompress many GZIP files. The automatic decompression also applies to other common formats like ZIP and TAR, so you just need to point <code>data_files</code> to the compressed files and you’re good to go!</p> <p>Now that you know how to load local files on your laptop or desktop, let’s take a look at loading remote files.</p> <h2 class="relative group"><a id="loading-a-remote-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-a-remote-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Loading a remote dataset</span></h2> <p>If you’re working as a data scientist or coder in a company, there’s a good chance the datasets you want to analyze are stored on some remote server. Fortunately, loading remote files is just as simple as loading local ones! Instead of providing a path to local files, we point the <code>data_files</code> argument of <code>load_dataset()</code> to one or more URLs where the remote files are stored. For example, for the SQuAD-it dataset hosted on GitHub, we can just point <code>data_files</code> to the <em>SQuAD_it-*.json.gz</em> URLs as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>url = <span class="hljs-string">"https://github.com/crux82/squad-it/raw/master/"</span> data_files = { <span class="hljs-string">"train"</span>: url + <span class="hljs-string">"SQuAD_it-train.json.gz"</span>, <span class="hljs-string">"test"</span>: url + <span class="hljs-string">"SQuAD_it-test.json.gz"</span>, } squad_it_dataset = load_dataset(<span class="hljs-string">"json"</span>, data_files=data_files, field=<span class="hljs-string">"data"</span>)</pre></div> <p>This returns the same <code>DatasetDict</code> object obtained above, but saves us the step of manually downloading and decompressing the <em>SQuAD_it-*.json.gz</em> files. This wraps up our foray into the various ways to load datasets that aren’t hosted on the Hugging Face Hub. Now that we’ve got a dataset to play with, let’s get our hands dirty with various data-wrangling techniques!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Pick another dataset hosted on GitHub or the <a href="https://archive.ics.uci.edu/ml/index.php" rel="nofollow">UCI Machine Learning Repository</a> and try loading it both locally and remotely using the techniques introduced above. For bonus points, try loading a dataset that’s stored in a CSV or text format (see the <a href="https://huggingface.co/docs/datasets/loading.html#local-and-remote-files" rel="nofollow">documentation</a> for more information on these formats).</p></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter5/1?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Introduction</a> <a href="/learn/nlp-course/chapter5/3?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Time to slice and dice<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;what-if-my-dataset-isnt-on-the-hub&quot;,&quot;url&quot;:&quot;#what-if-my-dataset-isnt-on-the-hub&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Working with local and remote datasets&quot;,&quot;id&quot;:&quot;working-with-local-and-remote-datasets&quot;,&quot;url&quot;:&quot;#working-with-local-and-remote-datasets&quot;},{&quot;title&quot;:&quot;Loading a local dataset&quot;,&quot;id&quot;:&quot;loading-a-local-dataset&quot;,&quot;url&quot;:&quot;#loading-a-local-dataset&quot;},{&quot;title&quot;:&quot;Loading a remote dataset&quot;,&quot;id&quot;:&quot;loading-a-remote-dataset&quot;,&quot;url&quot;:&quot;#loading-a-remote-dataset&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#what-if-my-dataset-isnt-on-the-hub" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-what-if-my-dataset-isnt-on-the-hub"><wbr>What if my dataset isn't on the <wbr>Hub?</a> <a href="#working-with-local-and-remote-datasets" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-working-with-local-and-remote-datasets"><wbr>Working with local and remote datasets</a> <a href="#loading-a-local-dataset" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-loading-a-local-dataset"><wbr>Loading a local dataset</a> <a href="#loading-a-remote-dataset" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-loading-a-remote-dataset"><wbr>Loading a remote dataset</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter5/2" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter5/2"); } </script> <iframe name="__privateStripeMetricsController9250" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter5%2F2%3Ffw%3Dpt&amp;title=What%20if%20my%20dataset%20isn't%20on%20the%20Hub%3F%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:18.400Z
Time to slice and dice - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter5/3?fw=pt
## [](#time-to-slice-and-dice)Time to slice and dice [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-5-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter5/section3.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter5/section3.ipynb) Most of the time, the data you work with won’t be perfectly prepared for training models. In this section we’ll explore the various features that 🤗 Datasets provides to clean up your datasets. ## [](#slicing-and-dicing-our-data)Slicing and dicing our data Similar to Pandas, 🤗 Datasets provides several functions to manipulate the contents of `Dataset` and `DatasetDict` objects. We already encountered the `Dataset.map()` method in [Chapter 3](/course/chapter3), and in this section we’ll explore some of the other functions at our disposal. For this example we’ll use the [Drug Review Dataset](https://archive.ics.uci.edu/ml/datasets/Drug+Review+Dataset+%28Drugs.com%29) that’s hosted on the [UC Irvine Machine Learning Repository](https://archive.ics.uci.edu/ml/index.php), which contains patient reviews on various drugs, along with the condition being treated and a 10-star rating of the patient’s satisfaction. First we need to download and extract the data, which can be done with the `wget` and `unzip` commands: ``` !wget "https://archive.ics.uci.edu/ml/machine-learning-databases/00462/drugsCom_raw.zip" !unzip drugsCom_raw.zip``` Since TSV is just a variant of CSV that uses tabs instead of commas as the separator, we can load these files by using the `csv` loading script and specifying the `delimiter` argument in the `load_dataset()` function as follows: ``` from datasets import load_dataset data_files = {"train": "drugsComTrain_raw.tsv", "test": "drugsComTest_raw.tsv"} drug_dataset = load_dataset("csv", data_files=data_files, delimiter="\t")``` A good practice when doing any sort of data analysis is to grab a small random sample to get a quick feel for the type of data you’re working with. In 🤗 Datasets, we can create a random sample by chaining the `Dataset.shuffle()` and `Dataset.select()` functions together: ``` drug_sample = drug_dataset["train"].shuffle(seed=42).select(range(1000)) drug_sample[:3]``` ``` {'Unnamed: 0': [87571, 178045, 80482], 'drugName': ['Naproxen', 'Duloxetine', 'Mobic'], 'condition': ['Gout, Acute', 'ibromyalgia', 'Inflammatory Conditions'], 'review': ['"like the previous person mention, I&#039;m a strong believer of aleve, it works faster for my gout than the prescription meds I take. No more going to the doctor for refills.....Aleve works!"', '"I have taken Cymbalta for about a year and a half for fibromyalgia pain. It is great\r\nas a pain reducer and an anti-depressant, however, the side effects outweighed \r\nany benefit I got from it. I had trouble with restlessness, being tired constantly,\r\ndizziness, dry mouth, numbness and tingling in my feet, and horrible sweating. I am\r\nbeing weaned off of it now. Went from 60 mg to 30mg and now to 15 mg. I will be\r\noff completely in about a week. The fibro pain is coming back, but I would rather deal with it than the side effects."', '"I have been taking Mobic for over a year with no side effects other than an elevated blood pressure. I had severe knee and ankle pain which completely went away after taking Mobic. I attempted to stop the medication however pain returned after a few days."'], 'rating': [9.0, 3.0, 10.0], 'date': ['September 2, 2015', 'November 7, 2011', 'June 5, 2013'], 'usefulCount': [36, 13, 128]}``` Note that we’ve fixed the seed in `Dataset.shuffle()` for reproducibility purposes. `Dataset.select()` expects an iterable of indices, so we’ve passed `range(1000)` to grab the first 1,000 examples from the shuffled dataset. From this sample we can already see a few quirks in our dataset: - The `Unnamed: 0` column looks suspiciously like an anonymized ID for each patient. - The `condition` column includes a mix of uppercase and lowercase labels. - The reviews are of varying length and contain a mix of Python line separators (`\r\n`) as well as HTML character codes like `&\#039;`. Let’s see how we can use 🤗 Datasets to deal with each of these issues. To test the patient ID hypothesis for the `Unnamed: 0` column, we can use the `Dataset.unique()` function to verify that the number of IDs matches the number of rows in each split: ``` for split in drug_dataset.keys(): assert len(drug_dataset[split]) == len(drug_dataset[split].unique("Unnamed: 0"))``` This seems to confirm our hypothesis, so let’s clean up the dataset a bit by renaming the `Unnamed: 0` column to something a bit more interpretable. We can use the `DatasetDict.rename_column()` function to rename the column across both splits in one go: ``` drug_dataset = drug_dataset.rename_column( original_column_name="Unnamed: 0", new_column_name="patient_id" ) drug_dataset``` ``` DatasetDict({ train: Dataset({ features: ['patient_id', 'drugName', 'condition', 'review', 'rating', 'date', 'usefulCount'], num_rows: 161297 }) test: Dataset({ features: ['patient_id', 'drugName', 'condition', 'review', 'rating', 'date', 'usefulCount'], num_rows: 53766 }) })``` ✏️ **Try it out!** Use the `Dataset.unique()` function to find the number of unique drugs and conditions in the training and test sets. Next, let’s normalize all the `condition` labels using `Dataset.map()`. As we did with tokenization in [Chapter 3](/course/chapter3), we can define a simple function that can be applied across all the rows of each split in `drug_dataset`: ``` def lowercase_condition(example): return {"condition": example["condition"].lower()} drug_dataset.map(lowercase_condition)``` ``` AttributeError: 'NoneType' object has no attribute 'lower'``` Oh no, we’ve run into a problem with our map function! From the error we can infer that some of the entries in the `condition` column are `None`, which cannot be lowercased as they’re not strings. Let’s drop these rows using `Dataset.filter()`, which works in a similar way to `Dataset.map()` and expects a function that receives a single example of the dataset. Instead of writing an explicit function like: ``` def filter_nones(x): return x["condition"] is not None``` and then running `drug_dataset.filter(filter_nones)`, we can do this in one line using a _lambda function_. In Python, lambda functions are small functions that you can define without explicitly naming them. They take the general form: ``` lambda <arguments> : <expression>``` where `lambda` is one of Python’s special [keywords](https://docs.python.org/3/reference/lexical_analysis.html#keywords), `<arguments>` is a list/set of comma-separated values that define the inputs to the function, and `<expression>` represents the operations you wish to execute. For example, we can define a simple lambda function that squares a number as follows: To apply this function to an input, we need to wrap it and the input in parentheses: Similarly, we can define lambda functions with multiple arguments by separating them with commas. For example, we can compute the area of a triangle as follows: ``` (lambda base, height: 0.5 * base * height)(4, 8)``` Lambda functions are handy when you want to define small, single-use functions (for more information about them, we recommend reading the excellent [Real Python tutorial](https://realpython.com/python-lambda/) by Andre Burgaud). In the 🤗 Datasets context, we can use lambda functions to define simple map and filter operations, so let’s use this trick to eliminate the `None` entries in our dataset: ``` drug_dataset = drug_dataset.filter(lambda x: x["condition"] is not None)``` With the `None` entries removed, we can normalize our `condition` column: ``` drug_dataset = drug_dataset.map(lowercase_condition) drug_dataset["train"]["condition"][:3]``` ``` ['left ventricular dysfunction', 'adhd', 'birth control']``` It works! Now that we’ve cleaned up the labels, let’s take a look at cleaning up the reviews themselves. ## [](#creating-new-columns)Creating new columns Whenever you’re dealing with customer reviews, a good practice is to check the number of words in each review. A review might be just a single word like “Great!” or a full-blown essay with thousands of words, and depending on the use case you’ll need to handle these extremes differently. To compute the number of words in each review, we’ll use a rough heuristic based on splitting each text by whitespace. Let’s define a simple function that counts the number of words in each review: ``` def compute_review_length(example): return {"review_length": len(example["review"].split())}``` Unlike our `lowercase_condition()` function, `compute_review_length()` returns a dictionary whose key does not correspond to one of the column names in the dataset. In this case, when `compute_review_length()` is passed to `Dataset.map()`, it will be applied to all the rows in the dataset to create a new `review_length` column: ``` drug_dataset = drug_dataset.map(compute_review_length) drug_dataset["train"][0]``` ``` {'patient_id': 206461, 'drugName': 'Valsartan', 'condition': 'left ventricular dysfunction', 'review': '"It has no side effect, I take it in combination of Bystolic 5 Mg and Fish Oil"', 'rating': 9.0, 'date': 'May 20, 2012', 'usefulCount': 27, 'review_length': 17}``` As expected, we can see a `review_length` column has been added to our training set. We can sort this new column with `Dataset.sort()` to see what the extreme values look like: ``` drug_dataset["train"].sort("review_length")[:3]``` ``` {'patient_id': [103488, 23627, 20558], 'drugName': ['Loestrin 21 1 / 20', 'Chlorzoxazone', 'Nucynta'], 'condition': ['birth control', 'muscle spasm', 'pain'], 'review': ['"Excellent."', '"useless"', '"ok"'], 'rating': [10.0, 1.0, 6.0], 'date': ['November 4, 2008', 'March 24, 2017', 'August 20, 2016'], 'usefulCount': [5, 2, 10], 'review_length': [1, 1, 1]}``` As we suspected, some reviews contain just a single word, which, although it may be okay for sentiment analysis, would not be informative if we want to predict the condition. 🙋 An alternative way to add new columns to a dataset is with the `Dataset.add_column()` function. This allows you to provide the column as a Python list or NumPy array and can be handy in situations where `Dataset.map()` is not well suited for your analysis. Let’s use the `Dataset.filter()` function to remove reviews that contain fewer than 30 words. Similarly to what we did with the `condition` column, we can filter out the very short reviews by requiring that the reviews have a length above this threshold: ``` drug_dataset = drug_dataset.filter(lambda x: x["review_length"] > 30) print(drug_dataset.num_rows)``` ``` {'train': 138514, 'test': 46108}``` As you can see, this has removed around 15% of the reviews from our original training and test sets. ✏️ **Try it out!** Use the `Dataset.sort()` function to inspect the reviews with the largest numbers of words. See the [documentation](https://huggingface.co/docs/datasets/package_reference/main_classes.html#datasets.Dataset.sort) to see which argument you need to use sort the reviews by length in descending order. The last thing we need to deal with is the presence of HTML character codes in our reviews. We can use Python’s `html` module to unescape these characters, like so: ``` import html text = "I&#039;m a transformer called BERT" html.unescape(text)``` ``` "I'm a transformer called BERT"``` We’ll use `Dataset.map()` to unescape all the HTML characters in our corpus: ``` drug_dataset = drug_dataset.map(lambda x: {"review": html.unescape(x["review"])})``` As you can see, the `Dataset.map()` method is quite useful for processing data — and we haven’t even scratched the surface of everything it can do! ## [](#the-map-methods-superpowers)The `map()` method's superpowers The `Dataset.map()` method takes a `batched` argument that, if set to `True`, causes it to send a batch of examples to the map function at once (the batch size is configurable but defaults to 1,000). For instance, the previous map function that unescaped all the HTML took a bit of time to run (you can read the time taken from the progress bars). We can speed this up by processing several elements at the same time using a list comprehension. When you specify `batched=True` the function receives a dictionary with the fields of the dataset, but each value is now a _list of values_, and not just a single value. The return value of `Dataset.map()` should be the same: a dictionary with the fields we want to update or add to our dataset, and a list of values. For example, here is another way to unescape all HTML characters, but using `batched=True`: ``` new_drug_dataset = drug_dataset.map( lambda x: {"review": [html.unescape(o) for o in x["review"]]}, batched=True )``` If you’re running this code in a notebook, you’ll see that this command executes way faster than the previous one. And it’s not because our reviews have already been HTML-unescaped — if you re-execute the instruction from the previous section (without `batched=True`), it will take the same amount of time as before. This is because list comprehensions are usually faster than executing the same code in a `for` loop, and we also gain some performance by accessing lots of elements at the same time instead of one by one. Using `Dataset.map()` with `batched=True` will be essential to unlock the speed of the “fast” tokenizers that we’ll encounter in [Chapter 6](/course/chapter6), which can quickly tokenize big lists of texts. For instance, to tokenize all the drug reviews with a fast tokenizer, we could use a function like this: ``` from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") def tokenize_function(examples): return tokenizer(examples["review"], truncation=True)``` As you saw in [Chapter 3](/course/chapter3), we can pass one or several examples to the tokenizer, so we can use this function with or without `batched=True`. Let’s take this opportunity to compare the performance of the different options. In a notebook, you can time a one-line instruction by adding `%time` before the line of code you wish to measure: ``` %time tokenized_dataset = drug_dataset.map(tokenize_function, batched=True)``` You can also time a whole cell by putting `%%time` at the beginning of the cell. On the hardware we executed this on, it showed 10.8s for this instruction (it’s the number written after “Wall time”). ✏️ **Try it out!** Execute the same instruction with and without `batched=True`, then try it with a slow tokenizer (add `use_fast=False` in the `AutoTokenizer.from_pretrained()` method) so you can see what numbers you get on your hardware. Here are the results we obtained with and without batching, with a fast and a slow tokenizer: | Options | Fast tokenizer | Slow tokenizer | | --- | --- | --- | | `batched=True` | 10.8s | 4min41s | | `batched=False` | 59.2s | 5min3s | This means that using a fast tokenizer with the `batched=True` option is 30 times faster than its slow counterpart with no batching — this is truly amazing! That’s the main reason why fast tokenizers are the default when using `AutoTokenizer` (and why they are called “fast”). They’re able to achieve such a speedup because behind the scenes the tokenization code is executed in Rust, which is a language that makes it easy to parallelize code execution. Parallelization is also the reason for the nearly 6x speedup the fast tokenizer achieves with batching: you can’t parallelize a single tokenization operation, but when you want to tokenize lots of texts at the same time you can just split the execution across several processes, each responsible for its own texts. `Dataset.map()` also has some parallelization capabilities of its own. Since they are not backed by Rust, they won’t let a slow tokenizer catch up with a fast one, but they can still be helpful (especially if you’re using a tokenizer that doesn’t have a fast version). To enable multiprocessing, use the `num_proc` argument and specify the number of processes to use in your call to `Dataset.map()`: ``` slow_tokenizer = AutoTokenizer.from_pretrained("bert-base-cased", use_fast=False) def slow_tokenize_function(examples): return slow_tokenizer(examples["review"], truncation=True) tokenized_dataset = drug_dataset.map(slow_tokenize_function, batched=True, num_proc=8)``` You can experiment a little with timing to determine the optimal number of processes to use; in our case 8 seemed to produce the best speed gain. Here are the numbers we got with and without multiprocessing: | Options | Fast tokenizer | Slow tokenizer | | --- | --- | --- | | `batched=True` | 10.8s | 4min41s | | `batched=False` | 59.2s | 5min3s | | `batched=True`, `num_proc=8` | 6.52s | 41.3s | | `batched=False`, `num_proc=8` | 9.49s | 45.2s | Those are much more reasonable results for the slow tokenizer, but the performance of the fast tokenizer was also substantially improved. Note, however, that won’t always be the case — for values of `num_proc` other than 8, our tests showed that it was faster to use `batched=True` without that option. In general, we don’t recommend using Python multiprocessing for fast tokenizers with `batched=True`. Using `num_proc` to speed up your processing is usually a great idea, as long as the function you are using is not already doing some kind of multiprocessing of its own. All of this functionality condensed into a single method is already pretty amazing, but there’s more! With `Dataset.map()` and `batched=True` you can change the number of elements in your dataset. This is super useful in many situations where you want to create several training features from one example, and we will need to do this as part of the preprocessing for several of the NLP tasks we’ll undertake in [Chapter 7](/course/chapter7). 💡 In machine learning, an _example_ is usually defined as the set of _features_ that we feed to the model. In some contexts, these features will be the set of columns in a `Dataset`, but in others (like here and for question answering), multiple features can be extracted from a single example and belong to a single column. Let’s have a look at how it works! Here we will tokenize our examples and truncate them to a maximum length of 128, but we will ask the tokenizer to return _all_ the chunks of the texts instead of just the first one. This can be done with `return_overflowing_tokens=True`: ``` def tokenize_and_split(examples): return tokenizer( examples["review"], truncation=True, max_length=128, return_overflowing_tokens=True, )``` Let’s test this on one example before using `Dataset.map()` on the whole dataset: ``` result = tokenize_and_split(drug_dataset["train"][0]) [len(inp) for inp in result["input_ids"]]``` So, our first example in the training set became two features because it was tokenized to more than the maximum number of tokens we specified: the first one of length 128 and the second one of length 49. Now let’s do this for all elements of the dataset! ``` tokenized_dataset = drug_dataset.map(tokenize_and_split, batched=True)``` ``` ArrowInvalid: Column 1 named condition expected length 1463 but got length 1000``` Oh no! That didn’t work! Why not? Looking at the error message will give us a clue: there is a mismatch in the lengths of one of the columns, one being of length 1,463 and the other of length 1,000. If you’ve looked at the `Dataset.map()` [documentation](https://huggingface.co/docs/datasets/package_reference/main_classes.html#datasets.Dataset.map), you may recall that it’s the number of samples passed to the function that we are mapping; here those 1,000 examples gave 1,463 new features, resulting in a shape error. The problem is that we’re trying to mix two different datasets of different sizes: the `drug_dataset` columns will have a certain number of examples (the 1,000 in our error), but the `tokenized_dataset` we are building will have more (the 1,463 in the error message; it is more than 1,000 because we are tokenizing long reviews into more than one example by using `return_overflowing_tokens=True`). That doesn’t work for a `Dataset`, so we need to either remove the columns from the old dataset or make them the same size as they are in the new dataset. We can do the former with the `remove_columns` argument: ``` tokenized_dataset = drug_dataset.map( tokenize_and_split, batched=True, remove_columns=drug_dataset["train"].column_names )``` Now this works without error. We can check that our new dataset has many more elements than the original dataset by comparing the lengths: ``` len(tokenized_dataset["train"]), len(drug_dataset["train"])``` We mentioned that we can also deal with the mismatched length problem by making the old columns the same size as the new ones. To do this, we will need the `overflow_to_sample_mapping` field the tokenizer returns when we set `return_overflowing_tokens=True`. It gives us a mapping from a new feature index to the index of the sample it originated from. Using this, we can associate each key present in our original dataset with a list of values of the right size by repeating the values of each example as many times as it generates new features: ``` def tokenize_and_split(examples): result = tokenizer( examples["review"], truncation=True, max_length=128, return_overflowing_tokens=True, ) sample_map = result.pop("overflow_to_sample_mapping") for key, values in examples.items(): result[key] = [values[i] for i in sample_map] return result``` We can see it works with `Dataset.map()` without us needing to remove the old columns: ``` tokenized_dataset = drug_dataset.map(tokenize_and_split, batched=True) tokenized_dataset``` ``` DatasetDict({ train: Dataset({ features: ['attention_mask', 'condition', 'date', 'drugName', 'input_ids', 'patient_id', 'rating', 'review', 'review_length', 'token_type_ids', 'usefulCount'], num_rows: 206772 }) test: Dataset({ features: ['attention_mask', 'condition', 'date', 'drugName', 'input_ids', 'patient_id', 'rating', 'review', 'review_length', 'token_type_ids', 'usefulCount'], num_rows: 68876 }) })``` We get the same number of training features as before, but here we’ve kept all the old fields. If you need them for some post-processing after applying your model, you might want to use this approach. You’ve now seen how 🤗 Datasets can be used to preprocess a dataset in various ways. Although the processing functions of 🤗 Datasets will cover most of your model training needs, there may be times when you’ll need to switch to Pandas to access more powerful features, like `DataFrame.groupby()` or high-level APIs for visualization. Fortunately, 🤗 Datasets is designed to be interoperable with libraries such as Pandas, NumPy, PyTorch, TensorFlow, and JAX. Let’s take a look at how this works. ## [](#from-datasets-to-dataframes-and-back)From `Dataset`s to `DataFrame`s and back To enable the conversion between various third-party libraries, 🤗 Datasets provides a `Dataset.set_format()` function. This function only changes the _output format_ of the dataset, so you can easily switch to another format without affecting the underlying _data format_, which is Apache Arrow. The formatting is done in place. To demonstrate, let’s convert our dataset to Pandas: ``` drug_dataset.set_format("pandas")``` Now when we access elements of the dataset we get a `pandas.DataFrame` instead of a dictionary: ``` drug_dataset["train"][:3]``` | | patient\_id | drugName | condition | review | rating | date | usefulCount | review\_length | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | 0 | 95260 | Guanfacine | adhd | "My son is halfway through his fourth week of Intuniv..." | 8.0 | April 27, 2010 | 192 | 141 | | 1 | 92703 | Lybrel | birth control | "I used to take another oral contraceptive, which had 21 pill cycle, and was very happy- very light periods, max 5 days, no other side effects..." | 5.0 | December 14, 2009 | 17 | 134 | | 2 | 138000 | Ortho Evra | birth control | "This is my first time using any form of birth control..." | 8.0 | November 3, 2015 | 10 | 89 | Let’s create a `pandas.DataFrame` for the whole training set by selecting all the elements of `drug_dataset["train"]`: ``` train_df = drug_dataset["train"][:]``` 🚨 Under the hood, `Dataset.set_format()` changes the return format for the dataset’s `__getitem__()` dunder method. This means that when we want to create a new object like `train_df` from a `Dataset` in the `"pandas"` format, we need to slice the whole dataset to obtain a `pandas.DataFrame`. You can verify for yourself that the type of `drug_dataset["train"]` is `Dataset`, irrespective of the output format. From here we can use all the Pandas functionality that we want. For example, we can do fancy chaining to compute the class distribution among the `condition` entries: ``` frequencies = ( train_df["condition"] .value_counts() .to_frame() .reset_index() .rename(columns={"index": "condition", "condition": "frequency"}) ) frequencies.head()``` | | condition | frequency | | --- | --- | --- | | 0 | birth control | 27655 | | 1 | depression | 8023 | | 2 | acne | 5209 | | 3 | anxiety | 4991 | | 4 | pain | 4744 | And once we’re done with our Pandas analysis, we can always create a new `Dataset` object by using the `Dataset.from_pandas()` function as follows: ``` from datasets import Dataset freq_dataset = Dataset.from_pandas(frequencies) freq_dataset``` ``` Dataset({ features: ['condition', 'frequency'], num_rows: 819 })``` ✏️ **Try it out!** Compute the average rating per drug and store the result in a new `Dataset`. This wraps up our tour of the various preprocessing techniques available in 🤗 Datasets. To round out the section, let’s create a validation set to prepare the dataset for training a classifier on. Before doing so, we’ll reset the output format of `drug_dataset` from `"pandas"` to `"arrow"`: ``` drug_dataset.reset_format()``` ## [](#creating-a-validation-set)Creating a validation set Although we have a test set we could use for evaluation, it’s a good practice to leave the test set untouched and create a separate validation set during development. Once you are happy with the performance of your models on the validation set, you can do a final sanity check on the test set. This process helps mitigate the risk that you’ll overfit to the test set and deploy a model that fails on real-world data. 🤗 Datasets provides a `Dataset.train_test_split()` function that is based on the famous functionality from `scikit-learn`. Let’s use it to split our training set into `train` and `validation` splits (we set the `seed` argument for reproducibility): ``` drug_dataset_clean = drug_dataset["train"].train_test_split(train_size=0.8, seed=42) drug_dataset_clean["validation"] = drug_dataset_clean.pop("test") drug_dataset_clean["test"] = drug_dataset["test"] drug_dataset_clean``` ``` DatasetDict({ train: Dataset({ features: ['patient_id', 'drugName', 'condition', 'review', 'rating', 'date', 'usefulCount', 'review_length', 'review_clean'], num_rows: 110811 }) validation: Dataset({ features: ['patient_id', 'drugName', 'condition', 'review', 'rating', 'date', 'usefulCount', 'review_length', 'review_clean'], num_rows: 27703 }) test: Dataset({ features: ['patient_id', 'drugName', 'condition', 'review', 'rating', 'date', 'usefulCount', 'review_length', 'review_clean'], num_rows: 46108 }) })``` Great, we’ve now prepared a dataset that’s ready for training some models on! In [section 5](/course/chapter5/5) we’ll show you how to upload datasets to the Hugging Face Hub, but for now let’s cap off our analysis by looking at a few ways you can save datasets on your local machine. ## [](#saving-a-dataset)Saving a dataset Although 🤗 Datasets will cache every downloaded dataset and the operations performed on it, there are times when you’ll want to save a dataset to disk (e.g., in case the cache gets deleted). As shown in the table below, 🤗 Datasets provides three main functions to save your dataset in different formats: | Data format | Function | | --- | --- | | Arrow | `Dataset.save_to_disk()` | | CSV | `Dataset.to_csv()` | | JSON | `Dataset.to_json()` | For example, let’s save our cleaned dataset in the Arrow format: ``` drug_dataset_clean.save_to_disk("drug-reviews")``` This will create a directory with the following structure: ``` drug-reviews/ ├── dataset_dict.json ├── test │ ├── dataset.arrow │ ├── dataset_info.json │ └── state.json ├── train │ ├── dataset.arrow │ ├── dataset_info.json │ ├── indices.arrow │ └── state.json └── validation ├── dataset.arrow ├── dataset_info.json ├── indices.arrow └── state.json``` where we can see that each split is associated with its own _dataset.arrow_ table, and some metadata in _dataset\_info.json_ and _state.json_. You can think of the Arrow format as a fancy table of columns and rows that is optimized for building high-performance applications that process and transport large datasets. Once the dataset is saved, we can load it by using the `load_from_disk()` function as follows: ``` from datasets import load_from_disk drug_dataset_reloaded = load_from_disk("drug-reviews") drug_dataset_reloaded``` ``` DatasetDict({ train: Dataset({ features: ['patient_id', 'drugName', 'condition', 'review', 'rating', 'date', 'usefulCount', 'review_length'], num_rows: 110811 }) validation: Dataset({ features: ['patient_id', 'drugName', 'condition', 'review', 'rating', 'date', 'usefulCount', 'review_length'], num_rows: 27703 }) test: Dataset({ features: ['patient_id', 'drugName', 'condition', 'review', 'rating', 'date', 'usefulCount', 'review_length'], num_rows: 46108 }) })``` For the CSV and JSON formats, we have to store each split as a separate file. One way to do this is by iterating over the keys and values in the `DatasetDict` object: ``` for split, dataset in drug_dataset_clean.items(): dataset.to_json(f"drug-reviews-{split}.jsonl")``` This saves each split in [JSON Lines format](https://jsonlines.org/), where each row in the dataset is stored as a single line of JSON. Here’s what the first example looks like: ``` !head -n 1 drug-reviews-train.jsonl``` ``` {"patient_id":141780,"drugName":"Escitalopram","condition":"depression","review":"\"I seemed to experience the regular side effects of LEXAPRO, insomnia, low sex drive, sleepiness during the day. I am taking it at night because my doctor said if it made me tired to take it at night. I assumed it would and started out taking it at night. Strange dreams, some pleasant. I was diagnosed with fibromyalgia. Seems to be helping with the pain. Have had anxiety and depression in my family, and have tried quite a few other medications that haven't worked. Only have been on it for two weeks but feel more positive in my mind, want to accomplish more in my life. Hopefully the side effects will dwindle away, worth it to stick with it from hearing others responses. Great medication.\"","rating":9.0,"date":"May 29, 2011","usefulCount":10,"review_length":125}``` We can then use the techniques from [section 2](/course/chapter5/2) to load the JSON files as follows: ``` data_files = { "train": "drug-reviews-train.jsonl", "validation": "drug-reviews-validation.jsonl", "test": "drug-reviews-test.jsonl", } drug_dataset_reloaded = load_dataset("json", data_files=data_files)``` And that’s it for our excursion into data wrangling with 🤗 Datasets! Now that we have a cleaned dataset for training a model on, here are a few ideas that you could try out: 1. Use the techniques from [Chapter 3](/course/chapter3) to train a classifier that can predict the patient condition based on the drug review. 2. Use the `summarization` pipeline from [Chapter 1](/course/chapter1) to generate summaries of the reviews. Next, we’ll take a look at how 🤗 Datasets can enable you to work with huge datasets without blowing up your laptop!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Time to slice and dice - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter5/3"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Time to slice and dice - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;time-to-slice-and-dice&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;slicing-and-dicing-our-data&quot;,&quot;title&quot;:&quot;Slicing and dicing our data&quot;},{&quot;local&quot;:&quot;creating-new-columns&quot;,&quot;title&quot;:&quot;Creating new columns&quot;},{&quot;local&quot;:&quot;the-map-methods-superpowers&quot;,&quot;title&quot;:&quot;The `map()` method's superpowers&quot;},{&quot;local&quot;:&quot;from-datasets-to-dataframes-and-back&quot;,&quot;title&quot;:&quot;From `Dataset`s to `DataFrame`s and back&quot;},{&quot;local&quot;:&quot;creating-a-validation-set&quot;,&quot;title&quot;:&quot;Creating a validation set&quot;},{&quot;local&quot;:&quot;saving-a-dataset&quot;,&quot;title&quot;:&quot;Saving a dataset&quot;}],&quot;title&quot;:&quot;Time to slice and dice&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter5/3&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Time to slice and dice&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Time to slice and dice</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/2?fw=pt">What if my dataset isn't on the Hub? </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter5/3?fw=pt">Time to slice and dice </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/4?fw=pt">Big data? 🤗 Datasets to the rescue! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/5?fw=pt">Creating your own dataset </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/6?fw=pt">Semantic search with FAISS </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/7?fw=pt">🤗 Datasets, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="time-to-slice-and-dice" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#time-to-slice-and-dice"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Time to slice and dice</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-5-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter5/section3.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter5/section3.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>Most of the time, the data you work with won’t be perfectly prepared for training models. In this section we’ll explore the various features that 🤗 Datasets provides to clean up your datasets.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/tqfSFcPMgOI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <h2 class="relative group"><a id="slicing-and-dicing-our-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#slicing-and-dicing-our-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Slicing and dicing our data</span></h2> <p>Similar to Pandas, 🤗 Datasets provides several functions to manipulate the contents of <code>Dataset</code> and <code>DatasetDict</code> objects. We already encountered the <code>Dataset.map()</code> method in <a href="/course/chapter3">Chapter 3</a>, and in this section we’ll explore some of the other functions at our disposal.</p> <p>For this example we’ll use the <a href="https://archive.ics.uci.edu/ml/datasets/Drug+Review+Dataset+%28Drugs.com%29" rel="nofollow">Drug Review Dataset</a> that’s hosted on the <a href="https://archive.ics.uci.edu/ml/index.php" rel="nofollow">UC Irvine Machine Learning Repository</a>, which contains patient reviews on various drugs, along with the condition being treated and a 10-star rating of the patient’s satisfaction.</p> <p>First we need to download and extract the data, which can be done with the <code>wget</code> and <code>unzip</code> commands:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!wget <span class="hljs-string">"https://archive.ics.uci.edu/ml/machine-learning-databases/00462/drugsCom_raw.zip"</span> !unzip drugsCom_raw.<span class="hljs-built_in">zip</span></pre></div> <p>Since TSV is just a variant of CSV that uses tabs instead of commas as the separator, we can load these files by using the <code>csv</code> loading script and specifying the <code>delimiter</code> argument in the <code>load_dataset()</code> function as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset data_files = {<span class="hljs-string">"train"</span>: <span class="hljs-string">"drugsComTrain_raw.tsv"</span>, <span class="hljs-string">"test"</span>: <span class="hljs-string">"drugsComTest_raw.tsv"</span>} <span class="hljs-comment"># \t is the tab character in Python</span> drug_dataset = load_dataset(<span class="hljs-string">"csv"</span>, data_files=data_files, delimiter=<span class="hljs-string">"\t"</span>)</pre></div> <p>A good practice when doing any sort of data analysis is to grab a small random sample to get a quick feel for the type of data you’re working with. In 🤗 Datasets, we can create a random sample by chaining the <code>Dataset.shuffle()</code> and <code>Dataset.select()</code> functions together:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_sample = drug_dataset[<span class="hljs-string">"train"</span>].shuffle(seed=<span class="hljs-number">42</span>).select(<span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>)) <span class="hljs-comment"># Peek at the first few examples</span> drug_sample[:<span class="hljs-number">3</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'Unnamed: 0'</span>: [<span class="hljs-number">87571</span>, <span class="hljs-number">178045</span>, <span class="hljs-number">80482</span>], <span class="hljs-string">'drugName'</span>: [<span class="hljs-string">'Naproxen'</span>, <span class="hljs-string">'Duloxetine'</span>, <span class="hljs-string">'Mobic'</span>], <span class="hljs-string">'condition'</span>: [<span class="hljs-string">'Gout, Acute'</span>, <span class="hljs-string">'ibromyalgia'</span>, <span class="hljs-string">'Inflammatory Conditions'</span>], <span class="hljs-string">'review'</span>: [<span class="hljs-string">'"like the previous person mention, I&amp;#039;m a strong believer of aleve, it works faster for my gout than the prescription meds I take. No more going to the doctor for refills.....Aleve works!"'</span>, <span class="hljs-string">'"I have taken Cymbalta for about a year and a half for fibromyalgia pain. It is great\r\nas a pain reducer and an anti-depressant, however, the side effects outweighed \r\nany benefit I got from it. I had trouble with restlessness, being tired constantly,\r\ndizziness, dry mouth, numbness and tingling in my feet, and horrible sweating. I am\r\nbeing weaned off of it now. Went from 60 mg to 30mg and now to 15 mg. I will be\r\noff completely in about a week. The fibro pain is coming back, but I would rather deal with it than the side effects."'</span>, <span class="hljs-string">'"I have been taking Mobic for over a year with no side effects other than an elevated blood pressure. I had severe knee and ankle pain which completely went away after taking Mobic. I attempted to stop the medication however pain returned after a few days."'</span>], <span class="hljs-string">'rating'</span>: [<span class="hljs-number">9.0</span>, <span class="hljs-number">3.0</span>, <span class="hljs-number">10.0</span>], <span class="hljs-string">'date'</span>: [<span class="hljs-string">'September 2, 2015'</span>, <span class="hljs-string">'November 7, 2011'</span>, <span class="hljs-string">'June 5, 2013'</span>], <span class="hljs-string">'usefulCount'</span>: [<span class="hljs-number">36</span>, <span class="hljs-number">13</span>, <span class="hljs-number">128</span>]}</pre></div> <p>Note that we’ve fixed the seed in <code>Dataset.shuffle()</code> for reproducibility purposes. <code>Dataset.select()</code> expects an iterable of indices, so we’ve passed <code>range(1000)</code> to grab the first 1,000 examples from the shuffled dataset. From this sample we can already see a few quirks in our dataset:</p> <ul><li>The <code>Unnamed: 0</code> column looks suspiciously like an anonymized ID for each patient.</li> <li>The <code>condition</code> column includes a mix of uppercase and lowercase labels.</li> <li>The reviews are of varying length and contain a mix of Python line separators (<code>\r\n</code>) as well as HTML character codes like <code>&amp;\#039;</code>.</li></ul> <p>Let’s see how we can use 🤗 Datasets to deal with each of these issues. To test the patient ID hypothesis for the <code>Unnamed: 0</code> column, we can use the <code>Dataset.unique()</code> function to verify that the number of IDs matches the number of rows in each split:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> split <span class="hljs-keyword">in</span> drug_dataset.keys(): <span class="hljs-keyword">assert</span> <span class="hljs-built_in">len</span>(drug_dataset[split]) == <span class="hljs-built_in">len</span>(drug_dataset[split].unique(<span class="hljs-string">"Unnamed: 0"</span>))</pre></div> <p>This seems to confirm our hypothesis, so let’s clean up the dataset a bit by renaming the <code>Unnamed: 0</code> column to something a bit more interpretable. We can use the <code>DatasetDict.rename_column()</code> function to rename the column across both splits in one go:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_dataset = drug_dataset.rename_column( original_column_name=<span class="hljs-string">"Unnamed: 0"</span>, new_column_name=<span class="hljs-string">"patient_id"</span> ) drug_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'patient_id'</span>, <span class="hljs-string">'drugName'</span>, <span class="hljs-string">'condition'</span>, <span class="hljs-string">'review'</span>, <span class="hljs-string">'rating'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'usefulCount'</span>], num_rows: <span class="hljs-number">161297</span> }) test: Dataset({ features: [<span class="hljs-string">'patient_id'</span>, <span class="hljs-string">'drugName'</span>, <span class="hljs-string">'condition'</span>, <span class="hljs-string">'review'</span>, <span class="hljs-string">'rating'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'usefulCount'</span>], num_rows: <span class="hljs-number">53766</span> }) })</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Use the <code>Dataset.unique()</code> function to find the number of unique drugs and conditions in the training and test sets.</p></div> <p>Next, let’s normalize all the <code>condition</code> labels using <code>Dataset.map()</code>. As we did with tokenization in <a href="/course/chapter3">Chapter 3</a>, we can define a simple function that can be applied across all the rows of each split in <code>drug_dataset</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">lowercase_condition</span>(<span class="hljs-params">example</span>): <span class="hljs-keyword">return</span> {<span class="hljs-string">"condition"</span>: example[<span class="hljs-string">"condition"</span>].lower()} drug_dataset.<span class="hljs-built_in">map</span>(lowercase_condition)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>AttributeError: <span class="hljs-string">'NoneType'</span> <span class="hljs-built_in">object</span> has no attribute <span class="hljs-string">'lower'</span></pre></div> <p>Oh no, we’ve run into a problem with our map function! From the error we can infer that some of the entries in the <code>condition</code> column are <code>None</code>, which cannot be lowercased as they’re not strings. Let’s drop these rows using <code>Dataset.filter()</code>, which works in a similar way to <code>Dataset.map()</code> and expects a function that receives a single example of the dataset. Instead of writing an explicit function like:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">filter_nones</span>(<span class="hljs-params">x</span>): <span class="hljs-keyword">return</span> x[<span class="hljs-string">"condition"</span>] <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span></pre></div> <p>and then running <code>drug_dataset.filter(filter_nones)</code>, we can do this in one line using a <em>lambda function</em>. In Python, lambda functions are small functions that you can define without explicitly naming them. They take the general form:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>lambda <span class="hljs-tag">&lt;<span class="hljs-name">arguments</span>&gt;</span> : <span class="hljs-tag">&lt;<span class="hljs-name">expression</span>&gt;</span></pre></div> <p>where <code>lambda</code> is one of Python’s special <a href="https://docs.python.org/3/reference/lexical_analysis.html#keywords" rel="nofollow">keywords</a>, <code>&lt;arguments&gt;</code> is a list/set of comma-separated values that define the inputs to the function, and <code>&lt;expression&gt;</code> represents the operations you wish to execute. For example, we can define a simple lambda function that squares a number as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>lambda <span class="hljs-keyword">x</span> : <span class="hljs-keyword">x</span> * <span class="hljs-keyword">x</span></pre></div> <p>To apply this function to an input, we need to wrap it and the input in parentheses:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-keyword">lambda</span> x: x * x)(<span class="hljs-number">3</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">9</span></pre></div> <p>Similarly, we can define lambda functions with multiple arguments by separating them with commas. For example, we can compute the area of a triangle as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-keyword">lambda</span> base, height: <span class="hljs-number">0.5</span> * base * height)(<span class="hljs-number">4</span>, <span class="hljs-number">8</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">16.0</span></pre></div> <p>Lambda functions are handy when you want to define small, single-use functions (for more information about them, we recommend reading the excellent <a href="https://realpython.com/python-lambda/" rel="nofollow">Real Python tutorial</a> by Andre Burgaud). In the 🤗 Datasets context, we can use lambda functions to define simple map and filter operations, so let’s use this trick to eliminate the <code>None</code> entries in our dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_dataset = drug_dataset.<span class="hljs-built_in">filter</span>(<span class="hljs-keyword">lambda</span> x: x[<span class="hljs-string">"condition"</span>] <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>)</pre></div> <p>With the <code>None</code> entries removed, we can normalize our <code>condition</code> column:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_dataset = drug_dataset.<span class="hljs-built_in">map</span>(lowercase_condition) <span class="hljs-comment"># Check that lowercasing worked</span> drug_dataset[<span class="hljs-string">"train"</span>][<span class="hljs-string">"condition"</span>][:<span class="hljs-number">3</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'left ventricular dysfunction'</span>, <span class="hljs-string">'adhd'</span>, <span class="hljs-string">'birth control'</span>]</pre></div> <p>It works! Now that we’ve cleaned up the labels, let’s take a look at cleaning up the reviews themselves.</p> <h2 class="relative group"><a id="creating-new-columns" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#creating-new-columns"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Creating new columns</span></h2> <p>Whenever you’re dealing with customer reviews, a good practice is to check the number of words in each review. A review might be just a single word like “Great!” or a full-blown essay with thousands of words, and depending on the use case you’ll need to handle these extremes differently. To compute the number of words in each review, we’ll use a rough heuristic based on splitting each text by whitespace.</p> <p>Let’s define a simple function that counts the number of words in each review:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_review_length</span>(<span class="hljs-params">example</span>): <span class="hljs-keyword">return</span> {<span class="hljs-string">"review_length"</span>: <span class="hljs-built_in">len</span>(example[<span class="hljs-string">"review"</span>].split())}</pre></div> <p>Unlike our <code>lowercase_condition()</code> function, <code>compute_review_length()</code> returns a dictionary whose key does not correspond to one of the column names in the dataset. In this case, when <code>compute_review_length()</code> is passed to <code>Dataset.map()</code>, it will be applied to all the rows in the dataset to create a new <code>review_length</code> column:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_dataset = drug_dataset.<span class="hljs-built_in">map</span>(compute_review_length) <span class="hljs-comment"># Inspect the first training example</span> drug_dataset[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'patient_id'</span>: <span class="hljs-number">206461</span>, <span class="hljs-string">'drugName'</span>: <span class="hljs-string">'Valsartan'</span>, <span class="hljs-string">'condition'</span>: <span class="hljs-string">'left ventricular dysfunction'</span>, <span class="hljs-string">'review'</span>: <span class="hljs-string">'"It has no side effect, I take it in combination of Bystolic 5 Mg and Fish Oil"'</span>, <span class="hljs-string">'rating'</span>: <span class="hljs-number">9.0</span>, <span class="hljs-string">'date'</span>: <span class="hljs-string">'May 20, 2012'</span>, <span class="hljs-string">'usefulCount'</span>: <span class="hljs-number">27</span>, <span class="hljs-string">'review_length'</span>: <span class="hljs-number">17</span>}</pre></div> <p>As expected, we can see a <code>review_length</code> column has been added to our training set. We can sort this new column with <code>Dataset.sort()</code> to see what the extreme values look like:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_dataset[<span class="hljs-string">"train"</span>].sort(<span class="hljs-string">"review_length"</span>)[:<span class="hljs-number">3</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'patient_id'</span>: [<span class="hljs-number">103488</span>, <span class="hljs-number">23627</span>, <span class="hljs-number">20558</span>], <span class="hljs-string">'drugName'</span>: [<span class="hljs-string">'Loestrin 21 1 / 20'</span>, <span class="hljs-string">'Chlorzoxazone'</span>, <span class="hljs-string">'Nucynta'</span>], <span class="hljs-string">'condition'</span>: [<span class="hljs-string">'birth control'</span>, <span class="hljs-string">'muscle spasm'</span>, <span class="hljs-string">'pain'</span>], <span class="hljs-string">'review'</span>: [<span class="hljs-string">'"Excellent."'</span>, <span class="hljs-string">'"useless"'</span>, <span class="hljs-string">'"ok"'</span>], <span class="hljs-string">'rating'</span>: [<span class="hljs-number">10.0</span>, <span class="hljs-number">1.0</span>, <span class="hljs-number">6.0</span>], <span class="hljs-string">'date'</span>: [<span class="hljs-string">'November 4, 2008'</span>, <span class="hljs-string">'March 24, 2017'</span>, <span class="hljs-string">'August 20, 2016'</span>], <span class="hljs-string">'usefulCount'</span>: [<span class="hljs-number">5</span>, <span class="hljs-number">2</span>, <span class="hljs-number">10</span>], <span class="hljs-string">'review_length'</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]}</pre></div> <p>As we suspected, some reviews contain just a single word, which, although it may be okay for sentiment analysis, would not be informative if we want to predict the condition.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>🙋 An alternative way to add new columns to a dataset is with the <code>Dataset.add_column()</code> function. This allows you to provide the column as a Python list or NumPy array and can be handy in situations where <code>Dataset.map()</code> is not well suited for your analysis.</p></div> <p>Let’s use the <code>Dataset.filter()</code> function to remove reviews that contain fewer than 30 words. Similarly to what we did with the <code>condition</code> column, we can filter out the very short reviews by requiring that the reviews have a length above this threshold:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_dataset = drug_dataset.<span class="hljs-built_in">filter</span>(<span class="hljs-keyword">lambda</span> x: x[<span class="hljs-string">"review_length"</span>] &gt; <span class="hljs-number">30</span>) <span class="hljs-built_in">print</span>(drug_dataset.num_rows)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'train'</span>: <span class="hljs-number">138514</span>, <span class="hljs-string">'test'</span>: <span class="hljs-number">46108</span>}</pre></div> <p>As you can see, this has removed around 15% of the reviews from our original training and test sets.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Use the <code>Dataset.sort()</code> function to inspect the reviews with the largest numbers of words. See the <a href="https://huggingface.co/docs/datasets/package_reference/main_classes.html#datasets.Dataset.sort" rel="nofollow">documentation</a> to see which argument you need to use sort the reviews by length in descending order.</p></div> <p>The last thing we need to deal with is the presence of HTML character codes in our reviews. We can use Python’s <code>html</code> module to unescape these characters, like so:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> html text = <span class="hljs-string">"I&amp;#039;m a transformer called BERT"</span> html.unescape(text)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">"I'm a transformer called BERT"</span></pre></div> <p>We’ll use <code>Dataset.map()</code> to unescape all the HTML characters in our corpus:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_dataset = drug_dataset.<span class="hljs-built_in">map</span>(<span class="hljs-keyword">lambda</span> x: {<span class="hljs-string">"review"</span>: html.unescape(x[<span class="hljs-string">"review"</span>])})</pre></div> <p>As you can see, the <code>Dataset.map()</code> method is quite useful for processing data — and we haven’t even scratched the surface of everything it can do!</p> <h2 class="relative group"><a id="the-map-methods-superpowers" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-map-methods-superpowers"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The <code>map()</code> method's superpowers</span></h2> <p>The <code>Dataset.map()</code> method takes a <code>batched</code> argument that, if set to <code>True</code>, causes it to send a batch of examples to the map function at once (the batch size is configurable but defaults to 1,000). For instance, the previous map function that unescaped all the HTML took a bit of time to run (you can read the time taken from the progress bars). We can speed this up by processing several elements at the same time using a list comprehension.</p> <p>When you specify <code>batched=True</code> the function receives a dictionary with the fields of the dataset, but each value is now a <em>list of values</em>, and not just a single value. The return value of <code>Dataset.map()</code> should be the same: a dictionary with the fields we want to update or add to our dataset, and a list of values. For example, here is another way to unescape all HTML characters, but using <code>batched=True</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>new_drug_dataset = drug_dataset.<span class="hljs-built_in">map</span>( <span class="hljs-keyword">lambda</span> x: {<span class="hljs-string">"review"</span>: [html.unescape(o) <span class="hljs-keyword">for</span> o <span class="hljs-keyword">in</span> x[<span class="hljs-string">"review"</span>]]}, batched=<span class="hljs-literal">True</span> )</pre></div> <p>If you’re running this code in a notebook, you’ll see that this command executes way faster than the previous one. And it’s not because our reviews have already been HTML-unescaped — if you re-execute the instruction from the previous section (without <code>batched=True</code>), it will take the same amount of time as before. This is because list comprehensions are usually faster than executing the same code in a <code>for</code> loop, and we also gain some performance by accessing lots of elements at the same time instead of one by one.</p> <p>Using <code>Dataset.map()</code> with <code>batched=True</code> will be essential to unlock the speed of the “fast” tokenizers that we’ll encounter in <a href="/course/chapter6">Chapter 6</a>, which can quickly tokenize big lists of texts. For instance, to tokenize all the drug reviews with a fast tokenizer, we could use a function like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_function</span>(<span class="hljs-params">examples</span>): <span class="hljs-keyword">return</span> tokenizer(examples[<span class="hljs-string">"review"</span>], truncation=<span class="hljs-literal">True</span>)</pre></div> <p>As you saw in <a href="/course/chapter3">Chapter 3</a>, we can pass one or several examples to the tokenizer, so we can use this function with or without <code>batched=True</code>. Let’s take this opportunity to compare the performance of the different options. In a notebook, you can time a one-line instruction by adding <code>%time</code> before the line of code you wish to measure:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>%time tokenized_dataset = drug_dataset.<span class="hljs-built_in">map</span>(tokenize_function, batched=<span class="hljs-literal">True</span>)</pre></div> <p>You can also time a whole cell by putting <code>%%time</code> at the beginning of the cell. On the hardware we executed this on, it showed 10.8s for this instruction (it’s the number written after “Wall time”).</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Execute the same instruction with and without <code>batched=True</code>, then try it with a slow tokenizer (add <code>use_fast=False</code> in the <code>AutoTokenizer.from_pretrained()</code> method) so you can see what numbers you get on your hardware.</p></div> <p>Here are the results we obtained with and without batching, with a fast and a slow tokenizer:</p> <table><thead><tr><th align="center">Options</th> <th align="center">Fast tokenizer</th> <th align="center">Slow tokenizer</th></tr></thead> <tbody><tr><td align="center"><code>batched=True</code></td> <td align="center">10.8s</td> <td align="center">4min41s</td></tr> <tr><td align="center"><code>batched=False</code></td> <td align="center">59.2s</td> <td align="center">5min3s</td></tr></tbody></table> <p>This means that using a fast tokenizer with the <code>batched=True</code> option is 30 times faster than its slow counterpart with no batching — this is truly amazing! That’s the main reason why fast tokenizers are the default when using <code>AutoTokenizer</code> (and why they are called “fast”). They’re able to achieve such a speedup because behind the scenes the tokenization code is executed in Rust, which is a language that makes it easy to parallelize code execution.</p> <p>Parallelization is also the reason for the nearly 6x speedup the fast tokenizer achieves with batching: you can’t parallelize a single tokenization operation, but when you want to tokenize lots of texts at the same time you can just split the execution across several processes, each responsible for its own texts.</p> <p><code>Dataset.map()</code> also has some parallelization capabilities of its own. Since they are not backed by Rust, they won’t let a slow tokenizer catch up with a fast one, but they can still be helpful (especially if you’re using a tokenizer that doesn’t have a fast version). To enable multiprocessing, use the <code>num_proc</code> argument and specify the number of processes to use in your call to <code>Dataset.map()</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>slow_tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>, use_fast=<span class="hljs-literal">False</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">slow_tokenize_function</span>(<span class="hljs-params">examples</span>): <span class="hljs-keyword">return</span> slow_tokenizer(examples[<span class="hljs-string">"review"</span>], truncation=<span class="hljs-literal">True</span>) tokenized_dataset = drug_dataset.<span class="hljs-built_in">map</span>(slow_tokenize_function, batched=<span class="hljs-literal">True</span>, num_proc=<span class="hljs-number">8</span>)</pre></div> <p>You can experiment a little with timing to determine the optimal number of processes to use; in our case 8 seemed to produce the best speed gain. Here are the numbers we got with and without multiprocessing:</p> <table><thead><tr><th align="center">Options</th> <th align="center">Fast tokenizer</th> <th align="center">Slow tokenizer</th></tr></thead> <tbody><tr><td align="center"><code>batched=True</code></td> <td align="center">10.8s</td> <td align="center">4min41s</td></tr> <tr><td align="center"><code>batched=False</code></td> <td align="center">59.2s</td> <td align="center">5min3s</td></tr> <tr><td align="center"><code>batched=True</code>, <code>num_proc=8</code></td> <td align="center">6.52s</td> <td align="center">41.3s</td></tr> <tr><td align="center"><code>batched=False</code>, <code>num_proc=8</code></td> <td align="center">9.49s</td> <td align="center">45.2s</td></tr></tbody></table> <p>Those are much more reasonable results for the slow tokenizer, but the performance of the fast tokenizer was also substantially improved. Note, however, that won’t always be the case — for values of <code>num_proc</code> other than 8, our tests showed that it was faster to use <code>batched=True</code> without that option. In general, we don’t recommend using Python multiprocessing for fast tokenizers with <code>batched=True</code>.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>Using <code>num_proc</code> to speed up your processing is usually a great idea, as long as the function you are using is not already doing some kind of multiprocessing of its own.</p></div> <p>All of this functionality condensed into a single method is already pretty amazing, but there’s more! With <code>Dataset.map()</code> and <code>batched=True</code> you can change the number of elements in your dataset. This is super useful in many situations where you want to create several training features from one example, and we will need to do this as part of the preprocessing for several of the NLP tasks we’ll undertake in <a href="/course/chapter7">Chapter 7</a>.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 In machine learning, an <em>example</em> is usually defined as the set of <em>features</em> that we feed to the model. In some contexts, these features will be the set of columns in a <code>Dataset</code>, but in others (like here and for question answering), multiple features can be extracted from a single example and belong to a single column.</p></div> <p>Let’s have a look at how it works! Here we will tokenize our examples and truncate them to a maximum length of 128, but we will ask the tokenizer to return <em>all</em> the chunks of the texts instead of just the first one. This can be done with <code>return_overflowing_tokens=True</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_and_split</span>(<span class="hljs-params">examples</span>): <span class="hljs-keyword">return</span> tokenizer( examples[<span class="hljs-string">"review"</span>], truncation=<span class="hljs-literal">True</span>, max_length=<span class="hljs-number">128</span>, return_overflowing_tokens=<span class="hljs-literal">True</span>, )</pre></div> <p>Let’s test this on one example before using <code>Dataset.map()</code> on the whole dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>result = tokenize_and_split(drug_dataset[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>]) [<span class="hljs-built_in">len</span>(inp) <span class="hljs-keyword">for</span> inp <span class="hljs-keyword">in</span> result[<span class="hljs-string">"input_ids"</span>]]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">128</span>, <span class="hljs-number">49</span>]</pre></div> <p>So, our first example in the training set became two features because it was tokenized to more than the maximum number of tokens we specified: the first one of length 128 and the second one of length 49. Now let’s do this for all elements of the dataset!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_dataset = drug_dataset.<span class="hljs-built_in">map</span>(tokenize_and_split, batched=<span class="hljs-literal">True</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>ArrowInvalid: Column <span class="hljs-number">1</span> named condition expected length <span class="hljs-number">1463</span> but got length <span class="hljs-number">1000</span></pre></div> <p>Oh no! That didn’t work! Why not? Looking at the error message will give us a clue: there is a mismatch in the lengths of one of the columns, one being of length 1,463 and the other of length 1,000. If you’ve looked at the <code>Dataset.map()</code> <a href="https://huggingface.co/docs/datasets/package_reference/main_classes.html#datasets.Dataset.map" rel="nofollow">documentation</a>, you may recall that it’s the number of samples passed to the function that we are mapping; here those 1,000 examples gave 1,463 new features, resulting in a shape error.</p> <p>The problem is that we’re trying to mix two different datasets of different sizes: the <code>drug_dataset</code> columns will have a certain number of examples (the 1,000 in our error), but the <code>tokenized_dataset</code> we are building will have more (the 1,463 in the error message; it is more than 1,000 because we are tokenizing long reviews into more than one example by using <code>return_overflowing_tokens=True</code>). That doesn’t work for a <code>Dataset</code>, so we need to either remove the columns from the old dataset or make them the same size as they are in the new dataset. We can do the former with the <code>remove_columns</code> argument:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_dataset = drug_dataset.<span class="hljs-built_in">map</span>( tokenize_and_split, batched=<span class="hljs-literal">True</span>, remove_columns=drug_dataset[<span class="hljs-string">"train"</span>].column_names )</pre></div> <p>Now this works without error. We can check that our new dataset has many more elements than the original dataset by comparing the lengths:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">len</span>(tokenized_dataset[<span class="hljs-string">"train"</span>]), <span class="hljs-built_in">len</span>(drug_dataset[<span class="hljs-string">"train"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-number">206772</span>, <span class="hljs-number">138514</span>)</pre></div> <p>We mentioned that we can also deal with the mismatched length problem by making the old columns the same size as the new ones. To do this, we will need the <code>overflow_to_sample_mapping</code> field the tokenizer returns when we set <code>return_overflowing_tokens=True</code>. It gives us a mapping from a new feature index to the index of the sample it originated from. Using this, we can associate each key present in our original dataset with a list of values of the right size by repeating the values of each example as many times as it generates new features:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_and_split</span>(<span class="hljs-params">examples</span>): result = tokenizer( examples[<span class="hljs-string">"review"</span>], truncation=<span class="hljs-literal">True</span>, max_length=<span class="hljs-number">128</span>, return_overflowing_tokens=<span class="hljs-literal">True</span>, ) <span class="hljs-comment"># Extract mapping between new and old indices</span> sample_map = result.pop(<span class="hljs-string">"overflow_to_sample_mapping"</span>) <span class="hljs-keyword">for</span> key, values <span class="hljs-keyword">in</span> examples.items(): result[key] = [values[i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> sample_map] <span class="hljs-keyword">return</span> result</pre></div> <p>We can see it works with <code>Dataset.map()</code> without us needing to remove the old columns:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_dataset = drug_dataset.<span class="hljs-built_in">map</span>(tokenize_and_split, batched=<span class="hljs-literal">True</span>) tokenized_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'condition'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'drugName'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'patient_id'</span>, <span class="hljs-string">'rating'</span>, <span class="hljs-string">'review'</span>, <span class="hljs-string">'review_length'</span>, <span class="hljs-string">'token_type_ids'</span>, <span class="hljs-string">'usefulCount'</span>], num_rows: <span class="hljs-number">206772</span> }) test: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'condition'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'drugName'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'patient_id'</span>, <span class="hljs-string">'rating'</span>, <span class="hljs-string">'review'</span>, <span class="hljs-string">'review_length'</span>, <span class="hljs-string">'token_type_ids'</span>, <span class="hljs-string">'usefulCount'</span>], num_rows: <span class="hljs-number">68876</span> }) })</pre></div> <p>We get the same number of training features as before, but here we’ve kept all the old fields. If you need them for some post-processing after applying your model, you might want to use this approach.</p> <p>You’ve now seen how 🤗 Datasets can be used to preprocess a dataset in various ways. Although the processing functions of 🤗 Datasets will cover most of your model training needs, there may be times when you’ll need to switch to Pandas to access more powerful features, like <code>DataFrame.groupby()</code> or high-level APIs for visualization. Fortunately, 🤗 Datasets is designed to be interoperable with libraries such as Pandas, NumPy, PyTorch, TensorFlow, and JAX. Let’s take a look at how this works.</p> <h2 class="relative group"><a id="from-datasets-to-dataframes-and-back" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#from-datasets-to-dataframes-and-back"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>From <code>Dataset</code>s to <code>DataFrame</code>s and back</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/tfcY1067A5Q" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>To enable the conversion between various third-party libraries, 🤗 Datasets provides a <code>Dataset.set_format()</code> function. This function only changes the <em>output format</em> of the dataset, so you can easily switch to another format without affecting the underlying <em>data format</em>, which is Apache Arrow. The formatting is done in place. To demonstrate, let’s convert our dataset to Pandas:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_dataset.set_format(<span class="hljs-string">"pandas"</span>)</pre></div> <p>Now when we access elements of the dataset we get a <code>pandas.DataFrame</code> instead of a dictionary:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_dataset[<span class="hljs-string">"train"</span>][:<span class="hljs-number">3</span>]</pre></div> <table border="1" class="dataframe"><thead><tr style="text-align: right;"><th></th> <th>patient_id</th> <th>drugName</th> <th>condition</th> <th>review</th> <th>rating</th> <th>date</th> <th>usefulCount</th> <th>review_length</th></tr></thead> <tbody><tr><th>0</th> <td>95260</td> <td>Guanfacine</td> <td>adhd</td> <td>"My son is halfway through his fourth week of Intuniv..."</td> <td>8.0</td> <td>April 27, 2010</td> <td>192</td> <td>141</td></tr> <tr><th>1</th> <td>92703</td> <td>Lybrel</td> <td>birth control</td> <td>"I used to take another oral contraceptive, which had 21 pill cycle, and was very happy- very light periods, max 5 days, no other side effects..."</td> <td>5.0</td> <td>December 14, 2009</td> <td>17</td> <td>134</td></tr> <tr><th>2</th> <td>138000</td> <td>Ortho Evra</td> <td>birth control</td> <td>"This is my first time using any form of birth control..."</td> <td>8.0</td> <td>November 3, 2015</td> <td>10</td> <td>89</td></tr></tbody></table> <p>Let’s create a <code>pandas.DataFrame</code> for the whole training set by selecting all the elements of <code>drug_dataset["train"]</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>train_df = drug_dataset[<span class="hljs-string">"train"</span>][:]</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>🚨 Under the hood, <code>Dataset.set_format()</code> changes the return format for the dataset’s <code>__getitem__()</code> dunder method. This means that when we want to create a new object like <code>train_df</code> from a <code>Dataset</code> in the <code>"pandas"</code> format, we need to slice the whole dataset to obtain a <code>pandas.DataFrame</code>. You can verify for yourself that the type of <code>drug_dataset["train"]</code> is <code>Dataset</code>, irrespective of the output format.</p></div> <p>From here we can use all the Pandas functionality that we want. For example, we can do fancy chaining to compute the class distribution among the <code>condition</code> entries:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>frequencies = ( train_df[<span class="hljs-string">"condition"</span>] .value_counts() .to_frame() .reset_index() .rename(columns={<span class="hljs-string">"index"</span>: <span class="hljs-string">"condition"</span>, <span class="hljs-string">"condition"</span>: <span class="hljs-string">"frequency"</span>}) ) frequencies.head()</pre></div> <table border="1" class="dataframe"><thead><tr style="text-align: right;"><th></th> <th>condition</th> <th>frequency</th></tr></thead> <tbody><tr><th>0</th> <td>birth control</td> <td>27655</td></tr> <tr><th>1</th> <td>depression</td> <td>8023</td></tr> <tr><th>2</th> <td>acne</td> <td>5209</td></tr> <tr><th>3</th> <td>anxiety</td> <td>4991</td></tr> <tr><th>4</th> <td>pain</td> <td>4744</td></tr></tbody></table> <p>And once we’re done with our Pandas analysis, we can always create a new <code>Dataset</code> object by using the <code>Dataset.from_pandas()</code> function as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset freq_dataset = Dataset.from_pandas(frequencies) freq_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Dataset({ features: [<span class="hljs-string">'condition'</span>, <span class="hljs-string">'frequency'</span>], num_rows: <span class="hljs-number">819</span> })</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Compute the average rating per drug and store the result in a new <code>Dataset</code>.</p></div> <p>This wraps up our tour of the various preprocessing techniques available in 🤗 Datasets. To round out the section, let’s create a validation set to prepare the dataset for training a classifier on. Before doing so, we’ll reset the output format of <code>drug_dataset</code> from <code>"pandas"</code> to <code>"arrow"</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_dataset.reset_format()</pre></div> <h2 class="relative group"><a id="creating-a-validation-set" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#creating-a-validation-set"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Creating a validation set</span></h2> <p>Although we have a test set we could use for evaluation, it’s a good practice to leave the test set untouched and create a separate validation set during development. Once you are happy with the performance of your models on the validation set, you can do a final sanity check on the test set. This process helps mitigate the risk that you’ll overfit to the test set and deploy a model that fails on real-world data.</p> <p>🤗 Datasets provides a <code>Dataset.train_test_split()</code> function that is based on the famous functionality from <code>scikit-learn</code>. Let’s use it to split our training set into <code>train</code> and <code>validation</code> splits (we set the <code>seed</code> argument for reproducibility):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_dataset_clean = drug_dataset[<span class="hljs-string">"train"</span>].train_test_split(train_size=<span class="hljs-number">0.8</span>, seed=<span class="hljs-number">42</span>) <span class="hljs-comment"># Rename the default "test" split to "validation"</span> drug_dataset_clean[<span class="hljs-string">"validation"</span>] = drug_dataset_clean.pop(<span class="hljs-string">"test"</span>) <span class="hljs-comment"># Add the "test" set to our `DatasetDict`</span> drug_dataset_clean[<span class="hljs-string">"test"</span>] = drug_dataset[<span class="hljs-string">"test"</span>] drug_dataset_clean</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'patient_id'</span>, <span class="hljs-string">'drugName'</span>, <span class="hljs-string">'condition'</span>, <span class="hljs-string">'review'</span>, <span class="hljs-string">'rating'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'usefulCount'</span>, <span class="hljs-string">'review_length'</span>, <span class="hljs-string">'review_clean'</span>], num_rows: <span class="hljs-number">110811</span> }) validation: Dataset({ features: [<span class="hljs-string">'patient_id'</span>, <span class="hljs-string">'drugName'</span>, <span class="hljs-string">'condition'</span>, <span class="hljs-string">'review'</span>, <span class="hljs-string">'rating'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'usefulCount'</span>, <span class="hljs-string">'review_length'</span>, <span class="hljs-string">'review_clean'</span>], num_rows: <span class="hljs-number">27703</span> }) test: Dataset({ features: [<span class="hljs-string">'patient_id'</span>, <span class="hljs-string">'drugName'</span>, <span class="hljs-string">'condition'</span>, <span class="hljs-string">'review'</span>, <span class="hljs-string">'rating'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'usefulCount'</span>, <span class="hljs-string">'review_length'</span>, <span class="hljs-string">'review_clean'</span>], num_rows: <span class="hljs-number">46108</span> }) })</pre></div> <p>Great, we’ve now prepared a dataset that’s ready for training some models on! In <a href="/course/chapter5/5">section 5</a> we’ll show you how to upload datasets to the Hugging Face Hub, but for now let’s cap off our analysis by looking at a few ways you can save datasets on your local machine.</p> <h2 class="relative group"><a id="saving-a-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#saving-a-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Saving a dataset</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/blF9uxYcKHo" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Although 🤗 Datasets will cache every downloaded dataset and the operations performed on it, there are times when you’ll want to save a dataset to disk (e.g., in case the cache gets deleted). As shown in the table below, 🤗 Datasets provides three main functions to save your dataset in different formats:</p> <table><thead><tr><th align="center">Data format</th> <th align="center">Function</th></tr></thead> <tbody><tr><td align="center">Arrow</td> <td align="center"><code>Dataset.save_to_disk()</code></td></tr> <tr><td align="center">CSV</td> <td align="center"><code>Dataset.to_csv()</code></td></tr> <tr><td align="center">JSON</td> <td align="center"><code>Dataset.to_json()</code></td></tr></tbody></table> <p>For example, let’s save our cleaned dataset in the Arrow format:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug_dataset_clean.save_to_disk(<span class="hljs-string">"drug-reviews"</span>)</pre></div> <p>This will create a directory with the following structure:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>drug-reviews/ ├── dataset_dict.json ├── test │ ├── dataset.arrow │ ├── dataset_info.json │ └── <span class="hljs-keyword">state</span>.json ├── train │ ├── dataset.arrow │ ├── dataset_info.json │ ├── indices.arrow │ └── <span class="hljs-keyword">state</span>.json └── validation ├── dataset.arrow ├── dataset_info.json ├── indices.arrow └── <span class="hljs-keyword">state</span>.json</pre></div> <p>where we can see that each split is associated with its own <em>dataset.arrow</em> table, and some metadata in <em>dataset_info.json</em> and <em>state.json</em>. You can think of the Arrow format as a fancy table of columns and rows that is optimized for building high-performance applications that process and transport large datasets.</p> <p>Once the dataset is saved, we can load it by using the <code>load_from_disk()</code> function as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_from_disk drug_dataset_reloaded = load_from_disk(<span class="hljs-string">"drug-reviews"</span>) drug_dataset_reloaded</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'patient_id'</span>, <span class="hljs-string">'drugName'</span>, <span class="hljs-string">'condition'</span>, <span class="hljs-string">'review'</span>, <span class="hljs-string">'rating'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'usefulCount'</span>, <span class="hljs-string">'review_length'</span>], num_rows: <span class="hljs-number">110811</span> }) validation: Dataset({ features: [<span class="hljs-string">'patient_id'</span>, <span class="hljs-string">'drugName'</span>, <span class="hljs-string">'condition'</span>, <span class="hljs-string">'review'</span>, <span class="hljs-string">'rating'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'usefulCount'</span>, <span class="hljs-string">'review_length'</span>], num_rows: <span class="hljs-number">27703</span> }) test: Dataset({ features: [<span class="hljs-string">'patient_id'</span>, <span class="hljs-string">'drugName'</span>, <span class="hljs-string">'condition'</span>, <span class="hljs-string">'review'</span>, <span class="hljs-string">'rating'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'usefulCount'</span>, <span class="hljs-string">'review_length'</span>], num_rows: <span class="hljs-number">46108</span> }) })</pre></div> <p>For the CSV and JSON formats, we have to store each split as a separate file. One way to do this is by iterating over the keys and values in the <code>DatasetDict</code> object:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> split, dataset <span class="hljs-keyword">in</span> drug_dataset_clean.items(): dataset.to_json(<span class="hljs-string">f"drug-reviews-<span class="hljs-subst">{split}</span>.jsonl"</span>)</pre></div> <p>This saves each split in <a href="https://jsonlines.org" rel="nofollow">JSON Lines format</a>, where each row in the dataset is stored as a single line of JSON. Here’s what the first example looks like:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!head -n <span class="hljs-number">1</span> drug-reviews-train.jsonl</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">"patient_id"</span>:<span class="hljs-number">141780</span>,<span class="hljs-string">"drugName"</span>:<span class="hljs-string">"Escitalopram"</span>,<span class="hljs-string">"condition"</span>:<span class="hljs-string">"depression"</span>,<span class="hljs-string">"review"</span>:<span class="hljs-string">"\"I seemed to experience the regular side effects of LEXAPRO, insomnia, low sex drive, sleepiness during the day. I am taking it at night because my doctor said if it made me tired to take it at night. I assumed it would and started out taking it at night. Strange dreams, some pleasant. I was diagnosed with fibromyalgia. Seems to be helping with the pain. Have had anxiety and depression in my family, and have tried quite a few other medications that haven't worked. Only have been on it for two weeks but feel more positive in my mind, want to accomplish more in my life. Hopefully the side effects will dwindle away, worth it to stick with it from hearing others responses. Great medication.\""</span>,<span class="hljs-string">"rating"</span>:<span class="hljs-number">9.0</span>,<span class="hljs-string">"date"</span>:<span class="hljs-string">"May 29, 2011"</span>,<span class="hljs-string">"usefulCount"</span>:<span class="hljs-number">10</span>,<span class="hljs-string">"review_length"</span>:<span class="hljs-number">125</span>}</pre></div> <p>We can then use the techniques from <a href="/course/chapter5/2">section 2</a> to load the JSON files as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>data_files = { <span class="hljs-string">"train"</span>: <span class="hljs-string">"drug-reviews-train.jsonl"</span>, <span class="hljs-string">"validation"</span>: <span class="hljs-string">"drug-reviews-validation.jsonl"</span>, <span class="hljs-string">"test"</span>: <span class="hljs-string">"drug-reviews-test.jsonl"</span>, } drug_dataset_reloaded = load_dataset(<span class="hljs-string">"json"</span>, data_files=data_files)</pre></div> <p>And that’s it for our excursion into data wrangling with 🤗 Datasets! Now that we have a cleaned dataset for training a model on, here are a few ideas that you could try out:</p> <ol><li>Use the techniques from <a href="/course/chapter3">Chapter 3</a> to train a classifier that can predict the patient condition based on the drug review.</li> <li>Use the <code>summarization</code> pipeline from <a href="/course/chapter1">Chapter 1</a> to generate summaries of the reviews.</li></ol> <p>Next, we’ll take a look at how 🤗 Datasets can enable you to work with huge datasets without blowing up your laptop!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter5/2?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>What if my dataset isn't on the Hub?</a> <a href="/learn/nlp-course/chapter5/4?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Big data? 🤗 Datasets to the rescue!<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;time-to-slice-and-dice&quot;,&quot;url&quot;:&quot;#time-to-slice-and-dice&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Slicing and dicing our data&quot;,&quot;id&quot;:&quot;slicing-and-dicing-our-data&quot;,&quot;url&quot;:&quot;#slicing-and-dicing-our-data&quot;},{&quot;title&quot;:&quot;Creating new columns&quot;,&quot;id&quot;:&quot;creating-new-columns&quot;,&quot;url&quot;:&quot;#creating-new-columns&quot;},{&quot;title&quot;:&quot;The `map()` method's superpowers&quot;,&quot;id&quot;:&quot;the-map-methods-superpowers&quot;,&quot;url&quot;:&quot;#the-map-methods-superpowers&quot;},{&quot;title&quot;:&quot;From `Dataset`s to `DataFrame`s and back&quot;,&quot;id&quot;:&quot;from-datasets-to-dataframes-and-back&quot;,&quot;url&quot;:&quot;#from-datasets-to-dataframes-and-back&quot;},{&quot;title&quot;:&quot;Creating a validation set&quot;,&quot;id&quot;:&quot;creating-a-validation-set&quot;,&quot;url&quot;:&quot;#creating-a-validation-set&quot;},{&quot;title&quot;:&quot;Saving a dataset&quot;,&quot;id&quot;:&quot;saving-a-dataset&quot;,&quot;url&quot;:&quot;#saving-a-dataset&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#time-to-slice-and-dice" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-time-to-slice-and-dice"><wbr>Time to slice and dice</a> <a href="#slicing-and-dicing-our-data" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-slicing-and-dicing-our-data"><wbr>Slicing and dicing our data</a> <a href="#creating-new-columns" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-creating-new-columns"><wbr>Creating new columns</a> <a href="#the-map-methods-superpowers" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-map-methods-superpowers"><wbr>The `map()` method's superpowers</a> <a href="#from-datasets-to-dataframes-and-back" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-from-datasets-to-dataframes-and-back"><wbr>From `<wbr>Dataset`s to `<wbr>Data<wbr>Frame`s and back</a> <a href="#creating-a-validation-set" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-creating-a-validation-set"><wbr>Creating a validation set</a> <a href="#saving-a-dataset" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-saving-a-dataset"><wbr>Saving a dataset</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter5/3" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter5/3"); } </script> <iframe name="__privateStripeMetricsController8320" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter5%2F3%3Ffw%3Dpt&amp;title=Time%20to%20slice%20and%20dice%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:18.903Z
Big data? 🤗 Datasets to the rescue! - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter5/4?fw=pt
## [](#big-data-datasets-to-the-rescue)Big data? 🤗 Datasets to the rescue! [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-5-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter5/section4.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter5/section4.ipynb) Nowadays it is not uncommon to find yourself working with multi-gigabyte datasets, especially if you’re planning to pretrain a transformer like BERT or GPT-2 from scratch. In these cases, even _loading_ the data can be a challenge. For example, the WebText corpus used to pretrain GPT-2 consists of over 8 million documents and 40 GB of text — loading this into your laptop’s RAM is likely to give it a heart attack! Fortunately, 🤗 Datasets has been designed to overcome these limitations. It frees you from memory management problems by treating datasets as _memory-mapped_ files, and from hard drive limits by _streaming_ the entries in a corpus. In this section we’ll explore these features of 🤗 Datasets with a huge 825 GB corpus known as [the Pile](https://pile.eleuther.ai/). Let’s get started! ## [](#what-is-the-pile)What is the Pile? The Pile is an English text corpus that was created by [EleutherAI](https://www.eleuther.ai/) for training large-scale language models. It includes a diverse range of datasets, spanning scientific articles, GitHub code repositories, and filtered web text. The training corpus is available in [14 GB chunks](https://the-eye.eu/public/AI/pile/), and you can also download several of the [individual components](https://the-eye.eu/public/AI/pile_preliminary_components/). Let’s start by taking a look at the PubMed Abstracts dataset, which is a corpus of abstracts from 15 million biomedical publications on [PubMed](https://pubmed.ncbi.nlm.nih.gov/). The dataset is in [JSON Lines format](https://jsonlines.org/) and is compressed using the `zstandard` library, so first we need to install that: Next, we can load the dataset using the method for remote files that we learned in [section 2](/course/chapter5/2): ``` from datasets import load_dataset data_files = "https://the-eye.eu/public/AI/pile_preliminary_components/PUBMED_title_abstracts_2019_baseline.jsonl.zst" pubmed_dataset = load_dataset("json", data_files=data_files, split="train") pubmed_dataset``` ``` Dataset({ features: ['meta', 'text'], num_rows: 15518009 })``` We can see that there are 15,518,009 rows and 2 columns in our dataset — that’s a lot! ✎ By default, 🤗 Datasets will decompress the files needed to load a dataset. If you want to preserve hard drive space, you can pass `DownloadConfig(delete_extracted=True)` to the `download_config` argument of `load_dataset()`. See the [documentation](https://huggingface.co/docs/datasets/package_reference/builder_classes.html?#datasets.utils.DownloadConfig) for more details. Let’s inspect the contents of the first example: ``` {'meta': {'pmid': 11409574, 'language': 'eng'}, 'text': 'Epidemiology of hypoxaemia in children with acute lower respiratory infection.\nTo determine the prevalence of hypoxaemia in children aged under 5 years suffering acute lower respiratory infections (ALRI), the risk factors for hypoxaemia in children under 5 years of age with ALRI, and the association of hypoxaemia with an increased risk of dying in children of the same age ...'}``` Okay, this looks like the abstract from a medical article. Now let’s see how much RAM we’ve used to load the dataset! ## [](#the-magic-of-memory-mapping)The magic of memory mapping A simple way to measure memory usage in Python is with the [`psutil`](https://psutil.readthedocs.io/en/latest/) library, which can be installed with `pip` as follows: It provides a `Process` class that allows us to check the memory usage of the current process as follows: ``` import psutil print(f"RAM used: {psutil.Process().memory_info().rss / (1024 * 1024):.2f} MB")``` Here the `rss` attribute refers to the _resident set size_, which is the fraction of memory that a process occupies in RAM. This measurement also includes the memory used by the Python interpreter and the libraries we’ve loaded, so the actual amount of memory used to load the dataset is a bit smaller. For comparison, let’s see how large the dataset is on disk, using the `dataset_size` attribute. Since the result is expressed in bytes like before, we need to manually convert it to gigabytes: ``` print(f"Number of files in dataset : {pubmed_dataset.dataset_size}") size_gb = pubmed_dataset.dataset_size / (1024**3) print(f"Dataset size (cache file) : {size_gb:.2f} GB")``` ``` Number of files in dataset : 20979437051 Dataset size (cache file) : 19.54 GB``` Nice — despite it being almost 20 GB large, we’re able to load and access the dataset with much less RAM! ✏️ **Try it out!** Pick one of the [subsets](https://the-eye.eu/public/AI/pile_preliminary_components/) from the Pile that is larger than your laptop or desktop’s RAM, load it with 🤗 Datasets, and measure the amount of RAM used. Note that to get an accurate measurement, you’ll want to do this in a new process. You can find the decompressed sizes of each subset in Table 1 of [the Pile paper](https://arxiv.org/abs/2101.00027). If you’re familiar with Pandas, this result might come as a surprise because of Wes Kinney’s famous [rule of thumb](https://wesmckinney.com/blog/apache-arrow-pandas-internals/) that you typically need 5 to 10 times as much RAM as the size of your dataset. So how does 🤗 Datasets solve this memory management problem? 🤗 Datasets treats each dataset as a [memory-mapped file](https://en.wikipedia.org/wiki/Memory-mapped_file), which provides a mapping between RAM and filesystem storage that allows the library to access and operate on elements of the dataset without needing to fully load it into memory. Memory-mapped files can also be shared across multiple processes, which enables methods like `Dataset.map()` to be parallelized without needing to move or copy the dataset. Under the hood, these capabilities are all realized by the [Apache Arrow](https://arrow.apache.org/) memory format and [`pyarrow`](https://arrow.apache.org/docs/python/index.html) library, which make the data loading and processing lightning fast. (For more details about Apache Arrow and comparisons to Pandas, check out [Dejan Simic’s blog post](https://towardsdatascience.com/apache-arrow-read-dataframe-with-zero-memory-69634092b1a).) To see this in action, let’s run a little speed test by iterating over all the elements in the PubMed Abstracts dataset: ``` import timeit code_snippet = """batch_size = 1000 for idx in range(0, len(pubmed_dataset), batch_size): _ = pubmed_dataset[idx:idx + batch_size] """ time = timeit.timeit(stmt=code_snippet, number=1, globals=globals()) print( f"Iterated over {len(pubmed_dataset)} examples (about {size_gb:.1f} GB) in " f"{time:.1f}s, i.e. {size_gb/time:.3f} GB/s" )``` ``` 'Iterated over 15518009 examples (about 19.5 GB) in 64.2s, i.e. 0.304 GB/s'``` Here we’ve used Python’s `timeit` module to measure the execution time taken by `code_snippet`. You’ll typically be able to iterate over a dataset at speed of a few tenths of a GB/s to several GB/s. This works great for the vast majority of applications, but sometimes you’ll have to work with a dataset that is too large to even store on your laptop’s hard drive. For example, if we tried to download the Pile in its entirety, we’d need 825 GB of free disk space! To handle these cases, 🤗 Datasets provides a streaming feature that allows us to download and access elements on the fly, without needing to download the whole dataset. Let’s take a look at how this works. 💡 In Jupyter notebooks you can also time cells using the [`%%timeit` magic function](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-timeit). ## [](#streaming-datasets)Streaming datasets To enable dataset streaming you just need to pass the `streaming=True` argument to the `load_dataset()` function. For example, let’s load the PubMed Abstracts dataset again, but in streaming mode: ``` pubmed_dataset_streamed = load_dataset( "json", data_files=data_files, split="train", streaming=True )``` Instead of the familiar `Dataset` that we’ve encountered elsewhere in this chapter, the object returned with `streaming=True` is an `IterableDataset`. As the name suggests, to access the elements of an `IterableDataset` we need to iterate over it. We can access the first element of our streamed dataset as follows: ``` next(iter(pubmed_dataset_streamed))``` ``` {'meta': {'pmid': 11409574, 'language': 'eng'}, 'text': 'Epidemiology of hypoxaemia in children with acute lower respiratory infection.\nTo determine the prevalence of hypoxaemia in children aged under 5 years suffering acute lower respiratory infections (ALRI), the risk factors for hypoxaemia in children under 5 years of age with ALRI, and the association of hypoxaemia with an increased risk of dying in children of the same age ...'}``` The elements from a streamed dataset can be processed on the fly using `IterableDataset.map()`, which is useful during training if you need to tokenize the inputs. The process is exactly the same as the one we used to tokenize our dataset in [Chapter 3](/course/chapter3), with the only difference being that outputs are returned one by one: ``` from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased") tokenized_dataset = pubmed_dataset_streamed.map(lambda x: tokenizer(x["text"])) next(iter(tokenized_dataset))``` ``` {'input_ids': [101, 4958, 5178, 4328, 6779, ...], 'attention_mask': [1, 1, 1, 1, 1, ...]}``` 💡 To speed up tokenization with streaming you can pass `batched=True`, as we saw in the last section. It will process the examples batch by batch; the default batch size is 1,000 and can be specified with the `batch_size` argument. You can also shuffle a streamed dataset using `IterableDataset.shuffle()`, but unlike `Dataset.shuffle()` this only shuffles the elements in a predefined `buffer_size`: ``` shuffled_dataset = pubmed_dataset_streamed.shuffle(buffer_size=10_000, seed=42) next(iter(shuffled_dataset))``` ``` {'meta': {'pmid': 11410799, 'language': 'eng'}, 'text': 'Randomized study of dose or schedule modification of granulocyte colony-stimulating factor in platinum-based chemotherapy for elderly patients with lung cancer ...'}``` In this example, we selected a random example from the first 10,000 examples in the buffer. Once an example is accessed, its spot in the buffer is filled with the next example in the corpus (i.e., the 10,001st example in the case above). You can also select elements from a streamed dataset using the `IterableDataset.take()` and `IterableDataset.skip()` functions, which act in a similar way to `Dataset.select()`. For example, to select the first 5 examples in the PubMed Abstracts dataset we can do the following: ``` dataset_head = pubmed_dataset_streamed.take(5) list(dataset_head)``` ``` [{'meta': {'pmid': 11409574, 'language': 'eng'}, 'text': 'Epidemiology of hypoxaemia in children with acute lower respiratory infection ...'}, {'meta': {'pmid': 11409575, 'language': 'eng'}, 'text': 'Clinical signs of hypoxaemia in children with acute lower respiratory infection: indicators of oxygen therapy ...'}, {'meta': {'pmid': 11409576, 'language': 'eng'}, 'text': "Hypoxaemia in children with severe pneumonia in Papua New Guinea ..."}, {'meta': {'pmid': 11409577, 'language': 'eng'}, 'text': 'Oxygen concentrators and cylinders ...'}, {'meta': {'pmid': 11409578, 'language': 'eng'}, 'text': 'Oxygen supply in rural africa: a personal experience ...'}]``` Similarly, you can use the `IterableDataset.skip()` function to create training and validation splits from a shuffled dataset as follows: ``` train_dataset = shuffled_dataset.skip(1000) validation_dataset = shuffled_dataset.take(1000)``` Let’s round out our exploration of dataset streaming with a common application: combining multiple datasets together to create a single corpus. 🤗 Datasets provides an `interleave_datasets()` function that converts a list of `IterableDataset` objects into a single `IterableDataset`, where the elements of the new dataset are obtained by alternating among the source examples. This function is especially useful when you’re trying to combine large datasets, so as an example let’s stream the FreeLaw subset of the Pile, which is a 51 GB dataset of legal opinions from US courts: ``` law_dataset_streamed = load_dataset( "json", data_files="https://the-eye.eu/public/AI/pile_preliminary_components/FreeLaw_Opinions.jsonl.zst", split="train", streaming=True, ) next(iter(law_dataset_streamed))``` ``` {'meta': {'case_ID': '110921.json', 'case_jurisdiction': 'scotus.tar.gz', 'date_created': '2010-04-28T17:12:49Z'}, 'text': '\n461 U.S. 238 (1983)\nOLIM ET AL.\nv.\nWAKINEKONA\nNo. 81-1581.\nSupreme Court of United States.\nArgued January 19, 1983.\nDecided April 26, 1983.\nCERTIORARI TO THE UNITED STATES COURT OF APPEALS FOR THE NINTH CIRCUIT\n*239 Michael A. Lilly, First Deputy Attorney General of Hawaii, argued the cause for petitioners. With him on the brief was James H. Dannenberg, Deputy Attorney General...'}``` This dataset is large enough to stress the RAM of most laptops, yet we’ve been able to load and access it without breaking a sweat! Let’s now combine the examples from the FreeLaw and PubMed Abstracts datasets with the `interleave_datasets()` function: ``` from itertools import islice from datasets import interleave_datasets combined_dataset = interleave_datasets([pubmed_dataset_streamed, law_dataset_streamed]) list(islice(combined_dataset, 2))``` ``` [{'meta': {'pmid': 11409574, 'language': 'eng'}, 'text': 'Epidemiology of hypoxaemia in children with acute lower respiratory infection ...'}, {'meta': {'case_ID': '110921.json', 'case_jurisdiction': 'scotus.tar.gz', 'date_created': '2010-04-28T17:12:49Z'}, 'text': '\n461 U.S. 238 (1983)\nOLIM ET AL.\nv.\nWAKINEKONA\nNo. 81-1581.\nSupreme Court of United States.\nArgued January 19, 1983.\nDecided April 26, 1983.\nCERTIORARI TO THE UNITED STATES COURT OF APPEALS FOR THE NINTH CIRCUIT\n*239 Michael A. Lilly, First Deputy Attorney General of Hawaii, argued the cause for petitioners. With him on the brief was James H. Dannenberg, Deputy Attorney General...'}]``` Here we’ve used the `islice()` function from Python’s `itertools` module to select the first two examples from the combined dataset, and we can see that they match the first examples from each of the two source datasets. Finally, if you want to stream the Pile in its 825 GB entirety, you can grab all the prepared files as follows: ``` base_url = "https://the-eye.eu/public/AI/pile/" data_files = { "train": [base_url + "train/" + f"{idx:02d}.jsonl.zst" for idx in range(30)], "validation": base_url + "val.jsonl.zst", "test": base_url + "test.jsonl.zst", } pile_dataset = load_dataset("json", data_files=data_files, streaming=True) next(iter(pile_dataset["train"]))``` ``` {'meta': {'pile_set_name': 'Pile-CC'}, 'text': 'It is done, and submitted. You can play “Survival of the Tastiest” on Android, and on the web...'}``` ✏️ **Try it out!** Use one of the large Common Crawl corpora like [`mc4`](https://huggingface.co/datasets/mc4) or [`oscar`](https://huggingface.co/datasets/oscar) to create a streaming multilingual dataset that represents the spoken proportions of languages in a country of your choice. For example, the four national languages in Switzerland are German, French, Italian, and Romansh, so you could try creating a Swiss corpus by sampling the Oscar subsets according to their spoken proportion. You now have all the tools you need to load and process datasets of all shapes and sizes — but unless you’re exceptionally lucky, there will come a point in your NLP journey where you’ll have to actually create a dataset to solve the problem at hand. That’s the topic of the next section!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Big data? 🤗 Datasets to the rescue! - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter5/4"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Big data? 🤗 Datasets to the rescue! - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;big-data-datasets-to-the-rescue&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;what-is-the-pile&quot;,&quot;title&quot;:&quot;What is the Pile?&quot;},{&quot;local&quot;:&quot;the-magic-of-memory-mapping&quot;,&quot;title&quot;:&quot;The magic of memory mapping&quot;},{&quot;local&quot;:&quot;streaming-datasets&quot;,&quot;title&quot;:&quot;Streaming datasets&quot;}],&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter5/4&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Big data? 🤗 Datasets to the rescue!</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/2?fw=pt">What if my dataset isn't on the Hub? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/3?fw=pt">Time to slice and dice </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter5/4?fw=pt">Big data? 🤗 Datasets to the rescue! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/5?fw=pt">Creating your own dataset </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/6?fw=pt">Semantic search with FAISS </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/7?fw=pt">🤗 Datasets, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="big-data-datasets-to-the-rescue" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#big-data-datasets-to-the-rescue"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Big data? 🤗 Datasets to the rescue!</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-5-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter5/section4.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter5/section4.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>Nowadays it is not uncommon to find yourself working with multi-gigabyte datasets, especially if you’re planning to pretrain a transformer like BERT or GPT-2 from scratch. In these cases, even <em>loading</em> the data can be a challenge. For example, the WebText corpus used to pretrain GPT-2 consists of over 8 million documents and 40 GB of text — loading this into your laptop’s RAM is likely to give it a heart attack!</p> <p>Fortunately, 🤗 Datasets has been designed to overcome these limitations. It frees you from memory management problems by treating datasets as <em>memory-mapped</em> files, and from hard drive limits by <em>streaming</em> the entries in a corpus.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/JwISwTCPPWo" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>In this section we’ll explore these features of 🤗 Datasets with a huge 825 GB corpus known as <a href="https://pile.eleuther.ai" rel="nofollow">the Pile</a>. Let’s get started!</p> <h2 class="relative group"><a id="what-is-the-pile" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#what-is-the-pile"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>What is the Pile?</span></h2> <p>The Pile is an English text corpus that was created by <a href="https://www.eleuther.ai" rel="nofollow">EleutherAI</a> for training large-scale language models. It includes a diverse range of datasets, spanning scientific articles, GitHub code repositories, and filtered web text. The training corpus is available in <a href="https://the-eye.eu/public/AI/pile/" rel="nofollow">14 GB chunks</a>, and you can also download several of the <a href="https://the-eye.eu/public/AI/pile_preliminary_components/" rel="nofollow">individual components</a>. Let’s start by taking a look at the PubMed Abstracts dataset, which is a corpus of abstracts from 15 million biomedical publications on <a href="https://pubmed.ncbi.nlm.nih.gov/" rel="nofollow">PubMed</a>. The dataset is in <a href="https://jsonlines.org" rel="nofollow">JSON Lines format</a> and is compressed using the <code>zstandard</code> library, so first we need to install that:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!pip install zstandard</pre></div> <p>Next, we can load the dataset using the method for remote files that we learned in <a href="/course/chapter5/2">section 2</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-comment"># This takes a few minutes to run, so go grab a tea or coffee while you wait :)</span> data_files = <span class="hljs-string">"https://the-eye.eu/public/AI/pile_preliminary_components/PUBMED_title_abstracts_2019_baseline.jsonl.zst"</span> pubmed_dataset = load_dataset(<span class="hljs-string">"json"</span>, data_files=data_files, split=<span class="hljs-string">"train"</span>) pubmed_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Dataset({ features: [<span class="hljs-string">'meta'</span>, <span class="hljs-string">'text'</span>], num_rows: <span class="hljs-number">15518009</span> })</pre></div> <p>We can see that there are 15,518,009 rows and 2 columns in our dataset — that’s a lot!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✎ By default, 🤗 Datasets will decompress the files needed to load a dataset. If you want to preserve hard drive space, you can pass <code>DownloadConfig(delete_extracted=True)</code> to the <code>download_config</code> argument of <code>load_dataset()</code>. See the <a href="https://huggingface.co/docs/datasets/package_reference/builder_classes.html?#datasets.utils.DownloadConfig" rel="nofollow">documentation</a> for more details.</p></div> <p>Let’s inspect the contents of the first example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pubmed_dataset[<span class="hljs-number">0</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'meta'</span>: {<span class="hljs-string">'pmid'</span>: <span class="hljs-number">11409574</span>, <span class="hljs-string">'language'</span>: <span class="hljs-string">'eng'</span>}, <span class="hljs-string">'text'</span>: <span class="hljs-string">'Epidemiology of hypoxaemia in children with acute lower respiratory infection.\nTo determine the prevalence of hypoxaemia in children aged under 5 years suffering acute lower respiratory infections (ALRI), the risk factors for hypoxaemia in children under 5 years of age with ALRI, and the association of hypoxaemia with an increased risk of dying in children of the same age ...'</span>}</pre></div> <p>Okay, this looks like the abstract from a medical article. Now let’s see how much RAM we’ve used to load the dataset!</p> <h2 class="relative group"><a id="the-magic-of-memory-mapping" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-magic-of-memory-mapping"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The magic of memory mapping</span></h2> <p>A simple way to measure memory usage in Python is with the <a href="https://psutil.readthedocs.io/en/latest/" rel="nofollow"><code>psutil</code></a> library, which can be installed with <code>pip</code> as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!pip install psutil</pre></div> <p>It provides a <code>Process</code> class that allows us to check the memory usage of the current process as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> psutil <span class="hljs-comment"># Process.memory_info is expressed in bytes, so convert to megabytes</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">f"RAM used: <span class="hljs-subst">{psutil.Process().memory_info().rss / (<span class="hljs-number">1024</span> * <span class="hljs-number">1024</span>):<span class="hljs-number">.2</span>f}</span> MB"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>RAM used: <span class="hljs-number">5678.33</span> MB</pre></div> <p>Here the <code>rss</code> attribute refers to the <em>resident set size</em>, which is the fraction of memory that a process occupies in RAM. This measurement also includes the memory used by the Python interpreter and the libraries we’ve loaded, so the actual amount of memory used to load the dataset is a bit smaller. For comparison, let’s see how large the dataset is on disk, using the <code>dataset_size</code> attribute. Since the result is expressed in bytes like before, we need to manually convert it to gigabytes:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(<span class="hljs-string">f"Number of files in dataset : <span class="hljs-subst">{pubmed_dataset.dataset_size}</span>"</span>) size_gb = pubmed_dataset.dataset_size / (<span class="hljs-number">1024</span>**<span class="hljs-number">3</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Dataset size (cache file) : <span class="hljs-subst">{size_gb:<span class="hljs-number">.2</span>f}</span> GB"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Number of files <span class="hljs-keyword">in</span> dataset : <span class="hljs-number">20979437051</span> Dataset size (cache file) : <span class="hljs-number">19.54</span> GB</pre></div> <p>Nice — despite it being almost 20 GB large, we’re able to load and access the dataset with much less RAM!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Pick one of the <a href="https://the-eye.eu/public/AI/pile_preliminary_components/" rel="nofollow">subsets</a> from the Pile that is larger than your laptop or desktop’s RAM, load it with 🤗 Datasets, and measure the amount of RAM used. Note that to get an accurate measurement, you’ll want to do this in a new process. You can find the decompressed sizes of each subset in Table 1 of <a href="https://arxiv.org/abs/2101.00027" rel="nofollow">the Pile paper</a>.</p></div> <p>If you’re familiar with Pandas, this result might come as a surprise because of Wes Kinney’s famous <a href="https://wesmckinney.com/blog/apache-arrow-pandas-internals/" rel="nofollow">rule of thumb</a> that you typically need 5 to 10 times as much RAM as the size of your dataset. So how does 🤗 Datasets solve this memory management problem? 🤗 Datasets treats each dataset as a <a href="https://en.wikipedia.org/wiki/Memory-mapped_file" rel="nofollow">memory-mapped file</a>, which provides a mapping between RAM and filesystem storage that allows the library to access and operate on elements of the dataset without needing to fully load it into memory.</p> <p>Memory-mapped files can also be shared across multiple processes, which enables methods like <code>Dataset.map()</code> to be parallelized without needing to move or copy the dataset. Under the hood, these capabilities are all realized by the <a href="https://arrow.apache.org" rel="nofollow">Apache Arrow</a> memory format and <a href="https://arrow.apache.org/docs/python/index.html" rel="nofollow"><code>pyarrow</code></a> library, which make the data loading and processing lightning fast. (For more details about Apache Arrow and comparisons to Pandas, check out <a href="https://towardsdatascience.com/apache-arrow-read-dataframe-with-zero-memory-69634092b1a" rel="nofollow">Dejan Simic’s blog post</a>.) To see this in action, let’s run a little speed test by iterating over all the elements in the PubMed Abstracts dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> timeit code_snippet = <span class="hljs-string">"""batch_size = 1000 for idx in range(0, len(pubmed_dataset), batch_size): _ = pubmed_dataset[idx:idx + batch_size] """</span> time = timeit.timeit(stmt=code_snippet, number=<span class="hljs-number">1</span>, <span class="hljs-built_in">globals</span>=<span class="hljs-built_in">globals</span>()) <span class="hljs-built_in">print</span>( <span class="hljs-string">f"Iterated over <span class="hljs-subst">{<span class="hljs-built_in">len</span>(pubmed_dataset)}</span> examples (about <span class="hljs-subst">{size_gb:<span class="hljs-number">.1</span>f}</span> GB) in "</span> <span class="hljs-string">f"<span class="hljs-subst">{time:<span class="hljs-number">.1</span>f}</span>s, i.e. <span class="hljs-subst">{size_gb/time:<span class="hljs-number">.3</span>f}</span> GB/s"</span> )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'Iterated over 15518009 examples (about 19.5 GB) in 64.2s, i.e. 0.304 GB/s'</span></pre></div> <p>Here we’ve used Python’s <code>timeit</code> module to measure the execution time taken by <code>code_snippet</code>. You’ll typically be able to iterate over a dataset at speed of a few tenths of a GB/s to several GB/s. This works great for the vast majority of applications, but sometimes you’ll have to work with a dataset that is too large to even store on your laptop’s hard drive. For example, if we tried to download the Pile in its entirety, we’d need 825 GB of free disk space! To handle these cases, 🤗 Datasets provides a streaming feature that allows us to download and access elements on the fly, without needing to download the whole dataset. Let’s take a look at how this works.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 In Jupyter notebooks you can also time cells using the <a href="https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-timeit" rel="nofollow"><code>%%timeit</code> magic function</a>.</p></div> <h2 class="relative group"><a id="streaming-datasets" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#streaming-datasets"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Streaming datasets</span></h2> <p>To enable dataset streaming you just need to pass the <code>streaming=True</code> argument to the <code>load_dataset()</code> function. For example, let’s load the PubMed Abstracts dataset again, but in streaming mode:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pubmed_dataset_streamed = load_dataset( <span class="hljs-string">"json"</span>, data_files=data_files, split=<span class="hljs-string">"train"</span>, streaming=<span class="hljs-literal">True</span> )</pre></div> <p>Instead of the familiar <code>Dataset</code> that we’ve encountered elsewhere in this chapter, the object returned with <code>streaming=True</code> is an <code>IterableDataset</code>. As the name suggests, to access the elements of an <code>IterableDataset</code> we need to iterate over it. We can access the first element of our streamed dataset as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">next</span>(<span class="hljs-built_in">iter</span>(pubmed_dataset_streamed))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'meta'</span>: {<span class="hljs-string">'pmid'</span>: <span class="hljs-number">11409574</span>, <span class="hljs-string">'language'</span>: <span class="hljs-string">'eng'</span>}, <span class="hljs-string">'text'</span>: <span class="hljs-string">'Epidemiology of hypoxaemia in children with acute lower respiratory infection.\nTo determine the prevalence of hypoxaemia in children aged under 5 years suffering acute lower respiratory infections (ALRI), the risk factors for hypoxaemia in children under 5 years of age with ALRI, and the association of hypoxaemia with an increased risk of dying in children of the same age ...'</span>}</pre></div> <p>The elements from a streamed dataset can be processed on the fly using <code>IterableDataset.map()</code>, which is useful during training if you need to tokenize the inputs. The process is exactly the same as the one we used to tokenize our dataset in <a href="/course/chapter3">Chapter 3</a>, with the only difference being that outputs are returned one by one:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>) tokenized_dataset = pubmed_dataset_streamed.<span class="hljs-built_in">map</span>(<span class="hljs-keyword">lambda</span> x: tokenizer(x[<span class="hljs-string">"text"</span>])) <span class="hljs-built_in">next</span>(<span class="hljs-built_in">iter</span>(tokenized_dataset))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'input_ids'</span>: [<span class="hljs-number">101</span>, <span class="hljs-number">4958</span>, <span class="hljs-number">5178</span>, <span class="hljs-number">4328</span>, <span class="hljs-number">6779</span>, ...], <span class="hljs-string">'attention_mask'</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, ...]}</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 To speed up tokenization with streaming you can pass <code>batched=True</code>, as we saw in the last section. It will process the examples batch by batch; the default batch size is 1,000 and can be specified with the <code>batch_size</code> argument.</p></div> <p>You can also shuffle a streamed dataset using <code>IterableDataset.shuffle()</code>, but unlike <code>Dataset.shuffle()</code> this only shuffles the elements in a predefined <code>buffer_size</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>shuffled_dataset = pubmed_dataset_streamed.shuffle(buffer_size=<span class="hljs-number">10_000</span>, seed=<span class="hljs-number">42</span>) <span class="hljs-built_in">next</span>(<span class="hljs-built_in">iter</span>(shuffled_dataset))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'meta'</span>: {<span class="hljs-string">'pmid'</span>: <span class="hljs-number">11410799</span>, <span class="hljs-string">'language'</span>: <span class="hljs-string">'eng'</span>}, <span class="hljs-string">'text'</span>: <span class="hljs-string">'Randomized study of dose or schedule modification of granulocyte colony-stimulating factor in platinum-based chemotherapy for elderly patients with lung cancer ...'</span>}</pre></div> <p>In this example, we selected a random example from the first 10,000 examples in the buffer. Once an example is accessed, its spot in the buffer is filled with the next example in the corpus (i.e., the 10,001st example in the case above). You can also select elements from a streamed dataset using the <code>IterableDataset.take()</code> and <code>IterableDataset.skip()</code> functions, which act in a similar way to <code>Dataset.select()</code>. For example, to select the first 5 examples in the PubMed Abstracts dataset we can do the following:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>dataset_head = pubmed_dataset_streamed.take(<span class="hljs-number">5</span>) <span class="hljs-built_in">list</span>(dataset_head)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'meta'</span>: {<span class="hljs-string">'pmid'</span>: <span class="hljs-number">11409574</span>, <span class="hljs-string">'language'</span>: <span class="hljs-string">'eng'</span>}, <span class="hljs-string">'text'</span>: <span class="hljs-string">'Epidemiology of hypoxaemia in children with acute lower respiratory infection ...'</span>}, {<span class="hljs-string">'meta'</span>: {<span class="hljs-string">'pmid'</span>: <span class="hljs-number">11409575</span>, <span class="hljs-string">'language'</span>: <span class="hljs-string">'eng'</span>}, <span class="hljs-string">'text'</span>: <span class="hljs-string">'Clinical signs of hypoxaemia in children with acute lower respiratory infection: indicators of oxygen therapy ...'</span>}, {<span class="hljs-string">'meta'</span>: {<span class="hljs-string">'pmid'</span>: <span class="hljs-number">11409576</span>, <span class="hljs-string">'language'</span>: <span class="hljs-string">'eng'</span>}, <span class="hljs-string">'text'</span>: <span class="hljs-string">"Hypoxaemia in children with severe pneumonia in Papua New Guinea ..."</span>}, {<span class="hljs-string">'meta'</span>: {<span class="hljs-string">'pmid'</span>: <span class="hljs-number">11409577</span>, <span class="hljs-string">'language'</span>: <span class="hljs-string">'eng'</span>}, <span class="hljs-string">'text'</span>: <span class="hljs-string">'Oxygen concentrators and cylinders ...'</span>}, {<span class="hljs-string">'meta'</span>: {<span class="hljs-string">'pmid'</span>: <span class="hljs-number">11409578</span>, <span class="hljs-string">'language'</span>: <span class="hljs-string">'eng'</span>}, <span class="hljs-string">'text'</span>: <span class="hljs-string">'Oxygen supply in rural africa: a personal experience ...'</span>}]</pre></div> <p>Similarly, you can use the <code>IterableDataset.skip()</code> function to create training and validation splits from a shuffled dataset as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># Skip the first 1,000 examples and include the rest in the training set</span> train_dataset = shuffled_dataset.skip(<span class="hljs-number">1000</span>) <span class="hljs-comment"># Take the first 1,000 examples for the validation set</span> validation_dataset = shuffled_dataset.take(<span class="hljs-number">1000</span>)</pre></div> <p>Let’s round out our exploration of dataset streaming with a common application: combining multiple datasets together to create a single corpus. 🤗 Datasets provides an <code>interleave_datasets()</code> function that converts a list of <code>IterableDataset</code> objects into a single <code>IterableDataset</code>, where the elements of the new dataset are obtained by alternating among the source examples. This function is especially useful when you’re trying to combine large datasets, so as an example let’s stream the FreeLaw subset of the Pile, which is a 51 GB dataset of legal opinions from US courts:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>law_dataset_streamed = load_dataset( <span class="hljs-string">"json"</span>, data_files=<span class="hljs-string">"https://the-eye.eu/public/AI/pile_preliminary_components/FreeLaw_Opinions.jsonl.zst"</span>, split=<span class="hljs-string">"train"</span>, streaming=<span class="hljs-literal">True</span>, ) <span class="hljs-built_in">next</span>(<span class="hljs-built_in">iter</span>(law_dataset_streamed))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'meta'</span>: {<span class="hljs-string">'case_ID'</span>: <span class="hljs-string">'110921.json'</span>, <span class="hljs-string">'case_jurisdiction'</span>: <span class="hljs-string">'scotus.tar.gz'</span>, <span class="hljs-string">'date_created'</span>: <span class="hljs-string">'2010-04-28T17:12:49Z'</span>}, <span class="hljs-string">'text'</span>: <span class="hljs-string">'\n461 U.S. 238 (1983)\nOLIM ET AL.\nv.\nWAKINEKONA\nNo. 81-1581.\nSupreme Court of United States.\nArgued January 19, 1983.\nDecided April 26, 1983.\nCERTIORARI TO THE UNITED STATES COURT OF APPEALS FOR THE NINTH CIRCUIT\n*239 Michael A. Lilly, First Deputy Attorney General of Hawaii, argued the cause for petitioners. With him on the brief was James H. Dannenberg, Deputy Attorney General...'</span>}</pre></div> <p>This dataset is large enough to stress the RAM of most laptops, yet we’ve been able to load and access it without breaking a sweat! Let’s now combine the examples from the FreeLaw and PubMed Abstracts datasets with the <code>interleave_datasets()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> itertools <span class="hljs-keyword">import</span> islice <span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> interleave_datasets combined_dataset = interleave_datasets([pubmed_dataset_streamed, law_dataset_streamed]) <span class="hljs-built_in">list</span>(islice(combined_dataset, <span class="hljs-number">2</span>))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'meta'</span>: {<span class="hljs-string">'pmid'</span>: <span class="hljs-number">11409574</span>, <span class="hljs-string">'language'</span>: <span class="hljs-string">'eng'</span>}, <span class="hljs-string">'text'</span>: <span class="hljs-string">'Epidemiology of hypoxaemia in children with acute lower respiratory infection ...'</span>}, {<span class="hljs-string">'meta'</span>: {<span class="hljs-string">'case_ID'</span>: <span class="hljs-string">'110921.json'</span>, <span class="hljs-string">'case_jurisdiction'</span>: <span class="hljs-string">'scotus.tar.gz'</span>, <span class="hljs-string">'date_created'</span>: <span class="hljs-string">'2010-04-28T17:12:49Z'</span>}, <span class="hljs-string">'text'</span>: <span class="hljs-string">'\n461 U.S. 238 (1983)\nOLIM ET AL.\nv.\nWAKINEKONA\nNo. 81-1581.\nSupreme Court of United States.\nArgued January 19, 1983.\nDecided April 26, 1983.\nCERTIORARI TO THE UNITED STATES COURT OF APPEALS FOR THE NINTH CIRCUIT\n*239 Michael A. Lilly, First Deputy Attorney General of Hawaii, argued the cause for petitioners. With him on the brief was James H. Dannenberg, Deputy Attorney General...'</span>}]</pre></div> <p>Here we’ve used the <code>islice()</code> function from Python’s <code>itertools</code> module to select the first two examples from the combined dataset, and we can see that they match the first examples from each of the two source datasets.</p> <p>Finally, if you want to stream the Pile in its 825 GB entirety, you can grab all the prepared files as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>base_url = <span class="hljs-string">"https://the-eye.eu/public/AI/pile/"</span> data_files = { <span class="hljs-string">"train"</span>: [base_url + <span class="hljs-string">"train/"</span> + <span class="hljs-string">f"<span class="hljs-subst">{idx:02d}</span>.jsonl.zst"</span> <span class="hljs-keyword">for</span> idx <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">30</span>)], <span class="hljs-string">"validation"</span>: base_url + <span class="hljs-string">"val.jsonl.zst"</span>, <span class="hljs-string">"test"</span>: base_url + <span class="hljs-string">"test.jsonl.zst"</span>, } pile_dataset = load_dataset(<span class="hljs-string">"json"</span>, data_files=data_files, streaming=<span class="hljs-literal">True</span>) <span class="hljs-built_in">next</span>(<span class="hljs-built_in">iter</span>(pile_dataset[<span class="hljs-string">"train"</span>]))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'meta'</span>: {<span class="hljs-string">'pile_set_name'</span>: <span class="hljs-string">'Pile-CC'</span>}, <span class="hljs-string">'text'</span>: <span class="hljs-string">'It is done, and submitted. You can play “Survival of the Tastiest” on Android, and on the web...'</span>}</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Use one of the large Common Crawl corpora like <a href="https://huggingface.co/datasets/mc4" rel="nofollow"><code>mc4</code></a> or <a href="https://huggingface.co/datasets/oscar" rel="nofollow"><code>oscar</code></a> to create a streaming multilingual dataset that represents the spoken proportions of languages in a country of your choice. For example, the four national languages in Switzerland are German, French, Italian, and Romansh, so you could try creating a Swiss corpus by sampling the Oscar subsets according to their spoken proportion.</p></div> <p>You now have all the tools you need to load and process datasets of all shapes and sizes — but unless you’re exceptionally lucky, there will come a point in your NLP journey where you’ll have to actually create a dataset to solve the problem at hand. That’s the topic of the next section!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter5/3?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Time to slice and dice</a> <a href="/learn/nlp-course/chapter5/5?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Creating your own dataset<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;big-data-datasets-to-the-rescue&quot;,&quot;url&quot;:&quot;#big-data-datasets-to-the-rescue&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;What is the Pile?&quot;,&quot;id&quot;:&quot;what-is-the-pile&quot;,&quot;url&quot;:&quot;#what-is-the-pile&quot;},{&quot;title&quot;:&quot;The magic of memory mapping&quot;,&quot;id&quot;:&quot;the-magic-of-memory-mapping&quot;,&quot;url&quot;:&quot;#the-magic-of-memory-mapping&quot;},{&quot;title&quot;:&quot;Streaming datasets&quot;,&quot;id&quot;:&quot;streaming-datasets&quot;,&quot;url&quot;:&quot;#streaming-datasets&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#big-data-datasets-to-the-rescue" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-big-data-datasets-to-the-rescue"><wbr>Big data? 🤗 <wbr>Datasets to the rescue!</a> <a href="#what-is-the-pile" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-what-is-the-pile"><wbr>What is the <wbr>Pile?</a> <a href="#the-magic-of-memory-mapping" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-magic-of-memory-mapping"><wbr>The magic of memory mapping</a> <a href="#streaming-datasets" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-streaming-datasets"><wbr>Streaming datasets</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter5/4" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter5/4"); } </script> <iframe name="__privateStripeMetricsController3220" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter5%2F4%3Ffw%3Dpt&amp;title=Big%20data%3F%20%F0%9F%A4%97%20Datasets%20to%20the%20rescue!%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:19.028Z
Creating your own dataset - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter5/5?fw=pt
## [](#creating-your-own-dataset)Creating your own dataset [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-5-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter5/section5.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter5/section5.ipynb) Sometimes the dataset that you need to build an NLP application doesn’t exist, so you’ll need to create it yourself. In this section we’ll show you how to create a corpus of [GitHub issues](https://github.com/features/issues/), which are commonly used to track bugs or features in GitHub repositories. This corpus could be used for various purposes, including: - Exploring how long it takes to close open issues or pull requests - Training a _multilabel classifier_ that can tag issues with metadata based on the issue’s description (e.g., “bug,” “enhancement,” or “question”) - Creating a semantic search engine to find which issues match a user’s query Here we’ll focus on creating the corpus, and in the next section we’ll tackle the semantic search application. To keep things meta, we’ll use the GitHub issues associated with a popular open source project: 🤗 Datasets! Let’s take a look at how to get the data and explore the information contained in these issues. ## [](#getting-the-data)Getting the data You can find all the issues in 🤗 Datasets by navigating to the repository’s [Issues tab](https://github.com/huggingface/datasets/issues). As shown in the following screenshot, at the time of writing there were 331 open issues and 668 closed ones. ![The GitHub issues associated with 🤗 Datasets.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/datasets-issues.png) If you click on one of these issues you’ll find it contains a title, a description, and a set of labels that characterize the issue. An example is shown in the screenshot below. ![A typical GitHub issue in the 🤗 Datasets repository.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/datasets-issues-single.png) To download all the repository’s issues, we’ll use the [GitHub REST API](https://docs.github.com/en/rest) to poll the [`Issues` endpoint](https://docs.github.com/en/rest/reference/issues#list-repository-issues). This endpoint returns a list of JSON objects, with each object containing a large number of fields that include the title and description as well as metadata about the status of the issue and so on. A convenient way to download the issues is via the `requests` library, which is the standard way for making HTTP requests in Python. You can install the library by running: Once the library is installed, you can make GET requests to the `Issues` endpoint by invoking the `requests.get()` function. For example, you can run the following command to retrieve the first issue on the first page: ``` import requests url = "https://api.github.com/repos/huggingface/datasets/issues?page=1&per_page=1" response = requests.get(url)``` The `response` object contains a lot of useful information about the request, including the HTTP status code: where a `200` status means the request was successful (you can find a list of possible HTTP status codes [here](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes)). What we are really interested in, though, is the _payload_, which can be accessed in various formats like bytes, strings, or JSON. Since we know our issues are in JSON format, let’s inspect the payload as follows: ``` [{'url': 'https://api.github.com/repos/huggingface/datasets/issues/2792', 'repository_url': 'https://api.github.com/repos/huggingface/datasets', 'labels_url': 'https://api.github.com/repos/huggingface/datasets/issues/2792/labels{/name}', 'comments_url': 'https://api.github.com/repos/huggingface/datasets/issues/2792/comments', 'events_url': 'https://api.github.com/repos/huggingface/datasets/issues/2792/events', 'html_url': 'https://github.com/huggingface/datasets/pull/2792', 'id': 968650274, 'node_id': 'MDExOlB1bGxSZXF1ZXN0NzEwNzUyMjc0', 'number': 2792, 'title': 'Update GooAQ', 'user': {'login': 'bhavitvyamalik', 'id': 19718818, 'node_id': 'MDQ6VXNlcjE5NzE4ODE4', 'avatar_url': 'https://avatars.githubusercontent.com/u/19718818?v=4', 'gravatar_id': '', 'url': 'https://api.github.com/users/bhavitvyamalik', 'html_url': 'https://github.com/bhavitvyamalik', 'followers_url': 'https://api.github.com/users/bhavitvyamalik/followers', 'following_url': 'https://api.github.com/users/bhavitvyamalik/following{/other_user}', 'gists_url': 'https://api.github.com/users/bhavitvyamalik/gists{/gist_id}', 'starred_url': 'https://api.github.com/users/bhavitvyamalik/starred{/owner}{/repo}', 'subscriptions_url': 'https://api.github.com/users/bhavitvyamalik/subscriptions', 'organizations_url': 'https://api.github.com/users/bhavitvyamalik/orgs', 'repos_url': 'https://api.github.com/users/bhavitvyamalik/repos', 'events_url': 'https://api.github.com/users/bhavitvyamalik/events{/privacy}', 'received_events_url': 'https://api.github.com/users/bhavitvyamalik/received_events', 'type': 'User', 'site_admin': False}, 'labels': [], 'state': 'open', 'locked': False, 'assignee': None, 'assignees': [], 'milestone': None, 'comments': 1, 'created_at': '2021-08-12T11:40:18Z', 'updated_at': '2021-08-12T12:31:17Z', 'closed_at': None, 'author_association': 'CONTRIBUTOR', 'active_lock_reason': None, 'pull_request': {'url': 'https://api.github.com/repos/huggingface/datasets/pulls/2792', 'html_url': 'https://github.com/huggingface/datasets/pull/2792', 'diff_url': 'https://github.com/huggingface/datasets/pull/2792.diff', 'patch_url': 'https://github.com/huggingface/datasets/pull/2792.patch'}, 'body': '[GooAQ](https://github.com/allenai/gooaq) dataset was recently updated after splits were added for the same. This PR contains new updated GooAQ with train/val/test splits and updated README as well.', 'performed_via_github_app': None}]``` Whoa, that’s a lot of information! We can see useful fields like `title`, `body`, and `number` that describe the issue, as well as information about the GitHub user who opened the issue. ✏️ **Try it out!** Click on a few of the URLs in the JSON payload above to get a feel for what type of information each GitHub issue is linked to. As described in the GitHub [documentation](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting), unauthenticated requests are limited to 60 requests per hour. Although you can increase the `per_page` query parameter to reduce the number of requests you make, you will still hit the rate limit on any repository that has more than a few thousand issues. So instead, you should follow GitHub’s [instructions](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) on creating a _personal access token_ so that you can boost the rate limit to 5,000 requests per hour. Once you have your token, you can include it as part of the request header: ``` GITHUB_TOKEN = xxx headers = {"Authorization": f"token {GITHUB_TOKEN}"}``` ⚠️ Do not share a notebook with your `GITHUB_TOKEN` pasted in it. We recommend you delete the last cell once you have executed it to avoid leaking this information accidentally. Even better, store the token in a _.env_ file and use the [`python-dotenv` library](https://github.com/theskumar/python-dotenv) to load it automatically for you as an environment variable. Now that we have our access token, let’s create a function that can download all the issues from a GitHub repository: ``` import time import math from pathlib import Path import pandas as pd from tqdm.notebook import tqdm def fetch_issues( owner="huggingface", repo="datasets", num_issues=10_000, rate_limit=5_000, issues_path=Path("."), ): if not issues_path.is_dir(): issues_path.mkdir(exist_ok=True) batch = [] all_issues = [] per_page = 100 num_pages = math.ceil(num_issues / per_page) base_url = "https://api.github.com/repos" for page in tqdm(range(num_pages)): query = f"issues?page={page}&per_page={per_page}&state=all" issues = requests.get(f"{base_url}/{owner}/{repo}/{query}", headers=headers) batch.extend(issues.json()) if len(batch) > rate_limit and len(all_issues) < num_issues: all_issues.extend(batch) batch = [] print(f"Reached GitHub rate limit. Sleeping for one hour ...") time.sleep(60 * 60 + 1) all_issues.extend(batch) df = pd.DataFrame.from_records(all_issues) df.to_json(f"{issues_path}/{repo}-issues.jsonl", orient="records", lines=True) print( f"Downloaded all the issues for {repo}! Dataset stored at {issues_path}/{repo}-issues.jsonl" )``` Now when we call `fetch_issues()` it will download all the issues in batches to avoid exceeding GitHub’s limit on the number of requests per hour; the result will be stored in a _repository\_name-issues.jsonl_ file, where each line is a JSON object the represents an issue. Let’s use this function to grab all the issues from 🤗 Datasets: Once the issues are downloaded we can load them locally using our newfound skills from [section 2](/course/chapter5/2): ``` issues_dataset = load_dataset("json", data_files="datasets-issues.jsonl", split="train") issues_dataset``` ``` Dataset({ features: ['url', 'repository_url', 'labels_url', 'comments_url', 'events_url', 'html_url', 'id', 'node_id', 'number', 'title', 'user', 'labels', 'state', 'locked', 'assignee', 'assignees', 'milestone', 'comments', 'created_at', 'updated_at', 'closed_at', 'author_association', 'active_lock_reason', 'pull_request', 'body', 'timeline_url', 'performed_via_github_app'], num_rows: 3019 })``` Great, we’ve created our first dataset from scratch! But why are there several thousand issues when the [Issues tab](https://github.com/huggingface/datasets/issues) of the 🤗 Datasets repository only shows around 1,000 issues in total 🤔? As described in the GitHub [documentation](https://docs.github.com/en/rest/reference/issues#list-issues-assigned-to-the-authenticated-user), that’s because we’ve downloaded all the pull requests as well: > GitHub’s REST API v3 considers every pull request an issue, but not every issue is a pull request. For this reason, “Issues” endpoints may return both issues and pull requests in the response. You can identify pull requests by the `pull_request` key. Be aware that the `id` of a pull request returned from “Issues” endpoints will be an issue id. Since the contents of issues and pull requests are quite different, let’s do some minor preprocessing to enable us to distinguish between them. ## [](#cleaning-up-the-data)Cleaning up the data The above snippet from GitHub’s documentation tells us that the `pull_request` column can be used to differentiate between issues and pull requests. Let’s look at a random sample to see what the difference is. As we did in [section 3](/course/chapter5/3), we’ll chain `Dataset.shuffle()` and `Dataset.select()` to create a random sample and then zip the `html_url` and `pull_request` columns so we can compare the various URLs: ``` sample = issues_dataset.shuffle(seed=666).select(range(3)) for url, pr in zip(sample["html_url"], sample["pull_request"]): print(f">> URL: {url}") print(f">> Pull request: {pr}\n")``` ``` >> URL: https://github.com/huggingface/datasets/pull/850 >> Pull request: {'url': 'https://api.github.com/repos/huggingface/datasets/pulls/850', 'html_url': 'https://github.com/huggingface/datasets/pull/850', 'diff_url': 'https://github.com/huggingface/datasets/pull/850.diff', 'patch_url': 'https://github.com/huggingface/datasets/pull/850.patch'} >> URL: https://github.com/huggingface/datasets/issues/2773 >> Pull request: None >> URL: https://github.com/huggingface/datasets/pull/783 >> Pull request: {'url': 'https://api.github.com/repos/huggingface/datasets/pulls/783', 'html_url': 'https://github.com/huggingface/datasets/pull/783', 'diff_url': 'https://github.com/huggingface/datasets/pull/783.diff', 'patch_url': 'https://github.com/huggingface/datasets/pull/783.patch'}``` Here we can see that each pull request is associated with various URLs, while ordinary issues have a `None` entry. We can use this distinction to create a new `is_pull_request` column that checks whether the `pull_request` field is `None` or not: ``` issues_dataset = issues_dataset.map( lambda x: {"is_pull_request": False if x["pull_request"] is None else True} )``` ✏️ **Try it out!** Calculate the average time it takes to close issues in 🤗 Datasets. You may find the `Dataset.filter()` function useful to filter out the pull requests and open issues, and you can use the `Dataset.set_format()` function to convert the dataset to a `DataFrame` so you can easily manipulate the `created_at` and `closed_at` timestamps. For bonus points, calculate the average time it takes to close pull requests. Although we could proceed to further clean up the dataset by dropping or renaming some columns, it is generally a good practice to keep the dataset as “raw” as possible at this stage so that it can be easily used in multiple applications. Before we push our dataset to the Hugging Face Hub, let’s deal with one thing that’s missing from it: the comments associated with each issue and pull request. We’ll add them next with — you guessed it — the GitHub REST API! ## [](#augmenting-the-dataset)Augmenting the dataset As shown in the following screenshot, the comments associated with an issue or pull request provide a rich source of information, especially if we’re interested in building a search engine to answer user queries about the library. ![Comments associated with an issue about 🤗 Datasets.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/datasets-issues-comment.png) The GitHub REST API provides a [`Comments` endpoint](https://docs.github.com/en/rest/reference/issues#list-issue-comments) that returns all the comments associated with an issue number. Let’s test the endpoint to see what it returns: ``` issue_number = 2792 url = f"https://api.github.com/repos/huggingface/datasets/issues/{issue_number}/comments" response = requests.get(url, headers=headers) response.json()``` ``` [{'url': 'https://api.github.com/repos/huggingface/datasets/issues/comments/897594128', 'html_url': 'https://github.com/huggingface/datasets/pull/2792#issuecomment-897594128', 'issue_url': 'https://api.github.com/repos/huggingface/datasets/issues/2792', 'id': 897594128, 'node_id': 'IC_kwDODunzps41gDMQ', 'user': {'login': 'bhavitvyamalik', 'id': 19718818, 'node_id': 'MDQ6VXNlcjE5NzE4ODE4', 'avatar_url': 'https://avatars.githubusercontent.com/u/19718818?v=4', 'gravatar_id': '', 'url': 'https://api.github.com/users/bhavitvyamalik', 'html_url': 'https://github.com/bhavitvyamalik', 'followers_url': 'https://api.github.com/users/bhavitvyamalik/followers', 'following_url': 'https://api.github.com/users/bhavitvyamalik/following{/other_user}', 'gists_url': 'https://api.github.com/users/bhavitvyamalik/gists{/gist_id}', 'starred_url': 'https://api.github.com/users/bhavitvyamalik/starred{/owner}{/repo}', 'subscriptions_url': 'https://api.github.com/users/bhavitvyamalik/subscriptions', 'organizations_url': 'https://api.github.com/users/bhavitvyamalik/orgs', 'repos_url': 'https://api.github.com/users/bhavitvyamalik/repos', 'events_url': 'https://api.github.com/users/bhavitvyamalik/events{/privacy}', 'received_events_url': 'https://api.github.com/users/bhavitvyamalik/received_events', 'type': 'User', 'site_admin': False}, 'created_at': '2021-08-12T12:21:52Z', 'updated_at': '2021-08-12T12:31:17Z', 'author_association': 'CONTRIBUTOR', 'body': "@albertvillanova my tests are failing here:\r\n```\r\ndataset_name = 'gooaq'\r\n\r\n def test_load_dataset(self, dataset_name):\r\n configs = self.dataset_tester.load_all_configs(dataset_name, is_local=True)[:1]\r\n> self.dataset_tester.check_load_dataset(dataset_name, configs, is_local=True, use_local_dummy_data=True)\r\n\r\ntests/test_dataset_common.py:234: \r\n_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \r\ntests/test_dataset_common.py:187: in check_load_dataset\r\n self.parent.assertTrue(len(dataset[split]) > 0)\r\nE AssertionError: False is not true\r\n```\r\nWhen I try loading dataset on local machine it works fine. Any suggestions on how can I avoid this error?", 'performed_via_github_app': None}]``` We can see that the comment is stored in the `body` field, so let’s write a simple function that returns all the comments associated with an issue by picking out the `body` contents for each element in `response.json()`: ``` def get_comments(issue_number): url = f"https://api.github.com/repos/huggingface/datasets/issues/{issue_number}/comments" response = requests.get(url, headers=headers) return [r["body"] for r in response.json()] get_comments(2792)``` ``` ["@albertvillanova my tests are failing here:\r\n```\r\ndataset_name = 'gooaq'\r\n\r\n def test_load_dataset(self, dataset_name):\r\n configs = self.dataset_tester.load_all_configs(dataset_name, is_local=True)[:1]\r\n> self.dataset_tester.check_load_dataset(dataset_name, configs, is_local=True, use_local_dummy_data=True)\r\n\r\ntests/test_dataset_common.py:234: \r\n_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \r\ntests/test_dataset_common.py:187: in check_load_dataset\r\n self.parent.assertTrue(len(dataset[split]) > 0)\r\nE AssertionError: False is not true\r\n```\r\nWhen I try loading dataset on local machine it works fine. Any suggestions on how can I avoid this error?"]``` This looks good, so let’s use `Dataset.map()` to add a new `comments` column to each issue in our dataset: ``` issues_with_comments_dataset = issues_dataset.map( lambda x: {"comments": get_comments(x["number"])} )``` The final step is to push our dataset to the Hub. Let’s take a look at how we can do that. ## [](#uploading-the-dataset-to-the-hugging-face-hub)Uploading the dataset to the Hugging Face Hub Now that we have our augmented dataset, it’s time to push it to the Hub so we can share it with the community! Uploading a dataset is very simple: just like models and tokenizers from 🤗 Transformers, we can use a `push_to_hub()` method to push a dataset. To do that we need an authentication token, which can be obtained by first logging into the Hugging Face Hub with the `notebook_login()` function: ``` from huggingface_hub import notebook_login notebook_login()``` This will create a widget where you can enter your username and password, and an API token will be saved in _~/.huggingface/token_. If you’re running the code in a terminal, you can log in via the CLI instead: Once we’ve done this, we can upload our dataset by running: ``` issues_with_comments_dataset.push_to_hub("github-issues")``` From here, anyone can download the dataset by simply providing `load_dataset()` with the repository ID as the `path` argument: ``` remote_dataset = load_dataset("lewtun/github-issues", split="train") remote_dataset``` ``` Dataset({ features: ['url', 'repository_url', 'labels_url', 'comments_url', 'events_url', 'html_url', 'id', 'node_id', 'number', 'title', 'user', 'labels', 'state', 'locked', 'assignee', 'assignees', 'milestone', 'comments', 'created_at', 'updated_at', 'closed_at', 'author_association', 'active_lock_reason', 'pull_request', 'body', 'performed_via_github_app', 'is_pull_request'], num_rows: 2855 })``` Cool, we’ve pushed our dataset to the Hub and it’s available for others to use! There’s just one important thing left to do: adding a _dataset card_ that explains how the corpus was created and provides other useful information for the community. 💡 You can also upload a dataset to the Hugging Face Hub directly from the terminal by using `huggingface-cli` and a bit of Git magic. See the [🤗 Datasets guide](https://huggingface.co/docs/datasets/share.html#add-a-community-dataset) for details on how to do this. ## [](#creating-a-dataset-card)Creating a dataset card Well-documented datasets are more likely to be useful to others (including your future self!), as they provide the context to enable users to decide whether the dataset is relevant to their task and to evaluate any potential biases in or risks associated with using the dataset. On the Hugging Face Hub, this information is stored in each dataset repository’s _README.md_ file. There are two main steps you should take before creating this file: 1. Use the [`datasets-tagging` application](https://huggingface.co/datasets/tagging/) to create metadata tags in YAML format. These tags are used for a variety of search features on the Hugging Face Hub and ensure your dataset can be easily found by members of the community. Since we have created a custom dataset here, you’ll need to clone the `datasets-tagging` repository and run the application locally. Here’s what the interface looks like: ![The `datasets-tagging` interface.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/datasets-tagger.png) 2. Read the [🤗 Datasets guide](https://github.com/huggingface/datasets/blob/master/templates/README_guide.md) on creating informative dataset cards and use it as a template. You can create the _README.md_ file directly on the Hub, and you can find a template dataset card in the `lewtun/github-issues` dataset repository. A screenshot of the filled-out dataset card is shown below. ![A dataset card.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/dataset-card.png) ✏️ **Try it out!** Use the `dataset-tagging` application and [🤗 Datasets guide](https://github.com/huggingface/datasets/blob/master/templates/README_guide.md) to complete the _README.md_ file for your GitHub issues dataset. That’s it! We’ve seen in this section that creating a good dataset can be quite involved, but fortunately uploading it and sharing it with the community is not. In the next section we’ll use our new dataset to create a semantic search engine with 🤗 Datasets that can match questions to the most relevant issues and comments. ✏️ **Try it out!** Go through the steps we took in this section to create a dataset of GitHub issues for your favorite open source library (pick something other than 🤗 Datasets, of course!). For bonus points, fine-tune a multilabel classifier to predict the tags present in the `labels` field.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Creating your own dataset - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter5/5"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Creating your own dataset - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;creating-your-own-dataset&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;getting-the-data&quot;,&quot;title&quot;:&quot;Getting the data&quot;},{&quot;local&quot;:&quot;cleaning-up-the-data&quot;,&quot;title&quot;:&quot;Cleaning up the data&quot;},{&quot;local&quot;:&quot;augmenting-the-dataset&quot;,&quot;title&quot;:&quot;Augmenting the dataset&quot;},{&quot;local&quot;:&quot;uploading-the-dataset-to-the-hugging-face-hub&quot;,&quot;title&quot;:&quot;Uploading the dataset to the Hugging Face Hub&quot;},{&quot;local&quot;:&quot;creating-a-dataset-card&quot;,&quot;title&quot;:&quot;Creating a dataset card&quot;}],&quot;title&quot;:&quot;Creating your own dataset&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter5/5&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Creating your own dataset&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Creating your own dataset</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/2?fw=pt">What if my dataset isn't on the Hub? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/3?fw=pt">Time to slice and dice </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/4?fw=pt">Big data? 🤗 Datasets to the rescue! </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter5/5?fw=pt">Creating your own dataset </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/6?fw=pt">Semantic search with FAISS </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/7?fw=pt">🤗 Datasets, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="creating-your-own-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#creating-your-own-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Creating your own dataset</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-5-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter5/section5.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter5/section5.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>Sometimes the dataset that you need to build an NLP application doesn’t exist, so you’ll need to create it yourself. In this section we’ll show you how to create a corpus of <a href="https://github.com/features/issues/" rel="nofollow">GitHub issues</a>, which are commonly used to track bugs or features in GitHub repositories. This corpus could be used for various purposes, including:</p> <ul><li>Exploring how long it takes to close open issues or pull requests</li> <li>Training a <em>multilabel classifier</em> that can tag issues with metadata based on the issue’s description (e.g., “bug,” “enhancement,” or “question”)</li> <li>Creating a semantic search engine to find which issues match a user’s query</li></ul> <p>Here we’ll focus on creating the corpus, and in the next section we’ll tackle the semantic search application. To keep things meta, we’ll use the GitHub issues associated with a popular open source project: 🤗 Datasets! Let’s take a look at how to get the data and explore the information contained in these issues.</p> <h2 class="relative group"><a id="getting-the-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#getting-the-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Getting the data</span></h2> <p>You can find all the issues in 🤗 Datasets by navigating to the repository’s <a href="https://github.com/huggingface/datasets/issues" rel="nofollow">Issues tab</a>. As shown in the following screenshot, at the time of writing there were 331 open issues and 668 closed ones.</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/datasets-issues.png" alt="The GitHub issues associated with 🤗 Datasets." width="80%"></div> <p>If you click on one of these issues you’ll find it contains a title, a description, and a set of labels that characterize the issue. An example is shown in the screenshot below.</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/datasets-issues-single.png" alt="A typical GitHub issue in the 🤗 Datasets repository." width="80%"></div> <p>To download all the repository’s issues, we’ll use the <a href="https://docs.github.com/en/rest" rel="nofollow">GitHub REST API</a> to poll the <a href="https://docs.github.com/en/rest/reference/issues#list-repository-issues" rel="nofollow"><code>Issues</code> endpoint</a>. This endpoint returns a list of JSON objects, with each object containing a large number of fields that include the title and description as well as metadata about the status of the issue and so on.</p> <p>A convenient way to download the issues is via the <code>requests</code> library, which is the standard way for making HTTP requests in Python. You can install the library by running:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!pip install requests</pre></div> <p>Once the library is installed, you can make GET requests to the <code>Issues</code> endpoint by invoking the <code>requests.get()</code> function. For example, you can run the following command to retrieve the first issue on the first page:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> requests url = <span class="hljs-string">"https://api.github.com/repos/huggingface/datasets/issues?page=1&amp;per_page=1"</span> response = requests.get(url)</pre></div> <p>The <code>response</code> object contains a lot of useful information about the request, including the HTTP status code:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>response.status_code</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">200</span></pre></div> <p>where a <code>200</code> status means the request was successful (you can find a list of possible HTTP status codes <a href="https://en.wikipedia.org/wiki/List_of_HTTP_status_codes" rel="nofollow">here</a>). What we are really interested in, though, is the <em>payload</em>, which can be accessed in various formats like bytes, strings, or JSON. Since we know our issues are in JSON format, let’s inspect the payload as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>response.json()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'url'</span>: <span class="hljs-string">'https://api.github.com/repos/huggingface/datasets/issues/2792'</span>, <span class="hljs-string">'repository_url'</span>: <span class="hljs-string">'https://api.github.com/repos/huggingface/datasets'</span>, <span class="hljs-string">'labels_url'</span>: <span class="hljs-string">'https://api.github.com/repos/huggingface/datasets/issues/2792/labels{/name}'</span>, <span class="hljs-string">'comments_url'</span>: <span class="hljs-string">'https://api.github.com/repos/huggingface/datasets/issues/2792/comments'</span>, <span class="hljs-string">'events_url'</span>: <span class="hljs-string">'https://api.github.com/repos/huggingface/datasets/issues/2792/events'</span>, <span class="hljs-string">'html_url'</span>: <span class="hljs-string">'https://github.com/huggingface/datasets/pull/2792'</span>, <span class="hljs-string">'id'</span>: <span class="hljs-number">968650274</span>, <span class="hljs-string">'node_id'</span>: <span class="hljs-string">'MDExOlB1bGxSZXF1ZXN0NzEwNzUyMjc0'</span>, <span class="hljs-string">'number'</span>: <span class="hljs-number">2792</span>, <span class="hljs-string">'title'</span>: <span class="hljs-string">'Update GooAQ'</span>, <span class="hljs-string">'user'</span>: {<span class="hljs-string">'login'</span>: <span class="hljs-string">'bhavitvyamalik'</span>, <span class="hljs-string">'id'</span>: <span class="hljs-number">19718818</span>, <span class="hljs-string">'node_id'</span>: <span class="hljs-string">'MDQ6VXNlcjE5NzE4ODE4'</span>, <span class="hljs-string">'avatar_url'</span>: <span class="hljs-string">'https://avatars.githubusercontent.com/u/19718818?v=4'</span>, <span class="hljs-string">'gravatar_id'</span>: <span class="hljs-string">''</span>, <span class="hljs-string">'url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik'</span>, <span class="hljs-string">'html_url'</span>: <span class="hljs-string">'https://github.com/bhavitvyamalik'</span>, <span class="hljs-string">'followers_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/followers'</span>, <span class="hljs-string">'following_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/following{/other_user}'</span>, <span class="hljs-string">'gists_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/gists{/gist_id}'</span>, <span class="hljs-string">'starred_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/starred{/owner}{/repo}'</span>, <span class="hljs-string">'subscriptions_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/subscriptions'</span>, <span class="hljs-string">'organizations_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/orgs'</span>, <span class="hljs-string">'repos_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/repos'</span>, <span class="hljs-string">'events_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/events{/privacy}'</span>, <span class="hljs-string">'received_events_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/received_events'</span>, <span class="hljs-string">'type'</span>: <span class="hljs-string">'User'</span>, <span class="hljs-string">'site_admin'</span>: <span class="hljs-literal">False</span>}, <span class="hljs-string">'labels'</span>: [], <span class="hljs-string">'state'</span>: <span class="hljs-string">'open'</span>, <span class="hljs-string">'locked'</span>: <span class="hljs-literal">False</span>, <span class="hljs-string">'assignee'</span>: <span class="hljs-literal">None</span>, <span class="hljs-string">'assignees'</span>: [], <span class="hljs-string">'milestone'</span>: <span class="hljs-literal">None</span>, <span class="hljs-string">'comments'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'created_at'</span>: <span class="hljs-string">'2021-08-12T11:40:18Z'</span>, <span class="hljs-string">'updated_at'</span>: <span class="hljs-string">'2021-08-12T12:31:17Z'</span>, <span class="hljs-string">'closed_at'</span>: <span class="hljs-literal">None</span>, <span class="hljs-string">'author_association'</span>: <span class="hljs-string">'CONTRIBUTOR'</span>, <span class="hljs-string">'active_lock_reason'</span>: <span class="hljs-literal">None</span>, <span class="hljs-string">'pull_request'</span>: {<span class="hljs-string">'url'</span>: <span class="hljs-string">'https://api.github.com/repos/huggingface/datasets/pulls/2792'</span>, <span class="hljs-string">'html_url'</span>: <span class="hljs-string">'https://github.com/huggingface/datasets/pull/2792'</span>, <span class="hljs-string">'diff_url'</span>: <span class="hljs-string">'https://github.com/huggingface/datasets/pull/2792.diff'</span>, <span class="hljs-string">'patch_url'</span>: <span class="hljs-string">'https://github.com/huggingface/datasets/pull/2792.patch'</span>}, <span class="hljs-string">'body'</span>: <span class="hljs-string">'[GooAQ](https://github.com/allenai/gooaq) dataset was recently updated after splits were added for the same. This PR contains new updated GooAQ with train/val/test splits and updated README as well.'</span>, <span class="hljs-string">'performed_via_github_app'</span>: <span class="hljs-literal">None</span>}]</pre></div> <p>Whoa, that’s a lot of information! We can see useful fields like <code>title</code>, <code>body</code>, and <code>number</code> that describe the issue, as well as information about the GitHub user who opened the issue.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Click on a few of the URLs in the JSON payload above to get a feel for what type of information each GitHub issue is linked to.</p></div> <p>As described in the GitHub <a href="https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting" rel="nofollow">documentation</a>, unauthenticated requests are limited to 60 requests per hour. Although you can increase the <code>per_page</code> query parameter to reduce the number of requests you make, you will still hit the rate limit on any repository that has more than a few thousand issues. So instead, you should follow GitHub’s <a href="https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token" rel="nofollow">instructions</a> on creating a <em>personal access token</em> so that you can boost the rate limit to 5,000 requests per hour. Once you have your token, you can include it as part of the request header:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>GITHUB_TOKEN = xxx <span class="hljs-comment"># Copy your GitHub token here</span> headers = {<span class="hljs-string">"Authorization"</span>: <span class="hljs-string">f"token <span class="hljs-subst">{GITHUB_TOKEN}</span>"</span>}</pre></div> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>⚠️ Do not share a notebook with your <code>GITHUB_TOKEN</code> pasted in it. We recommend you delete the last cell once you have executed it to avoid leaking this information accidentally. Even better, store the token in a <em>.env</em> file and use the <a href="https://github.com/theskumar/python-dotenv" rel="nofollow"><code>python-dotenv</code> library</a> to load it automatically for you as an environment variable.</p></div> <p>Now that we have our access token, let’s create a function that can download all the issues from a GitHub repository:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> time <span class="hljs-keyword">import</span> math <span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path <span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd <span class="hljs-keyword">from</span> tqdm.notebook <span class="hljs-keyword">import</span> tqdm <span class="hljs-keyword">def</span> <span class="hljs-title function_">fetch_issues</span>(<span class="hljs-params"> owner=<span class="hljs-string">"huggingface"</span>, repo=<span class="hljs-string">"datasets"</span>, num_issues=<span class="hljs-number">10_000</span>, rate_limit=<span class="hljs-number">5_000</span>, issues_path=Path(<span class="hljs-params"><span class="hljs-string">"."</span></span>), </span>): <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> issues_path.is_dir(): issues_path.mkdir(exist_ok=<span class="hljs-literal">True</span>) batch = [] all_issues = [] per_page = <span class="hljs-number">100</span> <span class="hljs-comment"># Number of issues to return per page</span> num_pages = math.ceil(num_issues / per_page) base_url = <span class="hljs-string">"https://api.github.com/repos"</span> <span class="hljs-keyword">for</span> page <span class="hljs-keyword">in</span> tqdm(<span class="hljs-built_in">range</span>(num_pages)): <span class="hljs-comment"># Query with state=all to get both open and closed issues</span> query = <span class="hljs-string">f"issues?page=<span class="hljs-subst">{page}</span>&amp;per_page=<span class="hljs-subst">{per_page}</span>&amp;state=all"</span> issues = requests.get(<span class="hljs-string">f"<span class="hljs-subst">{base_url}</span>/<span class="hljs-subst">{owner}</span>/<span class="hljs-subst">{repo}</span>/<span class="hljs-subst">{query}</span>"</span>, headers=headers) batch.extend(issues.json()) <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(batch) &gt; rate_limit <span class="hljs-keyword">and</span> <span class="hljs-built_in">len</span>(all_issues) &lt; num_issues: all_issues.extend(batch) batch = [] <span class="hljs-comment"># Flush batch for next time period</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Reached GitHub rate limit. Sleeping for one hour ..."</span>) time.sleep(<span class="hljs-number">60</span> * <span class="hljs-number">60</span> + <span class="hljs-number">1</span>) all_issues.extend(batch) df = pd.DataFrame.from_records(all_issues) df.to_json(<span class="hljs-string">f"<span class="hljs-subst">{issues_path}</span>/<span class="hljs-subst">{repo}</span>-issues.jsonl"</span>, orient=<span class="hljs-string">"records"</span>, lines=<span class="hljs-literal">True</span>) <span class="hljs-built_in">print</span>( <span class="hljs-string">f"Downloaded all the issues for <span class="hljs-subst">{repo}</span>! Dataset stored at <span class="hljs-subst">{issues_path}</span>/<span class="hljs-subst">{repo}</span>-issues.jsonl"</span> )</pre></div> <p>Now when we call <code>fetch_issues()</code> it will download all the issues in batches to avoid exceeding GitHub’s limit on the number of requests per hour; the result will be stored in a <em>repository_name-issues.jsonl</em> file, where each line is a JSON object the represents an issue. Let’s use this function to grab all the issues from 🤗 Datasets:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># Depending on your internet connection, this can take several minutes to run...</span> fetch_issues()</pre></div> <p>Once the issues are downloaded we can load them locally using our newfound skills from <a href="/course/chapter5/2">section 2</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>issues_dataset = load_dataset(<span class="hljs-string">"json"</span>, data_files=<span class="hljs-string">"datasets-issues.jsonl"</span>, split=<span class="hljs-string">"train"</span>) issues_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Dataset({ features: [<span class="hljs-string">'url'</span>, <span class="hljs-string">'repository_url'</span>, <span class="hljs-string">'labels_url'</span>, <span class="hljs-string">'comments_url'</span>, <span class="hljs-string">'events_url'</span>, <span class="hljs-string">'html_url'</span>, <span class="hljs-string">'id'</span>, <span class="hljs-string">'node_id'</span>, <span class="hljs-string">'number'</span>, <span class="hljs-string">'title'</span>, <span class="hljs-string">'user'</span>, <span class="hljs-string">'labels'</span>, <span class="hljs-string">'state'</span>, <span class="hljs-string">'locked'</span>, <span class="hljs-string">'assignee'</span>, <span class="hljs-string">'assignees'</span>, <span class="hljs-string">'milestone'</span>, <span class="hljs-string">'comments'</span>, <span class="hljs-string">'created_at'</span>, <span class="hljs-string">'updated_at'</span>, <span class="hljs-string">'closed_at'</span>, <span class="hljs-string">'author_association'</span>, <span class="hljs-string">'active_lock_reason'</span>, <span class="hljs-string">'pull_request'</span>, <span class="hljs-string">'body'</span>, <span class="hljs-string">'timeline_url'</span>, <span class="hljs-string">'performed_via_github_app'</span>], num_rows: <span class="hljs-number">3019</span> })</pre></div> <p>Great, we’ve created our first dataset from scratch! But why are there several thousand issues when the <a href="https://github.com/huggingface/datasets/issues" rel="nofollow">Issues tab</a> of the 🤗 Datasets repository only shows around 1,000 issues in total 🤔? As described in the GitHub <a href="https://docs.github.com/en/rest/reference/issues#list-issues-assigned-to-the-authenticated-user" rel="nofollow">documentation</a>, that’s because we’ve downloaded all the pull requests as well:</p> <blockquote><p>GitHub’s REST API v3 considers every pull request an issue, but not every issue is a pull request. For this reason, “Issues” endpoints may return both issues and pull requests in the response. You can identify pull requests by the <code>pull_request</code> key. Be aware that the <code>id</code> of a pull request returned from “Issues” endpoints will be an issue id.</p></blockquote> <p>Since the contents of issues and pull requests are quite different, let’s do some minor preprocessing to enable us to distinguish between them.</p> <h2 class="relative group"><a id="cleaning-up-the-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#cleaning-up-the-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Cleaning up the data</span></h2> <p>The above snippet from GitHub’s documentation tells us that the <code>pull_request</code> column can be used to differentiate between issues and pull requests. Let’s look at a random sample to see what the difference is. As we did in <a href="/course/chapter5/3">section 3</a>, we’ll chain <code>Dataset.shuffle()</code> and <code>Dataset.select()</code> to create a random sample and then zip the <code>html_url</code> and <code>pull_request</code> columns so we can compare the various URLs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sample = issues_dataset.shuffle(seed=<span class="hljs-number">666</span>).select(<span class="hljs-built_in">range</span>(<span class="hljs-number">3</span>)) <span class="hljs-comment"># Print out the URL and pull request entries</span> <span class="hljs-keyword">for</span> url, pr <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(sample[<span class="hljs-string">"html_url"</span>], sample[<span class="hljs-string">"pull_request"</span>]): <span class="hljs-built_in">print</span>(<span class="hljs-string">f"&gt;&gt; URL: <span class="hljs-subst">{url}</span>"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"&gt;&gt; Pull request: <span class="hljs-subst">{pr}</span>\n"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>&gt;&gt; URL: https://github.com/huggingface/datasets/pull/<span class="hljs-number">850</span> &gt;&gt; Pull request: {<span class="hljs-string">'url'</span>: <span class="hljs-string">'https://api.github.com/repos/huggingface/datasets/pulls/850'</span>, <span class="hljs-string">'html_url'</span>: <span class="hljs-string">'https://github.com/huggingface/datasets/pull/850'</span>, <span class="hljs-string">'diff_url'</span>: <span class="hljs-string">'https://github.com/huggingface/datasets/pull/850.diff'</span>, <span class="hljs-string">'patch_url'</span>: <span class="hljs-string">'https://github.com/huggingface/datasets/pull/850.patch'</span>} &gt;&gt; URL: https://github.com/huggingface/datasets/issues/<span class="hljs-number">2773</span> &gt;&gt; Pull request: <span class="hljs-literal">None</span> &gt;&gt; URL: https://github.com/huggingface/datasets/pull/<span class="hljs-number">783</span> &gt;&gt; Pull request: {<span class="hljs-string">'url'</span>: <span class="hljs-string">'https://api.github.com/repos/huggingface/datasets/pulls/783'</span>, <span class="hljs-string">'html_url'</span>: <span class="hljs-string">'https://github.com/huggingface/datasets/pull/783'</span>, <span class="hljs-string">'diff_url'</span>: <span class="hljs-string">'https://github.com/huggingface/datasets/pull/783.diff'</span>, <span class="hljs-string">'patch_url'</span>: <span class="hljs-string">'https://github.com/huggingface/datasets/pull/783.patch'</span>}</pre></div> <p>Here we can see that each pull request is associated with various URLs, while ordinary issues have a <code>None</code> entry. We can use this distinction to create a new <code>is_pull_request</code> column that checks whether the <code>pull_request</code> field is <code>None</code> or not:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>issues_dataset = issues_dataset.<span class="hljs-built_in">map</span>( <span class="hljs-keyword">lambda</span> x: {<span class="hljs-string">"is_pull_request"</span>: <span class="hljs-literal">False</span> <span class="hljs-keyword">if</span> x[<span class="hljs-string">"pull_request"</span>] <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">else</span> <span class="hljs-literal">True</span>} )</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Calculate the average time it takes to close issues in 🤗 Datasets. You may find the <code>Dataset.filter()</code> function useful to filter out the pull requests and open issues, and you can use the <code>Dataset.set_format()</code> function to convert the dataset to a <code>DataFrame</code> so you can easily manipulate the <code>created_at</code> and <code>closed_at</code> timestamps. For bonus points, calculate the average time it takes to close pull requests.</p></div> <p>Although we could proceed to further clean up the dataset by dropping or renaming some columns, it is generally a good practice to keep the dataset as “raw” as possible at this stage so that it can be easily used in multiple applications.</p> <p>Before we push our dataset to the Hugging Face Hub, let’s deal with one thing that’s missing from it: the comments associated with each issue and pull request. We’ll add them next with — you guessed it — the GitHub REST API!</p> <h2 class="relative group"><a id="augmenting-the-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#augmenting-the-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Augmenting the dataset</span></h2> <p>As shown in the following screenshot, the comments associated with an issue or pull request provide a rich source of information, especially if we’re interested in building a search engine to answer user queries about the library.</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/datasets-issues-comment.png" alt="Comments associated with an issue about 🤗 Datasets." width="80%"></div> <p>The GitHub REST API provides a <a href="https://docs.github.com/en/rest/reference/issues#list-issue-comments" rel="nofollow"><code>Comments</code> endpoint</a> that returns all the comments associated with an issue number. Let’s test the endpoint to see what it returns:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>issue_number = <span class="hljs-number">2792</span> url = <span class="hljs-string">f"https://api.github.com/repos/huggingface/datasets/issues/<span class="hljs-subst">{issue_number}</span>/comments"</span> response = requests.get(url, headers=headers) response.json()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'url'</span>: <span class="hljs-string">'https://api.github.com/repos/huggingface/datasets/issues/comments/897594128'</span>, <span class="hljs-string">'html_url'</span>: <span class="hljs-string">'https://github.com/huggingface/datasets/pull/2792#issuecomment-897594128'</span>, <span class="hljs-string">'issue_url'</span>: <span class="hljs-string">'https://api.github.com/repos/huggingface/datasets/issues/2792'</span>, <span class="hljs-string">'id'</span>: <span class="hljs-number">897594128</span>, <span class="hljs-string">'node_id'</span>: <span class="hljs-string">'IC_kwDODunzps41gDMQ'</span>, <span class="hljs-string">'user'</span>: {<span class="hljs-string">'login'</span>: <span class="hljs-string">'bhavitvyamalik'</span>, <span class="hljs-string">'id'</span>: <span class="hljs-number">19718818</span>, <span class="hljs-string">'node_id'</span>: <span class="hljs-string">'MDQ6VXNlcjE5NzE4ODE4'</span>, <span class="hljs-string">'avatar_url'</span>: <span class="hljs-string">'https://avatars.githubusercontent.com/u/19718818?v=4'</span>, <span class="hljs-string">'gravatar_id'</span>: <span class="hljs-string">''</span>, <span class="hljs-string">'url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik'</span>, <span class="hljs-string">'html_url'</span>: <span class="hljs-string">'https://github.com/bhavitvyamalik'</span>, <span class="hljs-string">'followers_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/followers'</span>, <span class="hljs-string">'following_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/following{/other_user}'</span>, <span class="hljs-string">'gists_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/gists{/gist_id}'</span>, <span class="hljs-string">'starred_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/starred{/owner}{/repo}'</span>, <span class="hljs-string">'subscriptions_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/subscriptions'</span>, <span class="hljs-string">'organizations_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/orgs'</span>, <span class="hljs-string">'repos_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/repos'</span>, <span class="hljs-string">'events_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/events{/privacy}'</span>, <span class="hljs-string">'received_events_url'</span>: <span class="hljs-string">'https://api.github.com/users/bhavitvyamalik/received_events'</span>, <span class="hljs-string">'type'</span>: <span class="hljs-string">'User'</span>, <span class="hljs-string">'site_admin'</span>: <span class="hljs-literal">False</span>}, <span class="hljs-string">'created_at'</span>: <span class="hljs-string">'2021-08-12T12:21:52Z'</span>, <span class="hljs-string">'updated_at'</span>: <span class="hljs-string">'2021-08-12T12:31:17Z'</span>, <span class="hljs-string">'author_association'</span>: <span class="hljs-string">'CONTRIBUTOR'</span>, <span class="hljs-string">'body'</span>: <span class="hljs-string">"@albertvillanova my tests are failing here:\r\n```\r\ndataset_name = 'gooaq'\r\n\r\n def test_load_dataset(self, dataset_name):\r\n configs = self.dataset_tester.load_all_configs(dataset_name, is_local=True)[:1]\r\n&gt; self.dataset_tester.check_load_dataset(dataset_name, configs, is_local=True, use_local_dummy_data=True)\r\n\r\ntests/test_dataset_common.py:234: \r\n_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \r\ntests/test_dataset_common.py:187: in check_load_dataset\r\n self.parent.assertTrue(len(dataset[split]) &gt; 0)\r\nE AssertionError: False is not true\r\n```\r\nWhen I try loading dataset on local machine it works fine. Any suggestions on how can I avoid this error?"</span>, <span class="hljs-string">'performed_via_github_app'</span>: <span class="hljs-literal">None</span>}]</pre></div> <p>We can see that the comment is stored in the <code>body</code> field, so let’s write a simple function that returns all the comments associated with an issue by picking out the <code>body</code> contents for each element in <code>response.json()</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">get_comments</span>(<span class="hljs-params">issue_number</span>): url = <span class="hljs-string">f"https://api.github.com/repos/huggingface/datasets/issues/<span class="hljs-subst">{issue_number}</span>/comments"</span> response = requests.get(url, headers=headers) <span class="hljs-keyword">return</span> [r[<span class="hljs-string">"body"</span>] <span class="hljs-keyword">for</span> r <span class="hljs-keyword">in</span> response.json()] <span class="hljs-comment"># Test our function works as expected</span> get_comments(<span class="hljs-number">2792</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">"@albertvillanova my tests are failing here:\r\n```\r\ndataset_name = 'gooaq'\r\n\r\n def test_load_dataset(self, dataset_name):\r\n configs = self.dataset_tester.load_all_configs(dataset_name, is_local=True)[:1]\r\n&gt; self.dataset_tester.check_load_dataset(dataset_name, configs, is_local=True, use_local_dummy_data=True)\r\n\r\ntests/test_dataset_common.py:234: \r\n_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \r\ntests/test_dataset_common.py:187: in check_load_dataset\r\n self.parent.assertTrue(len(dataset[split]) &gt; 0)\r\nE AssertionError: False is not true\r\n```\r\nWhen I try loading dataset on local machine it works fine. Any suggestions on how can I avoid this error?"</span>]</pre></div> <p>This looks good, so let’s use <code>Dataset.map()</code> to add a new <code>comments</code> column to each issue in our dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># Depending on your internet connection, this can take a few minutes...</span> issues_with_comments_dataset = issues_dataset.<span class="hljs-built_in">map</span>( <span class="hljs-keyword">lambda</span> x: {<span class="hljs-string">"comments"</span>: get_comments(x[<span class="hljs-string">"number"</span>])} )</pre></div> <p>The final step is to push our dataset to the Hub. Let’s take a look at how we can do that.</p> <h2 class="relative group"><a id="uploading-the-dataset-to-the-hugging-face-hub" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#uploading-the-dataset-to-the-hugging-face-hub"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Uploading the dataset to the Hugging Face Hub</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/HaN6qCr_Afc" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Now that we have our augmented dataset, it’s time to push it to the Hub so we can share it with the community! Uploading a dataset is very simple: just like models and tokenizers from 🤗 Transformers, we can use a <code>push_to_hub()</code> method to push a dataset. To do that we need an authentication token, which can be obtained by first logging into the Hugging Face Hub with the <code>notebook_login()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login notebook_login()</pre></div> <p>This will create a widget where you can enter your username and password, and an API token will be saved in <em>~/.huggingface/token</em>. If you’re running the code in a terminal, you can log in via the CLI instead:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-cli login</pre></div> <p>Once we’ve done this, we can upload our dataset by running:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>issues_with_comments_dataset.push_to_hub(<span class="hljs-string">"github-issues"</span>)</pre></div> <p>From here, anyone can download the dataset by simply providing <code>load_dataset()</code> with the repository ID as the <code>path</code> argument:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>remote_dataset = load_dataset(<span class="hljs-string">"lewtun/github-issues"</span>, split=<span class="hljs-string">"train"</span>) remote_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Dataset({ features: [<span class="hljs-string">'url'</span>, <span class="hljs-string">'repository_url'</span>, <span class="hljs-string">'labels_url'</span>, <span class="hljs-string">'comments_url'</span>, <span class="hljs-string">'events_url'</span>, <span class="hljs-string">'html_url'</span>, <span class="hljs-string">'id'</span>, <span class="hljs-string">'node_id'</span>, <span class="hljs-string">'number'</span>, <span class="hljs-string">'title'</span>, <span class="hljs-string">'user'</span>, <span class="hljs-string">'labels'</span>, <span class="hljs-string">'state'</span>, <span class="hljs-string">'locked'</span>, <span class="hljs-string">'assignee'</span>, <span class="hljs-string">'assignees'</span>, <span class="hljs-string">'milestone'</span>, <span class="hljs-string">'comments'</span>, <span class="hljs-string">'created_at'</span>, <span class="hljs-string">'updated_at'</span>, <span class="hljs-string">'closed_at'</span>, <span class="hljs-string">'author_association'</span>, <span class="hljs-string">'active_lock_reason'</span>, <span class="hljs-string">'pull_request'</span>, <span class="hljs-string">'body'</span>, <span class="hljs-string">'performed_via_github_app'</span>, <span class="hljs-string">'is_pull_request'</span>], num_rows: <span class="hljs-number">2855</span> })</pre></div> <p>Cool, we’ve pushed our dataset to the Hub and it’s available for others to use! There’s just one important thing left to do: adding a <em>dataset card</em> that explains how the corpus was created and provides other useful information for the community.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 You can also upload a dataset to the Hugging Face Hub directly from the terminal by using <code>huggingface-cli</code> and a bit of Git magic. See the <a href="https://huggingface.co/docs/datasets/share.html#add-a-community-dataset" rel="nofollow">🤗 Datasets guide</a> for details on how to do this.</p></div> <h2 class="relative group"><a id="creating-a-dataset-card" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#creating-a-dataset-card"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Creating a dataset card</span></h2> <p>Well-documented datasets are more likely to be useful to others (including your future self!), as they provide the context to enable users to decide whether the dataset is relevant to their task and to evaluate any potential biases in or risks associated with using the dataset.</p> <p>On the Hugging Face Hub, this information is stored in each dataset repository’s <em>README.md</em> file. There are two main steps you should take before creating this file:</p> <ol><li>Use the <a href="https://huggingface.co/datasets/tagging/" rel="nofollow"><code>datasets-tagging</code> application</a> to create metadata tags in YAML format. These tags are used for a variety of search features on the Hugging Face Hub and ensure your dataset can be easily found by members of the community. Since we have created a custom dataset here, you’ll need to clone the <code>datasets-tagging</code> repository and run the application locally. Here’s what the interface looks like:</li></ol> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/datasets-tagger.png" alt="The `datasets-tagging` interface." width="80%"></div> <ol start="2"><li>Read the <a href="https://github.com/huggingface/datasets/blob/master/templates/README_guide.md" rel="nofollow">🤗 Datasets guide</a> on creating informative dataset cards and use it as a template.</li></ol> <p>You can create the <em>README.md</em> file directly on the Hub, and you can find a template dataset card in the <code>lewtun/github-issues</code> dataset repository. A screenshot of the filled-out dataset card is shown below.</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/dataset-card.png" alt="A dataset card." width="80%"></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Use the <code>dataset-tagging</code> application and <a href="https://github.com/huggingface/datasets/blob/master/templates/README_guide.md" rel="nofollow">🤗 Datasets guide</a> to complete the <em>README.md</em> file for your GitHub issues dataset.</p></div> <p>That’s it! We’ve seen in this section that creating a good dataset can be quite involved, but fortunately uploading it and sharing it with the community is not. In the next section we’ll use our new dataset to create a semantic search engine with 🤗 Datasets that can match questions to the most relevant issues and comments.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Go through the steps we took in this section to create a dataset of GitHub issues for your favorite open source library (pick something other than 🤗 Datasets, of course!). For bonus points, fine-tune a multilabel classifier to predict the tags present in the <code>labels</code> field.</p></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter5/4?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Big data? 🤗 Datasets to the rescue!</a> <a href="/learn/nlp-course/chapter5/6?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Semantic search with FAISS<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;creating-your-own-dataset&quot;,&quot;url&quot;:&quot;#creating-your-own-dataset&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Getting the data&quot;,&quot;id&quot;:&quot;getting-the-data&quot;,&quot;url&quot;:&quot;#getting-the-data&quot;},{&quot;title&quot;:&quot;Cleaning up the data&quot;,&quot;id&quot;:&quot;cleaning-up-the-data&quot;,&quot;url&quot;:&quot;#cleaning-up-the-data&quot;},{&quot;title&quot;:&quot;Augmenting the dataset&quot;,&quot;id&quot;:&quot;augmenting-the-dataset&quot;,&quot;url&quot;:&quot;#augmenting-the-dataset&quot;},{&quot;title&quot;:&quot;Uploading the dataset to the Hugging Face Hub&quot;,&quot;id&quot;:&quot;uploading-the-dataset-to-the-hugging-face-hub&quot;,&quot;url&quot;:&quot;#uploading-the-dataset-to-the-hugging-face-hub&quot;},{&quot;title&quot;:&quot;Creating a dataset card&quot;,&quot;id&quot;:&quot;creating-a-dataset-card&quot;,&quot;url&quot;:&quot;#creating-a-dataset-card&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#creating-your-own-dataset" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-creating-your-own-dataset"><wbr>Creating your own dataset</a> <a href="#getting-the-data" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-getting-the-data"><wbr>Getting the data</a> <a href="#cleaning-up-the-data" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-cleaning-up-the-data"><wbr>Cleaning up the data</a> <a href="#augmenting-the-dataset" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-augmenting-the-dataset"><wbr>Augmenting the dataset</a> <a href="#uploading-the-dataset-to-the-hugging-face-hub" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-uploading-the-dataset-to-the-hugging-face-hub"><wbr>Uploading the dataset to the <wbr>Hugging <wbr>Face <wbr>Hub</a> <a href="#creating-a-dataset-card" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-creating-a-dataset-card"><wbr>Creating a dataset card</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter5/5" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter5/5"); } </script> <iframe name="__privateStripeMetricsController3510" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter5%2F5%3Ffw%3Dpt&amp;title=Creating%20your%20own%20dataset%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:19.654Z
🤗 Datasets, check! - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter5/7?fw=pt
## [](#datasets-check)🤗 Datasets, check! [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-5-questions) Well, that was quite a tour through the 🤗 Datasets library — congratulations on making it this far! With the knowledge that you’ve gained from this chapter, you should be able to: - Load datasets from anywhere, be it the Hugging Face Hub, your laptop, or a remote server at your company. - Wrangle your data using a mix of the `Dataset.map()` and `Dataset.filter()` functions. - Quickly switch between data formats like Pandas and NumPy using `Dataset.set_format()`. - Create your very own dataset and push it to the Hugging Face Hub. - Embed your documents using a Transformer model and build a semantic search engine using FAISS. In [Chapter 7](/course/chapter7), we’ll put all of this to good use as we take a deep dive into the core NLP tasks that Transformer models are great for. Before jumping ahead, though, put your knowledge of 🤗 Datasets to the test with a quick quiz!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="🤗 Datasets, check! - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter5/7"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>🤗 Datasets, check! - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;datasets-check&quot;,&quot;title&quot;:&quot;🤗 Datasets, check!&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter5/7&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;🤗 Datasets, check!&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">🤗 Datasets, check!</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/2?fw=pt">What if my dataset isn't on the Hub? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/3?fw=pt">Time to slice and dice </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/4?fw=pt">Big data? 🤗 Datasets to the rescue! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/5?fw=pt">Creating your own dataset </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/6?fw=pt">Semantic search with FAISS </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter5/7?fw=pt">🤗 Datasets, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="datasets-check" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#datasets-check"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>🤗 Datasets, check!</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-5-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>Well, that was quite a tour through the 🤗 Datasets library — congratulations on making it this far! With the knowledge that you’ve gained from this chapter, you should be able to:</p> <ul><li>Load datasets from anywhere, be it the Hugging Face Hub, your laptop, or a remote server at your company.</li> <li>Wrangle your data using a mix of the <code>Dataset.map()</code> and <code>Dataset.filter()</code> functions.</li> <li>Quickly switch between data formats like Pandas and NumPy using <code>Dataset.set_format()</code>.</li> <li>Create your very own dataset and push it to the Hugging Face Hub.</li> <li>Embed your documents using a Transformer model and build a semantic search engine using FAISS.</li></ul> <p>In <a href="/course/chapter7">Chapter 7</a>, we’ll put all of this to good use as we take a deep dive into the core NLP tasks that Transformer models are great for. Before jumping ahead, though, put your knowledge of 🤗 Datasets to the test with a quick quiz!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter5/6?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Semantic search with FAISS</a> <a href="/learn/nlp-course/chapter5/8?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">End-of-chapter quiz<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;datasets-check&quot;,&quot;url&quot;:&quot;#datasets-check&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#datasets-check" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-datasets-check">🤗 <wbr>Datasets, check!</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter5/7" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter5/7"); } </script> <iframe name="__privateStripeMetricsController7290" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter5%2F7%3Ffw%3Dpt&amp;title=%F0%9F%A4%97%20Datasets%2C%20check!%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:20.100Z
Semantic search with FAISS - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter5/6?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#semantic-search-with-faiss)Semantic search with FAISS [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-5-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter5/section6_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter5/section6_pt.ipynb) In [section 5](/course/chapter5/5), we created a dataset of GitHub issues and comments from the 🤗 Datasets repository. In this section we’ll use this information to build a search engine that can help us find answers to our most pressing questions about the library! ## [](#using-embeddings-for-semantic-search)Using embeddings for semantic search As we saw in [Chapter 1](/course/chapter1), Transformer-based language models represent each token in a span of text as an _embedding vector_. It turns out that one can “pool” the individual embeddings to create a vector representation for whole sentences, paragraphs, or (in some cases) documents. These embeddings can then be used to find similar documents in the corpus by computing the dot-product similarity (or some other similarity metric) between each embedding and returning the documents with the greatest overlap. In this section we’ll use embeddings to develop a semantic search engine. These search engines offer several advantages over conventional approaches that are based on matching keywords in a query with the documents. ![Semantic search.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/semantic-search.svg) ![Semantic search.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/semantic-search-dark.svg) ## [](#loading-and-preparing-the-dataset)Loading and preparing the dataset The first thing we need to do is download our dataset of GitHub issues, so let’s use `load_dataset()` function as usual: ``` from datasets import load_dataset issues_dataset = load_dataset("lewtun/github-issues", split="train") issues_dataset``` ``` Dataset({ features: ['url', 'repository_url', 'labels_url', 'comments_url', 'events_url', 'html_url', 'id', 'node_id', 'number', 'title', 'user', 'labels', 'state', 'locked', 'assignee', 'assignees', 'milestone', 'comments', 'created_at', 'updated_at', 'closed_at', 'author_association', 'active_lock_reason', 'pull_request', 'body', 'performed_via_github_app', 'is_pull_request'], num_rows: 2855 })``` Here we’ve specified the default `train` split in `load_dataset()`, so it returns a `Dataset` instead of a `DatasetDict`. The first order of business is to filter out the pull requests, as these tend to be rarely used for answering user queries and will introduce noise in our search engine. As should be familiar by now, we can use the `Dataset.filter()` function to exclude these rows in our dataset. While we’re at it, let’s also filter out rows with no comments, since these provide no answers to user queries: ``` issues_dataset = issues_dataset.filter( lambda x: (x["is_pull_request"] == False and len(x["comments"]) > 0) ) issues_dataset``` ``` Dataset({ features: ['url', 'repository_url', 'labels_url', 'comments_url', 'events_url', 'html_url', 'id', 'node_id', 'number', 'title', 'user', 'labels', 'state', 'locked', 'assignee', 'assignees', 'milestone', 'comments', 'created_at', 'updated_at', 'closed_at', 'author_association', 'active_lock_reason', 'pull_request', 'body', 'performed_via_github_app', 'is_pull_request'], num_rows: 771 })``` We can see that there are a lot of columns in our dataset, most of which we don’t need to build our search engine. From a search perspective, the most informative columns are `title`, `body`, and `comments`, while `html_url` provides us with a link back to the source issue. Let’s use the `Dataset.remove_columns()` function to drop the rest: ``` columns = issues_dataset.column_names columns_to_keep = ["title", "body", "html_url", "comments"] columns_to_remove = set(columns_to_keep).symmetric_difference(columns) issues_dataset = issues_dataset.remove_columns(columns_to_remove) issues_dataset``` ``` Dataset({ features: ['html_url', 'title', 'comments', 'body'], num_rows: 771 })``` To create our embeddings we’ll augment each comment with the issue’s title and body, since these fields often include useful contextual information. Because our `comments` column is currently a list of comments for each issue, we need to “explode” the column so that each row consists of an `(html_url, title, body, comment)` tuple. In Pandas we can do this with the [`DataFrame.explode()` function](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.explode.html), which creates a new row for each element in a list-like column, while replicating all the other column values. To see this in action, let’s first switch to the Pandas `DataFrame` format: ``` issues_dataset.set_format("pandas") df = issues_dataset[:]``` If we inspect the first row in this `DataFrame` we can see there are four comments associated with this issue: ``` df["comments"][0].tolist()``` ``` ['the bug code locate in :\r\n if data_args.task_name is not None:\r\n # Downloading and loading a dataset from the hub.\r\n datasets = load_dataset("glue", data_args.task_name, cache_dir=model_args.cache_dir)', 'Hi @jinec,\r\n\r\nFrom time to time we get this kind of `ConnectionError` coming from the github.com website: https://raw.githubusercontent.com\r\n\r\nNormally, it should work if you wait a little and then retry.\r\n\r\nCould you please confirm if the problem persists?', 'cannot connect,even by Web browser,please check that there is some problems。', 'I can access https://raw.githubusercontent.com/huggingface/datasets/1.7.0/datasets/glue/glue.py without problem...']``` When we explode `df`, we expect to get one row for each of these comments. Let’s check if that’s the case: ``` comments_df = df.explode("comments", ignore_index=True) comments_df.head(4)``` | | html\_url | title | comments | body | | --- | --- | --- | --- | --- | | 0 | https://github.com/huggingface/datasets/issues/2787 | ConnectionError: Couldn't reach https://raw.githubusercontent.com | the bug code locate in :\\r\\n if data\_args.task\_name is not None... | Hello,\\r\\nI am trying to run run\_glue.py and it gives me this error... | | 1 | https://github.com/huggingface/datasets/issues/2787 | ConnectionError: Couldn't reach https://raw.githubusercontent.com | Hi @jinec,\\r\\n\\r\\nFrom time to time we get this kind of \`ConnectionError\` coming from the github.com website: https://raw.githubusercontent.com... | Hello,\\r\\nI am trying to run run\_glue.py and it gives me this error... | | 2 | https://github.com/huggingface/datasets/issues/2787 | ConnectionError: Couldn't reach https://raw.githubusercontent.com | cannot connect,even by Web browser,please check that there is some problems。 | Hello,\\r\\nI am trying to run run\_glue.py and it gives me this error... | | 3 | https://github.com/huggingface/datasets/issues/2787 | ConnectionError: Couldn't reach https://raw.githubusercontent.com | I can access https://raw.githubusercontent.com/huggingface/datasets/1.7.0/datasets/glue/glue.py without problem... | Hello,\\r\\nI am trying to run run\_glue.py and it gives me this error... | Great, we can see the rows have been replicated, with the `comments` column containing the individual comments! Now that we’re finished with Pandas, we can quickly switch back to a `Dataset` by loading the `DataFrame` in memory: ``` from datasets import Dataset comments_dataset = Dataset.from_pandas(comments_df) comments_dataset``` ``` Dataset({ features: ['html_url', 'title', 'comments', 'body'], num_rows: 2842 })``` Okay, this has given us a few thousand comments to work with! ✏️ **Try it out!** See if you can use `Dataset.map()` to explode the `comments` column of `issues_dataset` _without_ resorting to the use of Pandas. This is a little tricky; you might find the [“Batch mapping”](https://huggingface.co/docs/datasets/v1.12.1/about_map_batch.html?batch-mapping#batch-mapping) section of the 🤗 Datasets documentation useful for this task. Now that we have one comment per row, let’s create a new `comments_length` column that contains the number of words per comment: ``` comments_dataset = comments_dataset.map( lambda x: {"comment_length": len(x["comments"].split())} )``` We can use this new column to filter out short comments, which typically include things like “cc @lewtun” or “Thanks!” that are not relevant for our search engine. There’s no precise number to select for the filter, but around 15 words seems like a good start: ``` comments_dataset = comments_dataset.filter(lambda x: x["comment_length"] > 15) comments_dataset``` ``` Dataset({ features: ['html_url', 'title', 'comments', 'body', 'comment_length'], num_rows: 2098 })``` Having cleaned up our dataset a bit, let’s concatenate the issue title, description, and comments together in a new `text` column. As usual, we’ll write a simple function that we can pass to `Dataset.map()`: ``` def concatenate_text(examples): return { "text": examples["title"] + " \n " + examples["body"] + " \n " + examples["comments"] } comments_dataset = comments_dataset.map(concatenate_text)``` We’re finally ready to create some embeddings! Let’s take a look. ## [](#creating-text-embeddings)Creating text embeddings We saw in [Chapter 2](/course/chapter2) that we can obtain token embeddings by using the `AutoModel` class. All we need to do is pick a suitable checkpoint to load the model from. Fortunately, there’s a library called `sentence-transformers` that is dedicated to creating embeddings. As described in the library’s [documentation](https://www.sbert.net/examples/applications/semantic-search/README.html#symmetric-vs-asymmetric-semantic-search), our use case is an example of _asymmetric semantic search_ because we have a short query whose answer we’d like to find in a longer document, like a an issue comment. The handy [model overview table](https://www.sbert.net/docs/pretrained_models.html#model-overview) in the documentation indicates that the `multi-qa-mpnet-base-dot-v1` checkpoint has the best performance for semantic search, so we’ll use that for our application. We’ll also load the tokenizer using the same checkpoint: ``` from transformers import AutoTokenizer, AutoModel model_ckpt = "sentence-transformers/multi-qa-mpnet-base-dot-v1" tokenizer = AutoTokenizer.from_pretrained(model_ckpt) model = AutoModel.from_pretrained(model_ckpt)``` To speed up the embedding process, it helps to place the model and inputs on a GPU device, so let’s do that now: ``` import torch device = torch.device("cuda") model.to(device)``` As we mentioned earlier, we’d like to represent each entry in our GitHub issues corpus as a single vector, so we need to “pool” or average our token embeddings in some way. One popular approach is to perform _CLS pooling_ on our model’s outputs, where we simply collect the last hidden state for the special `[CLS]` token. The following function does the trick for us: ``` def cls_pooling(model_output): return model_output.last_hidden_state[:, 0]``` Next, we’ll create a helper function that will tokenize a list of documents, place the tensors on the GPU, feed them to the model, and finally apply CLS pooling to the outputs: ``` def get_embeddings(text_list): encoded_input = tokenizer( text_list, padding=True, truncation=True, return_tensors="pt" ) encoded_input = {k: v.to(device) for k, v in encoded_input.items()} model_output = model(**encoded_input) return cls_pooling(model_output)``` We can test the function works by feeding it the first text entry in our corpus and inspecting the output shape: ``` embedding = get_embeddings(comments_dataset["text"][0]) embedding.shape``` Great, we’ve converted the first entry in our corpus into a 768-dimensional vector! We can use `Dataset.map()` to apply our `get_embeddings()` function to each row in our corpus, so let’s create a new `embeddings` column as follows: ``` embeddings_dataset = comments_dataset.map( lambda x: {"embeddings": get_embeddings(x["text"]).detach().cpu().numpy()[0]} )``` Notice that we’ve converted the embeddings to NumPy arrays — that’s because 🤗 Datasets requires this format when we try to index them with FAISS, which we’ll do next. ## [](#using-faiss-for-efficient-similarity-search)Using FAISS for efficient similarity search Now that we have a dataset of embeddings, we need some way to search over them. To do this, we’ll use a special data structure in 🤗 Datasets called a _FAISS index_. [FAISS](https://faiss.ai/) (short for Facebook AI Similarity Search) is a library that provides efficient algorithms to quickly search and cluster embedding vectors. The basic idea behind FAISS is to create a special data structure called an _index_ that allows one to find which embeddings are similar to an input embedding. Creating a FAISS index in 🤗 Datasets is simple — we use the `Dataset.add_faiss_index()` function and specify which column of our dataset we’d like to index: ``` embeddings_dataset.add_faiss_index(column="embeddings")``` We can now perform queries on this index by doing a nearest neighbor lookup with the `Dataset.get_nearest_examples()` function. Let’s test this out by first embedding a question as follows: ``` question = "How can I load a dataset offline?" question_embedding = get_embeddings([question]).cpu().detach().numpy() question_embedding.shape``` Just like with the documents, we now have a 768-dimensional vector representing the query, which we can compare against the whole corpus to find the most similar embeddings: ``` scores, samples = embeddings_dataset.get_nearest_examples( "embeddings", question_embedding, k=5 )``` The `Dataset.get_nearest_examples()` function returns a tuple of scores that rank the overlap between the query and the document, and a corresponding set of samples (here, the 5 best matches). Let’s collect these in a `pandas.DataFrame` so we can easily sort them: ``` import pandas as pd samples_df = pd.DataFrame.from_dict(samples) samples_df["scores"] = scores samples_df.sort_values("scores", ascending=False, inplace=True)``` Now we can iterate over the first few rows to see how well our query matched the available comments: ``` for _, row in samples_df.iterrows(): print(f"COMMENT: {row.comments}") print(f"SCORE: {row.scores}") print(f"TITLE: {row.title}") print(f"URL: {row.html_url}") print("=" * 50) print()``` ``` """ COMMENT: Requiring online connection is a deal breaker in some cases unfortunately so it'd be great if offline mode is added similar to how `transformers` loads models offline fine. @mandubian's second bullet point suggests that there's a workaround allowing you to use your offline (custom?) dataset with `datasets`. Could you please elaborate on how that should look like? SCORE: 25.505046844482422 TITLE: Discussion using datasets in offline mode URL: https://github.com/huggingface/datasets/issues/824 ================================================== COMMENT: The local dataset builders (csv, text , json and pandas) are now part of the `datasets` package since #1726 :) You can now use them offline \`\`\`python datasets = load_dataset("text", data_files=data_files) \`\`\` We'll do a new release soon SCORE: 24.555509567260742 TITLE: Discussion using datasets in offline mode URL: https://github.com/huggingface/datasets/issues/824 ================================================== COMMENT: I opened a PR that allows to reload modules that have already been loaded once even if there's no internet. Let me know if you know other ways that can make the offline mode experience better. I'd be happy to add them :) I already note the "freeze" modules option, to prevent local modules updates. It would be a cool feature. ---------- > @mandubian's second bullet point suggests that there's a workaround allowing you to use your offline (custom?) dataset with `datasets`. Could you please elaborate on how that should look like? Indeed `load_dataset` allows to load remote dataset script (squad, glue, etc.) but also you own local ones. For example if you have a dataset script at `./my_dataset/my_dataset.py` then you can do \`\`\`python load_dataset("./my_dataset") \`\`\` and the dataset script will generate your dataset once and for all. ---------- About I'm looking into having `csv`, `json`, `text`, `pandas` dataset builders already included in the `datasets` package, so that they are available offline by default, as opposed to the other datasets that require the script to be downloaded. cf #1724 SCORE: 24.14896583557129 TITLE: Discussion using datasets in offline mode URL: https://github.com/huggingface/datasets/issues/824 ================================================== COMMENT: > here is my way to load a dataset offline, but it **requires** an online machine > > 1. (online machine) > > ``` > > import datasets > > data = datasets.load_dataset(...) > > data.save_to_disk(/YOUR/DATASET/DIR) > > ``` > > 2. copy the dir from online to the offline machine > > 3. (offline machine) > > ``` > > import datasets > > data = datasets.load_from_disk(/SAVED/DATA/DIR) > > ``` > > > > HTH. SCORE: 22.893993377685547 TITLE: Discussion using datasets in offline mode URL: https://github.com/huggingface/datasets/issues/824 ================================================== COMMENT: here is my way to load a dataset offline, but it **requires** an online machine 1. (online machine) \`\`\` import datasets data = datasets.load_dataset(...) data.save_to_disk(/YOUR/DATASET/DIR) \`\`\` 2. copy the dir from online to the offline machine 3. (offline machine) \`\`\` import datasets data = datasets.load_from_disk(/SAVED/DATA/DIR) \`\`\` HTH. SCORE: 22.406635284423828 TITLE: Discussion using datasets in offline mode URL: https://github.com/huggingface/datasets/issues/824 ================================================== """``` Not bad! Our second hit seems to match the query. ✏️ **Try it out!** Create your own query and see whether you can find an answer in the retrieved documents. You might have to increase the `k` parameter in `Dataset.get_nearest_examples()` to broaden the search.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Semantic search with FAISS - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter5/6"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Semantic search with FAISS - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;semantic-search-with-faiss&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;using-embeddings-for-semantic-search&quot;,&quot;title&quot;:&quot;Using embeddings for semantic search&quot;},{&quot;local&quot;:&quot;loading-and-preparing-the-dataset&quot;,&quot;title&quot;:&quot;Loading and preparing the dataset&quot;},{&quot;local&quot;:&quot;creating-text-embeddings&quot;,&quot;title&quot;:&quot;Creating text embeddings&quot;},{&quot;local&quot;:&quot;using-faiss-for-efficient-similarity-search&quot;,&quot;title&quot;:&quot;Using FAISS for efficient similarity search&quot;}],&quot;title&quot;:&quot;Semantic search with FAISS&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter5/6&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Semantic search with FAISS&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Semantic search with FAISS</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/2?fw=pt">What if my dataset isn't on the Hub? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/3?fw=pt">Time to slice and dice </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/4?fw=pt">Big data? 🤗 Datasets to the rescue! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/5?fw=pt">Creating your own dataset </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter5/6?fw=pt">Semantic search with FAISS </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/7?fw=pt">🤗 Datasets, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="semantic-search-with-faiss" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#semantic-search-with-faiss"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Semantic search with FAISS</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-5-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter5/section6_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter5/section6_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>In <a href="/course/chapter5/5">section 5</a>, we created a dataset of GitHub issues and comments from the 🤗 Datasets repository. In this section we’ll use this information to build a search engine that can help us find answers to our most pressing questions about the library!</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/OATCgQtNX2o" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <h2 class="relative group"><a id="using-embeddings-for-semantic-search" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-embeddings-for-semantic-search"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using embeddings for semantic search</span></h2> <p>As we saw in <a href="/course/chapter1">Chapter 1</a>, Transformer-based language models represent each token in a span of text as an <em>embedding vector</em>. It turns out that one can “pool” the individual embeddings to create a vector representation for whole sentences, paragraphs, or (in some cases) documents. These embeddings can then be used to find similar documents in the corpus by computing the dot-product similarity (or some other similarity metric) between each embedding and returning the documents with the greatest overlap.</p> <p>In this section we’ll use embeddings to develop a semantic search engine. These search engines offer several advantages over conventional approaches that are based on matching keywords in a query with the documents.</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/semantic-search.svg" alt="Semantic search."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter5/semantic-search-dark.svg" alt="Semantic search."></div> <h2 class="relative group"><a id="loading-and-preparing-the-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-and-preparing-the-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Loading and preparing the dataset</span></h2> <p>The first thing we need to do is download our dataset of GitHub issues, so let’s use <code>load_dataset()</code> function as usual:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset issues_dataset = load_dataset(<span class="hljs-string">"lewtun/github-issues"</span>, split=<span class="hljs-string">"train"</span>) issues_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Dataset({ features: [<span class="hljs-string">'url'</span>, <span class="hljs-string">'repository_url'</span>, <span class="hljs-string">'labels_url'</span>, <span class="hljs-string">'comments_url'</span>, <span class="hljs-string">'events_url'</span>, <span class="hljs-string">'html_url'</span>, <span class="hljs-string">'id'</span>, <span class="hljs-string">'node_id'</span>, <span class="hljs-string">'number'</span>, <span class="hljs-string">'title'</span>, <span class="hljs-string">'user'</span>, <span class="hljs-string">'labels'</span>, <span class="hljs-string">'state'</span>, <span class="hljs-string">'locked'</span>, <span class="hljs-string">'assignee'</span>, <span class="hljs-string">'assignees'</span>, <span class="hljs-string">'milestone'</span>, <span class="hljs-string">'comments'</span>, <span class="hljs-string">'created_at'</span>, <span class="hljs-string">'updated_at'</span>, <span class="hljs-string">'closed_at'</span>, <span class="hljs-string">'author_association'</span>, <span class="hljs-string">'active_lock_reason'</span>, <span class="hljs-string">'pull_request'</span>, <span class="hljs-string">'body'</span>, <span class="hljs-string">'performed_via_github_app'</span>, <span class="hljs-string">'is_pull_request'</span>], num_rows: <span class="hljs-number">2855</span> })</pre></div> <p>Here we’ve specified the default <code>train</code> split in <code>load_dataset()</code>, so it returns a <code>Dataset</code> instead of a <code>DatasetDict</code>. The first order of business is to filter out the pull requests, as these tend to be rarely used for answering user queries and will introduce noise in our search engine. As should be familiar by now, we can use the <code>Dataset.filter()</code> function to exclude these rows in our dataset. While we’re at it, let’s also filter out rows with no comments, since these provide no answers to user queries:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>issues_dataset = issues_dataset.<span class="hljs-built_in">filter</span>( <span class="hljs-keyword">lambda</span> x: (x[<span class="hljs-string">"is_pull_request"</span>] == <span class="hljs-literal">False</span> <span class="hljs-keyword">and</span> <span class="hljs-built_in">len</span>(x[<span class="hljs-string">"comments"</span>]) &gt; <span class="hljs-number">0</span>) ) issues_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Dataset({ features: [<span class="hljs-string">'url'</span>, <span class="hljs-string">'repository_url'</span>, <span class="hljs-string">'labels_url'</span>, <span class="hljs-string">'comments_url'</span>, <span class="hljs-string">'events_url'</span>, <span class="hljs-string">'html_url'</span>, <span class="hljs-string">'id'</span>, <span class="hljs-string">'node_id'</span>, <span class="hljs-string">'number'</span>, <span class="hljs-string">'title'</span>, <span class="hljs-string">'user'</span>, <span class="hljs-string">'labels'</span>, <span class="hljs-string">'state'</span>, <span class="hljs-string">'locked'</span>, <span class="hljs-string">'assignee'</span>, <span class="hljs-string">'assignees'</span>, <span class="hljs-string">'milestone'</span>, <span class="hljs-string">'comments'</span>, <span class="hljs-string">'created_at'</span>, <span class="hljs-string">'updated_at'</span>, <span class="hljs-string">'closed_at'</span>, <span class="hljs-string">'author_association'</span>, <span class="hljs-string">'active_lock_reason'</span>, <span class="hljs-string">'pull_request'</span>, <span class="hljs-string">'body'</span>, <span class="hljs-string">'performed_via_github_app'</span>, <span class="hljs-string">'is_pull_request'</span>], num_rows: <span class="hljs-number">771</span> })</pre></div> <p>We can see that there are a lot of columns in our dataset, most of which we don’t need to build our search engine. From a search perspective, the most informative columns are <code>title</code>, <code>body</code>, and <code>comments</code>, while <code>html_url</code> provides us with a link back to the source issue. Let’s use the <code>Dataset.remove_columns()</code> function to drop the rest:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>columns = issues_dataset.column_names columns_to_keep = [<span class="hljs-string">"title"</span>, <span class="hljs-string">"body"</span>, <span class="hljs-string">"html_url"</span>, <span class="hljs-string">"comments"</span>] columns_to_remove = <span class="hljs-built_in">set</span>(columns_to_keep).symmetric_difference(columns) issues_dataset = issues_dataset.remove_columns(columns_to_remove) issues_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Dataset({ features: [<span class="hljs-string">'html_url'</span>, <span class="hljs-string">'title'</span>, <span class="hljs-string">'comments'</span>, <span class="hljs-string">'body'</span>], num_rows: <span class="hljs-number">771</span> })</pre></div> <p>To create our embeddings we’ll augment each comment with the issue’s title and body, since these fields often include useful contextual information. Because our <code>comments</code> column is currently a list of comments for each issue, we need to “explode” the column so that each row consists of an <code>(html_url, title, body, comment)</code> tuple. In Pandas we can do this with the <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.explode.html" rel="nofollow"><code>DataFrame.explode()</code> function</a>, which creates a new row for each element in a list-like column, while replicating all the other column values. To see this in action, let’s first switch to the Pandas <code>DataFrame</code> format:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>issues_dataset.set_format(<span class="hljs-string">"pandas"</span>) df = issues_dataset[:]</pre></div> <p>If we inspect the first row in this <code>DataFrame</code> we can see there are four comments associated with this issue:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>df[<span class="hljs-string">"comments"</span>][<span class="hljs-number">0</span>].tolist()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'the bug code locate in :\r\n if data_args.task_name is not None:\r\n # Downloading and loading a dataset from the hub.\r\n datasets = load_dataset("glue", data_args.task_name, cache_dir=model_args.cache_dir)'</span>, <span class="hljs-string">'Hi @jinec,\r\n\r\nFrom time to time we get this kind of `ConnectionError` coming from the github.com website: https://raw.githubusercontent.com\r\n\r\nNormally, it should work if you wait a little and then retry.\r\n\r\nCould you please confirm if the problem persists?'</span>, <span class="hljs-string">'cannot connect,even by Web browser,please check that there is some problems。'</span>, <span class="hljs-string">'I can access https://raw.githubusercontent.com/huggingface/datasets/1.7.0/datasets/glue/glue.py without problem...'</span>]</pre></div> <p>When we explode <code>df</code>, we expect to get one row for each of these comments. Let’s check if that’s the case:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>comments_df = df.explode(<span class="hljs-string">"comments"</span>, ignore_index=<span class="hljs-literal">True</span>) comments_df.head(<span class="hljs-number">4</span>)</pre></div> <table border="1" class="dataframe" style="table-layout: fixed; word-wrap:break-word; width: 100%;"><thead><tr style="text-align: right;"><th></th> <th>html_url</th> <th>title</th> <th>comments</th> <th>body</th></tr></thead> <tbody><tr><th>0</th> <td>https://github.com/huggingface/datasets/issues/2787</td> <td>ConnectionError: Couldn't reach https://raw.githubusercontent.com</td> <td>the bug code locate in :\r\n if data_args.task_name is not None...</td> <td>Hello,\r\nI am trying to run run_glue.py and it gives me this error...</td></tr> <tr><th>1</th> <td>https://github.com/huggingface/datasets/issues/2787</td> <td>ConnectionError: Couldn't reach https://raw.githubusercontent.com</td> <td>Hi @jinec,\r\n\r\nFrom time to time we get this kind of `ConnectionError` coming from the github.com website: https://raw.githubusercontent.com...</td> <td>Hello,\r\nI am trying to run run_glue.py and it gives me this error...</td></tr> <tr><th>2</th> <td>https://github.com/huggingface/datasets/issues/2787</td> <td>ConnectionError: Couldn't reach https://raw.githubusercontent.com</td> <td>cannot connect,even by Web browser,please check that there is some problems。</td> <td>Hello,\r\nI am trying to run run_glue.py and it gives me this error...</td></tr> <tr><th>3</th> <td>https://github.com/huggingface/datasets/issues/2787</td> <td>ConnectionError: Couldn't reach https://raw.githubusercontent.com</td> <td>I can access https://raw.githubusercontent.com/huggingface/datasets/1.7.0/datasets/glue/glue.py without problem...</td> <td>Hello,\r\nI am trying to run run_glue.py and it gives me this error...</td></tr></tbody></table> <p>Great, we can see the rows have been replicated, with the <code>comments</code> column containing the individual comments! Now that we’re finished with Pandas, we can quickly switch back to a <code>Dataset</code> by loading the <code>DataFrame</code> in memory:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset comments_dataset = Dataset.from_pandas(comments_df) comments_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Dataset({ features: [<span class="hljs-string">'html_url'</span>, <span class="hljs-string">'title'</span>, <span class="hljs-string">'comments'</span>, <span class="hljs-string">'body'</span>], num_rows: <span class="hljs-number">2842</span> })</pre></div> <p>Okay, this has given us a few thousand comments to work with!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> See if you can use <code>Dataset.map()</code> to explode the <code>comments</code> column of <code>issues_dataset</code> <em>without</em> resorting to the use of Pandas. This is a little tricky; you might find the <a href="https://huggingface.co/docs/datasets/v1.12.1/about_map_batch.html?batch-mapping#batch-mapping" rel="nofollow">“Batch mapping”</a> section of the 🤗 Datasets documentation useful for this task.</p></div> <p>Now that we have one comment per row, let’s create a new <code>comments_length</code> column that contains the number of words per comment:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>comments_dataset = comments_dataset.<span class="hljs-built_in">map</span>( <span class="hljs-keyword">lambda</span> x: {<span class="hljs-string">"comment_length"</span>: <span class="hljs-built_in">len</span>(x[<span class="hljs-string">"comments"</span>].split())} )</pre></div> <p>We can use this new column to filter out short comments, which typically include things like “cc @lewtun” or “Thanks!” that are not relevant for our search engine. There’s no precise number to select for the filter, but around 15 words seems like a good start:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>comments_dataset = comments_dataset.<span class="hljs-built_in">filter</span>(<span class="hljs-keyword">lambda</span> x: x[<span class="hljs-string">"comment_length"</span>] &gt; <span class="hljs-number">15</span>) comments_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Dataset({ features: [<span class="hljs-string">'html_url'</span>, <span class="hljs-string">'title'</span>, <span class="hljs-string">'comments'</span>, <span class="hljs-string">'body'</span>, <span class="hljs-string">'comment_length'</span>], num_rows: <span class="hljs-number">2098</span> })</pre></div> <p>Having cleaned up our dataset a bit, let’s concatenate the issue title, description, and comments together in a new <code>text</code> column. As usual, we’ll write a simple function that we can pass to <code>Dataset.map()</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">concatenate_text</span>(<span class="hljs-params">examples</span>): <span class="hljs-keyword">return</span> { <span class="hljs-string">"text"</span>: examples[<span class="hljs-string">"title"</span>] + <span class="hljs-string">" \n "</span> + examples[<span class="hljs-string">"body"</span>] + <span class="hljs-string">" \n "</span> + examples[<span class="hljs-string">"comments"</span>] } comments_dataset = comments_dataset.<span class="hljs-built_in">map</span>(concatenate_text)</pre></div> <p>We’re finally ready to create some embeddings! Let’s take a look.</p> <h2 class="relative group"><a id="creating-text-embeddings" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#creating-text-embeddings"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Creating text embeddings</span></h2> <p>We saw in <a href="/course/chapter2">Chapter 2</a> that we can obtain token embeddings by using the <code>AutoModel</code> class. All we need to do is pick a suitable checkpoint to load the model from. Fortunately, there’s a library called <code>sentence-transformers</code> that is dedicated to creating embeddings. As described in the library’s <a href="https://www.sbert.net/examples/applications/semantic-search/README.html#symmetric-vs-asymmetric-semantic-search" rel="nofollow">documentation</a>, our use case is an example of <em>asymmetric semantic search</em> because we have a short query whose answer we’d like to find in a longer document, like a an issue comment. The handy <a href="https://www.sbert.net/docs/pretrained_models.html#model-overview" rel="nofollow">model overview table</a> in the documentation indicates that the <code>multi-qa-mpnet-base-dot-v1</code> checkpoint has the best performance for semantic search, so we’ll use that for our application. We’ll also load the tokenizer using the same checkpoint:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModel model_ckpt = <span class="hljs-string">"sentence-transformers/multi-qa-mpnet-base-dot-v1"</span> tokenizer = AutoTokenizer.from_pretrained(model_ckpt) model = AutoModel.from_pretrained(model_ckpt)</pre></div> <p>To speed up the embedding process, it helps to place the model and inputs on a GPU device, so let’s do that now:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch device = torch.device(<span class="hljs-string">"cuda"</span>) model.to(device)</pre></div> <p>As we mentioned earlier, we’d like to represent each entry in our GitHub issues corpus as a single vector, so we need to “pool” or average our token embeddings in some way. One popular approach is to perform <em>CLS pooling</em> on our model’s outputs, where we simply collect the last hidden state for the special <code>[CLS]</code> token. The following function does the trick for us:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">cls_pooling</span>(<span class="hljs-params">model_output</span>): <span class="hljs-keyword">return</span> model_output.last_hidden_state[:, <span class="hljs-number">0</span>]</pre></div> <p>Next, we’ll create a helper function that will tokenize a list of documents, place the tensors on the GPU, feed them to the model, and finally apply CLS pooling to the outputs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">get_embeddings</span>(<span class="hljs-params">text_list</span>): encoded_input = tokenizer( text_list, padding=<span class="hljs-literal">True</span>, truncation=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">"pt"</span> ) encoded_input = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> encoded_input.items()} model_output = model(**encoded_input) <span class="hljs-keyword">return</span> cls_pooling(model_output)</pre></div> <p>We can test the function works by feeding it the first text entry in our corpus and inspecting the output shape:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>embedding = get_embeddings(comments_dataset[<span class="hljs-string">"text"</span>][<span class="hljs-number">0</span>]) embedding.shape</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>torch.Size([<span class="hljs-number">1</span>, <span class="hljs-number">768</span>])</pre></div> <p>Great, we’ve converted the first entry in our corpus into a 768-dimensional vector! We can use <code>Dataset.map()</code> to apply our <code>get_embeddings()</code> function to each row in our corpus, so let’s create a new <code>embeddings</code> column as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>embeddings_dataset = comments_dataset.<span class="hljs-built_in">map</span>( <span class="hljs-keyword">lambda</span> x: {<span class="hljs-string">"embeddings"</span>: get_embeddings(x[<span class="hljs-string">"text"</span>]).detach().cpu().numpy()[<span class="hljs-number">0</span>]} )</pre></div> <p>Notice that we’ve converted the embeddings to NumPy arrays — that’s because 🤗 Datasets requires this format when we try to index them with FAISS, which we’ll do next.</p> <h2 class="relative group"><a id="using-faiss-for-efficient-similarity-search" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-faiss-for-efficient-similarity-search"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using FAISS for efficient similarity search</span></h2> <p>Now that we have a dataset of embeddings, we need some way to search over them. To do this, we’ll use a special data structure in 🤗 Datasets called a <em>FAISS index</em>. <a href="https://faiss.ai/" rel="nofollow">FAISS</a> (short for Facebook AI Similarity Search) is a library that provides efficient algorithms to quickly search and cluster embedding vectors.</p> <p>The basic idea behind FAISS is to create a special data structure called an <em>index</em> that allows one to find which embeddings are similar to an input embedding. Creating a FAISS index in 🤗 Datasets is simple — we use the <code>Dataset.add_faiss_index()</code> function and specify which column of our dataset we’d like to index:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>embeddings_dataset.add_faiss_index(column=<span class="hljs-string">"embeddings"</span>)</pre></div> <p>We can now perform queries on this index by doing a nearest neighbor lookup with the <code>Dataset.get_nearest_examples()</code> function. Let’s test this out by first embedding a question as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>question = <span class="hljs-string">"How can I load a dataset offline?"</span> question_embedding = get_embeddings([question]).cpu().detach().numpy() question_embedding.shape</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>torch.Size([<span class="hljs-number">1</span>, <span class="hljs-number">768</span>])</pre></div> <p>Just like with the documents, we now have a 768-dimensional vector representing the query, which we can compare against the whole corpus to find the most similar embeddings:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>scores, samples = embeddings_dataset.get_nearest_examples( <span class="hljs-string">"embeddings"</span>, question_embedding, k=<span class="hljs-number">5</span> )</pre></div> <p>The <code>Dataset.get_nearest_examples()</code> function returns a tuple of scores that rank the overlap between the query and the document, and a corresponding set of samples (here, the 5 best matches). Let’s collect these in a <code>pandas.DataFrame</code> so we can easily sort them:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd samples_df = pd.DataFrame.from_dict(samples) samples_df[<span class="hljs-string">"scores"</span>] = scores samples_df.sort_values(<span class="hljs-string">"scores"</span>, ascending=<span class="hljs-literal">False</span>, inplace=<span class="hljs-literal">True</span>)</pre></div> <p>Now we can iterate over the first few rows to see how well our query matched the available comments:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> _, row <span class="hljs-keyword">in</span> samples_df.iterrows(): <span class="hljs-built_in">print</span>(<span class="hljs-string">f"COMMENT: <span class="hljs-subst">{row.comments}</span>"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"SCORE: <span class="hljs-subst">{row.scores}</span>"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"TITLE: <span class="hljs-subst">{row.title}</span>"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"URL: <span class="hljs-subst">{row.html_url}</span>"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">"="</span> * <span class="hljs-number">50</span>) <span class="hljs-built_in">print</span>()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">""" COMMENT: Requiring online connection is a deal breaker in some cases unfortunately so it'd be great if offline mode is added similar to how `transformers` loads models offline fine. @mandubian's second bullet point suggests that there's a workaround allowing you to use your offline (custom?) dataset with `datasets`. Could you please elaborate on how that should look like? SCORE: 25.505046844482422 TITLE: Discussion using datasets in offline mode URL: https://github.com/huggingface/datasets/issues/824 ================================================== COMMENT: The local dataset builders (csv, text , json and pandas) are now part of the `datasets` package since #1726 :) You can now use them offline \`\`\`python datasets = load_dataset("text", data_files=data_files) \`\`\` We'll do a new release soon SCORE: 24.555509567260742 TITLE: Discussion using datasets in offline mode URL: https://github.com/huggingface/datasets/issues/824 ================================================== COMMENT: I opened a PR that allows to reload modules that have already been loaded once even if there's no internet. Let me know if you know other ways that can make the offline mode experience better. I'd be happy to add them :) I already note the "freeze" modules option, to prevent local modules updates. It would be a cool feature. ---------- &gt; @mandubian's second bullet point suggests that there's a workaround allowing you to use your offline (custom?) dataset with `datasets`. Could you please elaborate on how that should look like? Indeed `load_dataset` allows to load remote dataset script (squad, glue, etc.) but also you own local ones. For example if you have a dataset script at `./my_dataset/my_dataset.py` then you can do \`\`\`python load_dataset("./my_dataset") \`\`\` and the dataset script will generate your dataset once and for all. ---------- About I'm looking into having `csv`, `json`, `text`, `pandas` dataset builders already included in the `datasets` package, so that they are available offline by default, as opposed to the other datasets that require the script to be downloaded. cf #1724 SCORE: 24.14896583557129 TITLE: Discussion using datasets in offline mode URL: https://github.com/huggingface/datasets/issues/824 ================================================== COMMENT: &gt; here is my way to load a dataset offline, but it **requires** an online machine &gt; &gt; 1. (online machine) &gt; &gt; ``` &gt; &gt; import datasets &gt; &gt; data = datasets.load_dataset(...) &gt; &gt; data.save_to_disk(/YOUR/DATASET/DIR) &gt; &gt; ``` &gt; &gt; 2. copy the dir from online to the offline machine &gt; &gt; 3. (offline machine) &gt; &gt; ``` &gt; &gt; import datasets &gt; &gt; data = datasets.load_from_disk(/SAVED/DATA/DIR) &gt; &gt; ``` &gt; &gt; &gt; &gt; HTH. SCORE: 22.893993377685547 TITLE: Discussion using datasets in offline mode URL: https://github.com/huggingface/datasets/issues/824 ================================================== COMMENT: here is my way to load a dataset offline, but it **requires** an online machine 1. (online machine) \`\`\` import datasets data = datasets.load_dataset(...) data.save_to_disk(/YOUR/DATASET/DIR) \`\`\` 2. copy the dir from online to the offline machine 3. (offline machine) \`\`\` import datasets data = datasets.load_from_disk(/SAVED/DATA/DIR) \`\`\` HTH. SCORE: 22.406635284423828 TITLE: Discussion using datasets in offline mode URL: https://github.com/huggingface/datasets/issues/824 ================================================== """</span></pre></div> <p>Not bad! Our second hit seems to match the query.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Create your own query and see whether you can find an answer in the retrieved documents. You might have to increase the <code>k</code> parameter in <code>Dataset.get_nearest_examples()</code> to broaden the search.</p></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter5/5?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Creating your own dataset</a> <a href="/learn/nlp-course/chapter5/7?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">🤗 Datasets, check!<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;semantic-search-with-faiss&quot;,&quot;url&quot;:&quot;#semantic-search-with-faiss&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Using embeddings for semantic search&quot;,&quot;id&quot;:&quot;using-embeddings-for-semantic-search&quot;,&quot;url&quot;:&quot;#using-embeddings-for-semantic-search&quot;},{&quot;title&quot;:&quot;Loading and preparing the dataset&quot;,&quot;id&quot;:&quot;loading-and-preparing-the-dataset&quot;,&quot;url&quot;:&quot;#loading-and-preparing-the-dataset&quot;},{&quot;title&quot;:&quot;Creating text embeddings&quot;,&quot;id&quot;:&quot;creating-text-embeddings&quot;,&quot;url&quot;:&quot;#creating-text-embeddings&quot;},{&quot;title&quot;:&quot;Using FAISS for efficient similarity search&quot;,&quot;id&quot;:&quot;using-faiss-for-efficient-similarity-search&quot;,&quot;url&quot;:&quot;#using-faiss-for-efficient-similarity-search&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#semantic-search-with-faiss" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-semantic-search-with-faiss"><wbr>Semantic search with FAISS</a> <a href="#using-embeddings-for-semantic-search" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-embeddings-for-semantic-search"><wbr>Using embeddings for semantic search</a> <a href="#loading-and-preparing-the-dataset" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-loading-and-preparing-the-dataset"><wbr>Loading and preparing the dataset</a> <a href="#creating-text-embeddings" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-creating-text-embeddings"><wbr>Creating text embeddings</a> <a href="#using-faiss-for-efficient-similarity-search" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-faiss-for-efficient-similarity-search"><wbr>Using FAIS<wbr>S for efficient similarity search</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter5/6" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter5/6"); } </script> <iframe name="__privateStripeMetricsController4900" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter5%2F6%3Ffw%3Dpt&amp;title=Semantic%20search%20with%20FAISS%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:20.301Z
End-of-chapter quiz - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter5/8?fw=pt
## [](#end-of-chapter-quiz)End-of-chapter quiz [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-5-questions) This chapter covered a lot of ground! Don’t worry if you didn’t grasp all the details; the next chapters will help you understand how things work under the hood. Before moving on, though, let’s test what you learned in this chapter. ### [](#1.-the-<code>load_dataset()</code>-function-in-🤗-datasets-allows-you-to-load-a-dataset-from-which-of-the-following-locations?)1\. The `load_dataset()` function in 🤗 Datasets allows you to load a dataset from which of the following locations? ### [](#2.-suppose-you-load-one-of-the-glue-tasks-as-follows:)2\. Suppose you load one of the GLUE tasks as follows: ``` from datasets import load_dataset dataset = load_dataset("glue", "mrpc", split="train")``` Which of the following commands will produce a random sample of 50 elements from `dataset`? ### [](#3.-suppose-you-have-a-dataset-about-household-pets-called-<code>pets_dataset</code>,-which-has-a-<code>name</code>-column-that-denotes-the-name-of-each-pet.-which-of-the-following-approaches-would-allow-you-to-filter-the-dataset-for-all-pets-whose-names-start-with-the-letter-“l”?)3\. Suppose you have a dataset about household pets called `pets_dataset`, which has a `name` column that denotes the name of each pet. Which of the following approaches would allow you to filter the dataset for all pets whose names start with the letter “L”? ### [](#4.-what-is-memory-mapping?)4\. What is memory mapping? ### [](#5.-which-of-the-following-are-the-main-benefits-of-memory-mapping?)5\. Which of the following are the main benefits of memory mapping? ### [](#6.-why-does-the-following-code-fail?)6\. Why does the following code fail? ``` from datasets import load_dataset dataset = load_dataset("allocine", streaming=True, split="train") dataset[0]``` ### [](#7.-which-of-the-following-are-the-main-benefits-of-creating-a-dataset-card?)7\. Which of the following are the main benefits of creating a dataset card? ### [](#8.-what-is-semantic-search?)8\. What is semantic search? ### [](#9.-for-asymmetric-semantic-search,-you-usually-have:)9\. For asymmetric semantic search, you usually have: ### [](#10.-can-i-use-🤗-datasets-to-load-data-for-use-in-other-domains,-like-speech-processing?)10\. Can I use 🤗 Datasets to load data for use in other domains, like speech processing?
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="End-of-chapter quiz - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter5/8"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>End-of-chapter quiz - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;end-of-chapter-quiz&quot;,&quot;title&quot;:&quot;End-of-chapter quiz&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter5/8&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;End-of-chapter quiz&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">End-of-chapter quiz</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/2?fw=pt">What if my dataset isn't on the Hub? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/3?fw=pt">Time to slice and dice </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/4?fw=pt">Big data? 🤗 Datasets to the rescue! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/5?fw=pt">Creating your own dataset </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/6?fw=pt">Semantic search with FAISS </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter5/7?fw=pt">🤗 Datasets, check! </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter5/8?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="end-of-chapter-quiz" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#end-of-chapter-quiz"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>End-of-chapter quiz</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-5-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>This chapter covered a lot of ground! Don’t worry if you didn’t grasp all the details; the next chapters will help you understand how things work under the hood.</p> <p>Before moving on, though, let’s test what you learned in this chapter.</p> <h3 class="relative group"><a id="1.-the-<code>load_dataset()</code>-function-in-🤗-datasets-allows-you-to-load-a-dataset-from-which-of-the-following-locations?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#1.-the-<code>load_dataset()</code>-function-in-🤗-datasets-allows-you-to-load-a-dataset-from-which-of-the-following-locations?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>1. The <code>load_dataset()</code> function in 🤗 Datasets allows you to load a dataset from which of the following locations?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Locally, e.g. on your laptop</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> The Hugging Face Hub</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A remote server</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="2.-suppose-you-load-one-of-the-glue-tasks-as-follows:" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#2.-suppose-you-load-one-of-the-glue-tasks-as-follows:"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>2. Suppose you load one of the GLUE tasks as follows:</span></h3> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset dataset = load_dataset(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mrpc"</span>, split=<span class="hljs-string">"train"</span>)</pre></div> <p>Which of the following commands will produce a random sample of 50 elements from <code>dataset</code>?</p> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> <code>dataset.sample(50)</code></label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> <code>dataset.shuffle().select(range(50))</code></label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> <code>dataset.select(range(50)).shuffle()</code></label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="3.-suppose-you-have-a-dataset-about-household-pets-called-<code>pets_dataset</code>,-which-has-a-<code>name</code>-column-that-denotes-the-name-of-each-pet.-which-of-the-following-approaches-would-allow-you-to-filter-the-dataset-for-all-pets-whose-names-start-with-the-letter-“l”?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#3.-suppose-you-have-a-dataset-about-household-pets-called-<code>pets_dataset</code>,-which-has-a-<code>name</code>-column-that-denotes-the-name-of-each-pet.-which-of-the-following-approaches-would-allow-you-to-filter-the-dataset-for-all-pets-whose-names-start-with-the-letter-“l”?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>3. Suppose you have a dataset about household pets called <code>pets_dataset</code>, which has a <code>name</code> column that denotes the name of each pet. Which of the following approaches would allow you to filter the dataset for all pets whose names start with the letter “L”?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> <code>pets_dataset.filter(lambda x : x['name'].startswith('L'))</code></label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> <code>pets_dataset.filter(lambda x['name'].startswith('L'))</code></label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Create a function like <code>def filter_names(x): return x['name'].startswith('L')</code> and run <code>pets_dataset.filter(filter_names)</code>.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="4.-what-is-memory-mapping?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#4.-what-is-memory-mapping?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>4. What is memory mapping?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> A mapping between CPU and GPU RAM</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> A mapping between RAM and filesystem storage</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A mapping between two files in the 🤗 Datasets cache</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="5.-which-of-the-following-are-the-main-benefits-of-memory-mapping?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#5.-which-of-the-following-are-the-main-benefits-of-memory-mapping?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>5. Which of the following are the main benefits of memory mapping?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Accessing memory-mapped files is faster than reading from or writing to disk.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Applications can access segments of data in an extremely large file without having to read the whole file into RAM first.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It consumes less energy, so your battery lasts longer.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="6.-why-does-the-following-code-fail?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#6.-why-does-the-following-code-fail?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>6. Why does the following code fail?</span></h3> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset dataset = load_dataset(<span class="hljs-string">"allocine"</span>, streaming=<span class="hljs-literal">True</span>, split=<span class="hljs-string">"train"</span>) dataset[<span class="hljs-number">0</span>]</pre></div> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It tries to stream a dataset that's too large to fit in RAM.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It tries to access an <code>IterableDataset</code>.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> The <code>allocine</code> dataset doesn't have a <code>train</code> split.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="7.-which-of-the-following-are-the-main-benefits-of-creating-a-dataset-card?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#7.-which-of-the-following-are-the-main-benefits-of-creating-a-dataset-card?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>7. Which of the following are the main benefits of creating a dataset card?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It provides information about the intended use and supported tasks of the dataset so others in the community can make an informed decision about using it.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It helps draw attention to the biases that are present in a corpus.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It improves the chances that others in the community will use my dataset.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="8.-what-is-semantic-search?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#8.-what-is-semantic-search?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>8. What is semantic search?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> A way to search for exact matches between the words in a query and the documents in a corpus</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> A way to search for matching documents by understanding the contextual meaning of a query</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A way to improve search accuracy</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="9.-for-asymmetric-semantic-search,-you-usually-have:" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#9.-for-asymmetric-semantic-search,-you-usually-have:"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>9. For asymmetric semantic search, you usually have:</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> A short query and a longer paragraph that answers the query</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Queries and paragraphs that are of about the same length</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A long query and a shorter paragraph that answers the query</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="10.-can-i-use-🤗-datasets-to-load-data-for-use-in-other-domains,-like-speech-processing?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#10.-can-i-use-🤗-datasets-to-load-data-for-use-in-other-domains,-like-speech-processing?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>10. Can I use 🤗 Datasets to load data for use in other domains, like speech processing?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> No</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Yes</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter5/7?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>🤗 Datasets, check!</a> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isLoggedIn&quot;:false,&quot;nextChapterUrl&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;,&quot;quizChapter&quot;:5}" data-target="CourseCompleteButton"><div class="relative ml-auto"> <button class="btn btn-lg" disabled=""><svg class="mr-2" width="1em" height="1em" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0 16C0 7.16344 7.16344 0 16 0V0C24.8366 0 32 7.16344 32 16V16C32 24.8366 24.8366 32 16 32V32C7.16344 32 0 24.8366 0 16V16Z" fill="#D9FFD2"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M23.5607 10.9393C24.1464 11.5251 24.1464 12.4749 23.5607 13.0607L14.5 22.1213L8.93934 16.5607C8.35355 15.9749 8.35355 15.0251 8.93934 14.4393C9.52513 13.8536 10.4749 13.8536 11.0607 14.4393L14.5 17.8787L21.4393 10.9393C22.0251 10.3536 22.9749 10.3536 23.5607 10.9393Z" fill="#188B3F"></path></svg> Complete Chapter</button></div></div> <a href="/learn/nlp-course/chapter6/1?fw=pt" class="hidden">Next chapter</a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;id&quot;:&quot;end-of-chapter-quiz&quot;,&quot;url&quot;:&quot;#end-of-chapter-quiz&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#end-of-chapter-quiz" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-end-of-chapter-quiz"><wbr>End-of-chapter quiz</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter5/8" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter5/8"); } </script> <iframe name="__privateStripeMetricsController2610" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter5%2F8%3Ffw%3Dpt&amp;title=End-of-chapter%20quiz%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:20.409Z
Introduction - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter6/1?fw=pt
## [](#introduction)Introduction [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-6-questions) In [Chapter 3](/course/chapter3), we looked at how to fine-tune a model on a given task. When we do that, we use the same tokenizer that the model was pretrained with — but what do we do when we want to train a model from scratch? In these cases, using a tokenizer that was pretrained on a corpus from another domain or language is typically suboptimal. For example, a tokenizer that’s trained on an English corpus will perform poorly on a corpus of Japanese texts because the use of spaces and punctuation is very different in the two languages. In this chapter, you will learn how to train a brand new tokenizer on a corpus of texts, so it can then be used to pretrain a language model. This will all be done with the help of the [🤗 Tokenizers](https://github.com/huggingface/tokenizers) library, which provides the “fast” tokenizers in the [🤗 Transformers](https://github.com/huggingface/transformers) library. We’ll take a close look at the features that this library provides, and explore how the fast tokenizers differ from the “slow” versions. Topics we will cover include: - How to train a new tokenizer similar to the one used by a given checkpoint on a new corpus of texts - The special features of fast tokenizers - The differences between the three main subword tokenization algorithms used in NLP today - How to build a tokenizer from scratch with the 🤗 Tokenizers library and train it on some data The techniques introduced in this chapter will prepare you for the section in [Chapter 7](/course/chapter7/6) where we look at creating a language model for Python source code. Let’s start by looking at what it means to “train” a tokenizer in the first place.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Introduction - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter6/1"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Introduction - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;introduction&quot;,&quot;title&quot;:&quot;Introduction&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter6/1&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Introduction&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Introduction</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter6/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/2?fw=pt">Training a new tokenizer from an old one </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3?fw=pt">Fast tokenizers' special powers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3b?fw=pt">Fast tokenizers in the QA pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/4?fw=pt">Normalization and pre-tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/5?fw=pt">Byte-Pair Encoding tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/6?fw=pt">WordPiece tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/7?fw=pt">Unigram tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/8?fw=pt">Building a tokenizer, block by block </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/9?fw=pt">Tokenizers, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="introduction" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#introduction"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Introduction</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-6-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>In <a href="/course/chapter3">Chapter 3</a>, we looked at how to fine-tune a model on a given task. When we do that, we use the same tokenizer that the model was pretrained with — but what do we do when we want to train a model from scratch? In these cases, using a tokenizer that was pretrained on a corpus from another domain or language is typically suboptimal. For example, a tokenizer that’s trained on an English corpus will perform poorly on a corpus of Japanese texts because the use of spaces and punctuation is very different in the two languages.</p> <p>In this chapter, you will learn how to train a brand new tokenizer on a corpus of texts, so it can then be used to pretrain a language model. This will all be done with the help of the <a href="https://github.com/huggingface/tokenizers" rel="nofollow">🤗 Tokenizers</a> library, which provides the “fast” tokenizers in the <a href="https://github.com/huggingface/transformers" rel="nofollow">🤗 Transformers</a> library. We’ll take a close look at the features that this library provides, and explore how the fast tokenizers differ from the “slow” versions.</p> <p>Topics we will cover include:</p> <ul><li>How to train a new tokenizer similar to the one used by a given checkpoint on a new corpus of texts</li> <li>The special features of fast tokenizers</li> <li>The differences between the three main subword tokenization algorithms used in NLP today</li> <li>How to build a tokenizer from scratch with the 🤗 Tokenizers library and train it on some data</li></ul> <p>The techniques introduced in this chapter will prepare you for the section in <a href="/course/chapter7/6">Chapter 7</a> where we look at creating a language model for Python source code. Let’s start by looking at what it means to “train” a tokenizer in the first place.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter5/8?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>End-of-chapter quiz</a> <a href="/learn/nlp-course/chapter6/2?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Training a new tokenizer from an old one<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;introduction&quot;,&quot;url&quot;:&quot;#introduction&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#introduction" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-introduction"><wbr>Introduction</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter6/1" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter6/1"); } </script> <iframe name="__privateStripeMetricsController2320" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter6%2F1%3Ffw%3Dpt&amp;title=Introduction%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:20.960Z
Fast tokenizers in the QA pipeline - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter6/3b?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#fast-tokenizers-in-the-qa-pipeline)Fast tokenizers in the QA pipeline [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-6-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section3b_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section3b_pt.ipynb) We will now dive into the `question-answering` pipeline and see how to leverage the offsets to grab the answer to the question at hand from the context, a bit like we did for the grouped entities in the previous section. Then we will see how we can deal with very long contexts that end up being truncated. You can skip this section if you’re not interested in the question answering task. ## [](#using-the-question-answering-pipeline)Using the `question-answering` pipeline As we saw in [Chapter 1](/course/chapter1), we can use the `question-answering` pipeline like this to get the answer to a question: ``` from transformers import pipeline question_answerer = pipeline("question-answering") context = """ 🤗 Transformers is backed by the three most popular deep learning libraries — Jax, PyTorch, and TensorFlow — with a seamless integration between them. It's straightforward to train your models with one before loading them for inference with the other. """ question = "Which deep learning libraries back 🤗 Transformers?" question_answerer(question=question, context=context)``` ``` {'score': 0.97773, 'start': 78, 'end': 105, 'answer': 'Jax, PyTorch and TensorFlow'}``` Unlike the other pipelines, which can’t truncate and split texts that are longer than the maximum length accepted by the model (and thus may miss information at the end of a document), this pipeline can deal with very long contexts and will return the answer to the question even if it’s at the end: ``` long_context = """ 🤗 Transformers: State of the Art NLP 🤗 Transformers provides thousands of pretrained models to perform tasks on texts such as classification, information extraction, question answering, summarization, translation, text generation and more in over 100 languages. Its aim is to make cutting-edge NLP easier to use for everyone. 🤗 Transformers provides APIs to quickly download and use those pretrained models on a given text, fine-tune them on your own datasets and then share them with the community on our model hub. At the same time, each python module defining an architecture is fully standalone and can be modified to enable quick research experiments. Why should I use transformers? 1. Easy-to-use state-of-the-art models: - High performance on NLU and NLG tasks. - Low barrier to entry for educators and practitioners. - Few user-facing abstractions with just three classes to learn. - A unified API for using all our pretrained models. - Lower compute costs, smaller carbon footprint: 2. Researchers can share trained models instead of always retraining. - Practitioners can reduce compute time and production costs. - Dozens of architectures with over 10,000 pretrained models, some in more than 100 languages. 3. Choose the right framework for every part of a model's lifetime: - Train state-of-the-art models in 3 lines of code. - Move a single model between TF2.0/PyTorch frameworks at will. - Seamlessly pick the right framework for training, evaluation and production. 4. Easily customize a model or an example to your needs: - We provide examples for each architecture to reproduce the results published by its original authors. - Model internals are exposed as consistently as possible. - Model files can be used independently of the library for quick experiments. 🤗 Transformers is backed by the three most popular deep learning libraries — Jax, PyTorch and TensorFlow — with a seamless integration between them. It's straightforward to train your models with one before loading them for inference with the other. """ question_answerer(question=question, context=long_context)``` ``` {'score': 0.97149, 'start': 1892, 'end': 1919, 'answer': 'Jax, PyTorch and TensorFlow'}``` Let’s see how it does all of this! ## [](#using-a-model-for-question-answering)Using a model for question answering Like with any other pipeline, we start by tokenizing our input and then send it through the model. The checkpoint used by default for the `question-answering` pipeline is [`distilbert-base-cased-distilled-squad`](https://huggingface.co/distilbert-base-cased-distilled-squad) (the “squad” in the name comes from the dataset on which the model was fine-tuned; we’ll talk more about the SQuAD dataset in [Chapter 7](/course/chapter7/7)): ``` from transformers import AutoTokenizer, AutoModelForQuestionAnswering model_checkpoint = "distilbert-base-cased-distilled-squad" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) model = AutoModelForQuestionAnswering.from_pretrained(model_checkpoint) inputs = tokenizer(question, context, return_tensors="pt") outputs = model(**inputs)``` Note that we tokenize the question and the context as a pair, with the question first. ![An example of tokenization of question and context](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/question_tokens.svg) ![An example of tokenization of question and context](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/question_tokens-dark.svg) Models for question answering work a little differently from the models we’ve seen up to now. Using the picture above as an example, the model has been trained to predict the index of the token starting the answer (here 21) and the index of the token where the answer ends (here 24). This is why those models don’t return one tensor of logits but two: one for the logits corresponding to the start token of the answer, and one for the logits corresponding to the end token of the answer. Since in this case we have only one input containing 66 tokens, we get: ``` start_logits = outputs.start_logits end_logits = outputs.end_logits print(start_logits.shape, end_logits.shape)``` ``` torch.Size([1, 66]) torch.Size([1, 66])``` To convert those logits into probabilities, we will apply a softmax function — but before that, we need to make sure we mask the indices that are not part of the context. Our input is `[CLS] question [SEP] context [SEP]`, so we need to mask the tokens of the question as well as the `[SEP]` token. We’ll keep the `[CLS]` token, however, as some models use it to indicate that the answer is not in the context. Since we will apply a softmax afterward, we just need to replace the logits we want to mask with a large negative number. Here, we use `-10000`: ``` import torch sequence_ids = inputs.sequence_ids() mask = [i != 1 for i in sequence_ids] mask[0] = False mask = torch.tensor(mask)[None] start_logits[mask] = -10000 end_logits[mask] = -10000``` Now that we have properly masked the logits corresponding to positions we don’t want to predict, we can apply the softmax: ``` start_probabilities = torch.nn.functional.softmax(start_logits, dim=-1)[0] end_probabilities = torch.nn.functional.softmax(end_logits, dim=-1)[0]``` At this stage, we could take the argmax of the start and end probabilities — but we might end up with a start index that is greater than the end index, so we need to take a few more precautions. We will compute the probabilities of each possible `start_index` and `end_index` where `start_index <= end_index`, then take the tuple `(start_index, end_index)` with the highest probability. Assuming the events “The answer starts at `start_index`” and “The answer ends at `end_index`” to be independent, the probability that the answer starts at `start_index` and ends at `end_index` is: start\_probabilities\[start\_index\]×end\_probabilities\[end\_index\]\\mathrm{start\\\_probabilities}\[\\mathrm{start\\\_index}\] \\times \\mathrm{end\\\_probabilities}\[\\mathrm{end\\\_index}\] So, to compute all the scores, we just need to compute all the products start\_probabilities\[start\_index\]×end\_probabilities\[end\_index\]\\mathrm{start\\\_probabilities}\[\\mathrm{start\\\_index}\] \\times \\mathrm{end\\\_probabilities}\[\\mathrm{end\\\_index}\] where `start_index <= end_index`. First let’s compute all the possible products: ``` scores = start_probabilities[:, None] * end_probabilities[None, :]``` Then we’ll mask the values where `start_index > end_index` by setting them to `0` (the other probabilities are all positive numbers). The `torch.triu()` function returns the upper triangular part of the 2D tensor passed as an argument, so it will do that masking for us: ``` scores = torch.triu(scores)``` Now we just have to get the index of the maximum. Since PyTorch will return the index in the flattened tensor, we need to use the floor division `//` and modulus `%` operations to get the `start_index` and `end_index`: ``` max_index = scores.argmax().item() start_index = max_index // scores.shape[1] end_index = max_index % scores.shape[1] print(scores[start_index, end_index])``` We’re not quite done yet, but at least we already have the correct score for the answer (you can check this by comparing it to the first result in the previous section): ✏️ **Try it out!** Compute the start and end indices for the five most likely answers. We have the `start_index` and `end_index` of the answer in terms of tokens, so now we just need to convert to the character indices in the context. This is where the offsets will be super useful. We can grab them and use them like we did in the token classification task: ``` inputs_with_offsets = tokenizer(question, context, return_offsets_mapping=True) offsets = inputs_with_offsets["offset_mapping"] start_char, _ = offsets[start_index] _, end_char = offsets[end_index] answer = context[start_char:end_char]``` Now we just have to format everything to get our result: ``` result = { "answer": answer, "start": start_char, "end": end_char, "score": scores[start_index, end_index], } print(result)``` ``` {'answer': 'Jax, PyTorch and TensorFlow', 'start': 78, 'end': 105, 'score': 0.97773}``` Great! That’s the same as in our first example! ✏️ **Try it out!** Use the best scores you computed earlier to show the five most likely answers. To check your results, go back to the first pipeline and pass in `top_k=5` when calling it. ## [](#handling-long-contexts)Handling long contexts If we try to tokenize the question and long context we used as an example previously, we’ll get a number of tokens higher than the maximum length used in the `question-answering` pipeline (which is 384): ``` inputs = tokenizer(question, long_context) print(len(inputs["input_ids"]))``` So, we’ll need to truncate our inputs at that maximum length. There are several ways we can do this, but we don’t want to truncate the question, only the context. Since the context is the second sentence, we’ll use the `"only_second"` truncation strategy. The problem that arises then is that the answer to the question may not be in the truncated context. Here, for instance, we picked a question where the answer is toward the end of the context, and when we truncate it that answer is not present: ``` inputs = tokenizer(question, long_context, max_length=384, truncation="only_second") print(tokenizer.decode(inputs["input_ids"]))``` ``` """ [CLS] Which deep learning libraries back [UNK] Transformers? [SEP] [UNK] Transformers : State of the Art NLP [UNK] Transformers provides thousands of pretrained models to perform tasks on texts such as classification, information extraction, question answering, summarization, translation, text generation and more in over 100 languages. Its aim is to make cutting-edge NLP easier to use for everyone. [UNK] Transformers provides APIs to quickly download and use those pretrained models on a given text, fine-tune them on your own datasets and then share them with the community on our model hub. At the same time, each python module defining an architecture is fully standalone and can be modified to enable quick research experiments. Why should I use transformers? 1. Easy-to-use state-of-the-art models: - High performance on NLU and NLG tasks. - Low barrier to entry for educators and practitioners. - Few user-facing abstractions with just three classes to learn. - A unified API for using all our pretrained models. - Lower compute costs, smaller carbon footprint: 2. Researchers can share trained models instead of always retraining. - Practitioners can reduce compute time and production costs. - Dozens of architectures with over 10,000 pretrained models, some in more than 100 languages. 3. Choose the right framework for every part of a model's lifetime: - Train state-of-the-art models in 3 lines of code. - Move a single model between TF2.0/PyTorch frameworks at will. - Seamlessly pick the right framework for training, evaluation and production. 4. Easily customize a model or an example to your needs: - We provide examples for each architecture to reproduce the results published by its original authors. - Model internal [SEP] """``` This means the model will have a hard time picking the correct answer. To fix this, the `question-answering` pipeline allows us to split the context into smaller chunks, specifying the maximum length. To make sure we don’t split the context at exactly the wrong place to make it possible to find the answer, it also includes some overlap between the chunks. We can have the tokenizer (fast or slow) do this for us by adding `return_overflowing_tokens=True`, and we can specify the overlap we want with the `stride` argument. Here is an example, using a smaller sentence: ``` sentence = "This sentence is not too long but we are going to split it anyway." inputs = tokenizer( sentence, truncation=True, return_overflowing_tokens=True, max_length=6, stride=2 ) for ids in inputs["input_ids"]: print(tokenizer.decode(ids))``` ``` '[CLS] This sentence is not [SEP]' '[CLS] is not too long [SEP]' '[CLS] too long but we [SEP]' '[CLS] but we are going [SEP]' '[CLS] are going to split [SEP]' '[CLS] to split it anyway [SEP]' '[CLS] it anyway. [SEP]'``` As we can see, the sentence has been split into chunks in such a way that each entry in `inputs["input_ids"]` has at most 6 tokens (we would need to add padding to have the last entry be the same size as the others) and there is an overlap of 2 tokens between each of the entries. Let’s take a closer look at the result of the tokenization: ``` dict_keys(['input_ids', 'attention_mask', 'overflow_to_sample_mapping'])``` As expected, we get input IDs and an attention mask. The last key, `overflow_to_sample_mapping`, is a map that tells us which sentence each of the results corresponds to — here we have 7 results that all come from the (only) sentence we passed the tokenizer: ``` print(inputs["overflow_to_sample_mapping"])``` This is more useful when we tokenize several sentences together. For instance, this: ``` sentences = [ "This sentence is not too long but we are going to split it anyway.", "This sentence is shorter but will still get split.", ] inputs = tokenizer( sentences, truncation=True, return_overflowing_tokens=True, max_length=6, stride=2 ) print(inputs["overflow_to_sample_mapping"])``` gets us: ``` [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1]``` which means the first sentence is split into 7 chunks as before, and the next 4 chunks come from the second sentence. Now let’s go back to our long context. By default the `question-answering` pipeline uses a maximum length of 384, as we mentioned earlier, and a stride of 128, which correspond to the way the model was fine-tuned (you can adjust those parameters by passing `max_seq_len` and `stride` arguments when calling the pipeline). We will thus use those parameters when tokenizing. We’ll also add padding (to have samples of the same length, so we can build tensors) as well as ask for the offsets: ``` inputs = tokenizer( question, long_context, stride=128, max_length=384, padding="longest", truncation="only_second", return_overflowing_tokens=True, return_offsets_mapping=True, )``` Those `inputs` will contain the input IDs and attention masks the model expects, as well as the offsets and the `overflow_to_sample_mapping` we just talked about. Since those two are not parameters used by the model, we’ll pop them out of the `inputs` (and we won’t store the map, since it’s not useful here) before converting it to a tensor: ``` _ = inputs.pop("overflow_to_sample_mapping") offsets = inputs.pop("offset_mapping") inputs = inputs.convert_to_tensors("pt") print(inputs["input_ids"].shape)``` Our long context was split in two, which means that after it goes through our model, we will have two sets of start and end logits: ``` outputs = model(**inputs) start_logits = outputs.start_logits end_logits = outputs.end_logits print(start_logits.shape, end_logits.shape)``` ``` torch.Size([2, 384]) torch.Size([2, 384])``` Like before, we first mask the tokens that are not part of the context before taking the softmax. We also mask all the padding tokens (as flagged by the attention mask): ``` sequence_ids = inputs.sequence_ids() mask = [i != 1 for i in sequence_ids] mask[0] = False mask = torch.logical_or(torch.tensor(mask)[None], (inputs["attention_mask"] == 0)) start_logits[mask] = -10000 end_logits[mask] = -10000``` Then we can use the softmax to convert our logits to probabilities: ``` start_probabilities = torch.nn.functional.softmax(start_logits, dim=-1) end_probabilities = torch.nn.functional.softmax(end_logits, dim=-1)``` The next step is similar to what we did for the small context, but we repeat it for each of our two chunks. We attribute a score to all possible spans of answer, then take the span with the best score: ``` candidates = [] for start_probs, end_probs in zip(start_probabilities, end_probabilities): scores = start_probs[:, None] * end_probs[None, :] idx = torch.triu(scores).argmax().item() start_idx = idx // scores.shape[1] end_idx = idx % scores.shape[1] score = scores[start_idx, end_idx].item() candidates.append((start_idx, end_idx, score)) print(candidates)``` ``` [(0, 18, 0.33867), (173, 184, 0.97149)]``` Those two candidates correspond to the best answers the model was able to find in each chunk. The model is way more confident the right answer is in the second part (which is a good sign!). Now we just have to map those two token spans to spans of characters in the context (we only need to map the second one to have our answer, but it’s interesting to see what the model has picked in the first chunk). ✏️ **Try it out!** Adapt the code above to return the scores and spans for the five most likely answers (in total, not per chunk). The `offsets` we grabbed earlier is actually a list of offsets, with one list per chunk of text: ``` for candidate, offset in zip(candidates, offsets): start_token, end_token, score = candidate start_char, _ = offset[start_token] _, end_char = offset[end_token] answer = long_context[start_char:end_char] result = {"answer": answer, "start": start_char, "end": end_char, "score": score} print(result)``` ``` {'answer': '\n🤗 Transformers: State of the Art NLP', 'start': 0, 'end': 37, 'score': 0.33867} {'answer': 'Jax, PyTorch and TensorFlow', 'start': 1892, 'end': 1919, 'score': 0.97149}``` If we ignore the first result, we get the same result as our pipeline for this long context — yay! ✏️ **Try it out!** Use the best scores you computed before to show the five most likely answers (for the whole context, not each chunk). To check your results, go back to the first pipeline and pass in `top_k=5` when calling it. This concludes our deep dive into the tokenizer’s capabilities. We will put all of this in practice again in the next chapter, when we show you how to fine-tune a model on a range of common NLP tasks.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Fast tokenizers in the QA pipeline - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter6/3b"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Fast tokenizers in the QA pipeline - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;fast-tokenizers-in-the-qa-pipeline&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;using-the-question-answering-pipeline&quot;,&quot;title&quot;:&quot;Using the `question-answering` pipeline&quot;},{&quot;local&quot;:&quot;using-a-model-for-question-answering&quot;,&quot;title&quot;:&quot;Using a model for question answering&quot;},{&quot;local&quot;:&quot;handling-long-contexts&quot;,&quot;title&quot;:&quot;Handling long contexts&quot;}],&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter6/3b&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Fast tokenizers in the QA pipeline</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/2?fw=pt">Training a new tokenizer from an old one </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3?fw=pt">Fast tokenizers' special powers </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter6/3b?fw=pt">Fast tokenizers in the QA pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/4?fw=pt">Normalization and pre-tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/5?fw=pt">Byte-Pair Encoding tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/6?fw=pt">WordPiece tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/7?fw=pt">Unigram tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/8?fw=pt">Building a tokenizer, block by block </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/9?fw=pt">Tokenizers, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="fast-tokenizers-in-the-qa-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fast-tokenizers-in-the-qa-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fast tokenizers in the QA pipeline</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-6-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section3b_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section3b_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>We will now dive into the <code>question-answering</code> pipeline and see how to leverage the offsets to grab the answer to the question at hand from the context, a bit like we did for the grouped entities in the previous section. Then we will see how we can deal with very long contexts that end up being truncated. You can skip this section if you’re not interested in the question answering task.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/_wxyB3j3mk4" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <h2 class="relative group"><a id="using-the-question-answering-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-the-question-answering-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using the <code>question-answering</code> pipeline</span></h2> <p>As we saw in <a href="/course/chapter1">Chapter 1</a>, we can use the <code>question-answering</code> pipeline like this to get the answer to a question:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline question_answerer = pipeline(<span class="hljs-string">"question-answering"</span>) context = <span class="hljs-string">""" 🤗 Transformers is backed by the three most popular deep learning libraries — Jax, PyTorch, and TensorFlow — with a seamless integration between them. It's straightforward to train your models with one before loading them for inference with the other. """</span> question = <span class="hljs-string">"Which deep learning libraries back 🤗 Transformers?"</span> question_answerer(question=question, context=context)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'score'</span>: <span class="hljs-number">0.97773</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">78</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">105</span>, <span class="hljs-string">'answer'</span>: <span class="hljs-string">'Jax, PyTorch and TensorFlow'</span>}</pre></div> <p>Unlike the other pipelines, which can’t truncate and split texts that are longer than the maximum length accepted by the model (and thus may miss information at the end of a document), this pipeline can deal with very long contexts and will return the answer to the question even if it’s at the end:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>long_context = <span class="hljs-string">""" 🤗 Transformers: State of the Art NLP 🤗 Transformers provides thousands of pretrained models to perform tasks on texts such as classification, information extraction, question answering, summarization, translation, text generation and more in over 100 languages. Its aim is to make cutting-edge NLP easier to use for everyone. 🤗 Transformers provides APIs to quickly download and use those pretrained models on a given text, fine-tune them on your own datasets and then share them with the community on our model hub. At the same time, each python module defining an architecture is fully standalone and can be modified to enable quick research experiments. Why should I use transformers? 1. Easy-to-use state-of-the-art models: - High performance on NLU and NLG tasks. - Low barrier to entry for educators and practitioners. - Few user-facing abstractions with just three classes to learn. - A unified API for using all our pretrained models. - Lower compute costs, smaller carbon footprint: 2. Researchers can share trained models instead of always retraining. - Practitioners can reduce compute time and production costs. - Dozens of architectures with over 10,000 pretrained models, some in more than 100 languages. 3. Choose the right framework for every part of a model's lifetime: - Train state-of-the-art models in 3 lines of code. - Move a single model between TF2.0/PyTorch frameworks at will. - Seamlessly pick the right framework for training, evaluation and production. 4. Easily customize a model or an example to your needs: - We provide examples for each architecture to reproduce the results published by its original authors. - Model internals are exposed as consistently as possible. - Model files can be used independently of the library for quick experiments. 🤗 Transformers is backed by the three most popular deep learning libraries — Jax, PyTorch and TensorFlow — with a seamless integration between them. It's straightforward to train your models with one before loading them for inference with the other. """</span> question_answerer(question=question, context=long_context)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'score'</span>: <span class="hljs-number">0.97149</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">1892</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">1919</span>, <span class="hljs-string">'answer'</span>: <span class="hljs-string">'Jax, PyTorch and TensorFlow'</span>}</pre></div> <p>Let’s see how it does all of this!</p> <h2 class="relative group"><a id="using-a-model-for-question-answering" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-a-model-for-question-answering"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using a model for question answering</span></h2> <p>Like with any other pipeline, we start by tokenizing our input and then send it through the model. The checkpoint used by default for the <code>question-answering</code> pipeline is <a href="https://huggingface.co/distilbert-base-cased-distilled-squad" rel="nofollow"><code>distilbert-base-cased-distilled-squad</code></a> (the “squad” in the name comes from the dataset on which the model was fine-tuned; we’ll talk more about the SQuAD dataset in <a href="/course/chapter7/7">Chapter 7</a>):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForQuestionAnswering model_checkpoint = <span class="hljs-string">"distilbert-base-cased-distilled-squad"</span> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) model = AutoModelForQuestionAnswering.from_pretrained(model_checkpoint) inputs = tokenizer(question, context, return_tensors=<span class="hljs-string">"pt"</span>) outputs = model(**inputs)</pre></div> <p>Note that we tokenize the question and the context as a pair, with the question first.</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/question_tokens.svg" alt="An example of tokenization of question and context"> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/question_tokens-dark.svg" alt="An example of tokenization of question and context"></div> <p>Models for question answering work a little differently from the models we’ve seen up to now. Using the picture above as an example, the model has been trained to predict the index of the token starting the answer (here 21) and the index of the token where the answer ends (here 24). This is why those models don’t return one tensor of logits but two: one for the logits corresponding to the start token of the answer, and one for the logits corresponding to the end token of the answer. Since in this case we have only one input containing 66 tokens, we get:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>start_logits = outputs.start_logits end_logits = outputs.end_logits <span class="hljs-built_in">print</span>(start_logits.shape, end_logits.shape)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>torch.Size([<span class="hljs-number">1</span>, <span class="hljs-number">66</span>]) torch.Size([<span class="hljs-number">1</span>, <span class="hljs-number">66</span>])</pre></div> <p>To convert those logits into probabilities, we will apply a softmax function — but before that, we need to make sure we mask the indices that are not part of the context. Our input is <code>[CLS] question [SEP] context [SEP]</code>, so we need to mask the tokens of the question as well as the <code>[SEP]</code> token. We’ll keep the <code>[CLS]</code> token, however, as some models use it to indicate that the answer is not in the context.</p> <p>Since we will apply a softmax afterward, we just need to replace the logits we want to mask with a large negative number. Here, we use <code>-10000</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch sequence_ids = inputs.sequence_ids() <span class="hljs-comment"># Mask everything apart from the tokens of the context</span> mask = [i != <span class="hljs-number">1</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> sequence_ids] <span class="hljs-comment"># Unmask the [CLS] token</span> mask[<span class="hljs-number">0</span>] = <span class="hljs-literal">False</span> mask = torch.tensor(mask)[<span class="hljs-literal">None</span>] start_logits[mask] = -<span class="hljs-number">10000</span> end_logits[mask] = -<span class="hljs-number">10000</span></pre></div> <p>Now that we have properly masked the logits corresponding to positions we don’t want to predict, we can apply the softmax:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>start_probabilities = torch.nn.functional.softmax(start_logits, dim=-<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>] end_probabilities = torch.nn.functional.softmax(end_logits, dim=-<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>]</pre></div> <p>At this stage, we could take the argmax of the start and end probabilities — but we might end up with a start index that is greater than the end index, so we need to take a few more precautions. We will compute the probabilities of each possible <code>start_index</code> and <code>end_index</code> where <code>start_index &lt;= end_index</code>, then take the tuple <code>(start_index, end_index)</code> with the highest probability.</p> <p>Assuming the events “The answer starts at <code>start_index</code>” and “The answer ends at <code>end_index</code>” to be independent, the probability that the answer starts at <code>start_index</code> and ends at <code>end_index</code> is: <span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mrow><mi mathvariant="normal">s</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">p</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">b</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">b</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">l</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">s</mi></mrow><mo stretchy="false">[</mo><mrow><mi mathvariant="normal">s</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">x</mi></mrow><mo stretchy="false">]</mo><mo>×</mo><mrow><mi mathvariant="normal">e</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">p</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">b</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">b</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">l</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">s</mi></mrow><mo stretchy="false">[</mo><mrow><mi mathvariant="normal">e</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">x</mi></mrow><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">\mathrm{start\_probabilities}[\mathrm{start\_index}] \times \mathrm{end\_probabilities}[\mathrm{end\_index}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.06em;vertical-align:-0.31em;"></span><span class="mord"><span class="mord mathrm">start_probabilities</span></span><span class="mopen">[</span><span class="mord"><span class="mord mathrm">start_index</span></span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1.06em;vertical-align:-0.31em;"></span><span class="mord"><span class="mord mathrm">end_probabilities</span></span><span class="mopen">[</span><span class="mord"><span class="mord mathrm">end_index</span></span><span class="mclose">]</span></span></span></span></span></p> <p>So, to compute all the scores, we just need to compute all the products <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi mathvariant="normal">s</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">p</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">b</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">b</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">l</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">s</mi></mrow><mo stretchy="false">[</mo><mrow><mi mathvariant="normal">s</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">x</mi></mrow><mo stretchy="false">]</mo><mo>×</mo><mrow><mi mathvariant="normal">e</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">p</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">b</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">b</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">l</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">s</mi></mrow><mo stretchy="false">[</mo><mrow><mi mathvariant="normal">e</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">x</mi></mrow><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">\mathrm{start\_probabilities}[\mathrm{start\_index}] \times \mathrm{end\_probabilities}[\mathrm{end\_index}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.06em;vertical-align:-0.31em;"></span><span class="mord"><span class="mord mathrm">start_probabilities</span></span><span class="mopen">[</span><span class="mord"><span class="mord mathrm">start_index</span></span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1.06em;vertical-align:-0.31em;"></span><span class="mord"><span class="mord mathrm">end_probabilities</span></span><span class="mopen">[</span><span class="mord"><span class="mord mathrm">end_index</span></span><span class="mclose">]</span></span></span></span> where <code>start_index &lt;= end_index</code>.</p> <p>First let’s compute all the possible products:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>scores = start_probabilities[:, <span class="hljs-literal">None</span>] * end_probabilities[<span class="hljs-literal">None</span>, :]</pre></div> <p>Then we’ll mask the values where <code>start_index &gt; end_index</code> by setting them to <code>0</code> (the other probabilities are all positive numbers). The <code>torch.triu()</code> function returns the upper triangular part of the 2D tensor passed as an argument, so it will do that masking for us:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>scores = torch.triu(scores)</pre></div> <p>Now we just have to get the index of the maximum. Since PyTorch will return the index in the flattened tensor, we need to use the floor division <code>//</code> and modulus <code>%</code> operations to get the <code>start_index</code> and <code>end_index</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>max_index = scores.argmax().item() start_index = max_index // scores.shape[<span class="hljs-number">1</span>] end_index = max_index % scores.shape[<span class="hljs-number">1</span>] <span class="hljs-built_in">print</span>(scores[start_index, end_index])</pre></div> <p>We’re not quite done yet, but at least we already have the correct score for the answer (you can check this by comparing it to the first result in the previous section):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">0.97773</span></pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Compute the start and end indices for the five most likely answers.</p></div> <p>We have the <code>start_index</code> and <code>end_index</code> of the answer in terms of tokens, so now we just need to convert to the character indices in the context. This is where the offsets will be super useful. We can grab them and use them like we did in the token classification task:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs_with_offsets = tokenizer(question, context, return_offsets_mapping=<span class="hljs-literal">True</span>) offsets = inputs_with_offsets[<span class="hljs-string">"offset_mapping"</span>] start_char, _ = offsets[start_index] _, end_char = offsets[end_index] answer = context[start_char:end_char]</pre></div> <p>Now we just have to format everything to get our result:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>result = { <span class="hljs-string">"answer"</span>: answer, <span class="hljs-string">"start"</span>: start_char, <span class="hljs-string">"end"</span>: end_char, <span class="hljs-string">"score"</span>: scores[start_index, end_index], } <span class="hljs-built_in">print</span>(result)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'answer'</span>: <span class="hljs-string">'Jax, PyTorch and TensorFlow'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">78</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">105</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.97773</span>}</pre></div> <p>Great! That’s the same as in our first example!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Use the best scores you computed earlier to show the five most likely answers. To check your results, go back to the first pipeline and pass in <code>top_k=5</code> when calling it.</p></div> <h2 class="relative group"><a id="handling-long-contexts" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#handling-long-contexts"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Handling long contexts</span></h2> <p>If we try to tokenize the question and long context we used as an example previously, we’ll get a number of tokens higher than the maximum length used in the <code>question-answering</code> pipeline (which is 384):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs = tokenizer(question, long_context) <span class="hljs-built_in">print</span>(<span class="hljs-built_in">len</span>(inputs[<span class="hljs-string">"input_ids"</span>]))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">461</span></pre></div> <p>So, we’ll need to truncate our inputs at that maximum length. There are several ways we can do this, but we don’t want to truncate the question, only the context. Since the context is the second sentence, we’ll use the <code>"only_second"</code> truncation strategy. The problem that arises then is that the answer to the question may not be in the truncated context. Here, for instance, we picked a question where the answer is toward the end of the context, and when we truncate it that answer is not present:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs = tokenizer(question, long_context, max_length=<span class="hljs-number">384</span>, truncation=<span class="hljs-string">"only_second"</span>) <span class="hljs-built_in">print</span>(tokenizer.decode(inputs[<span class="hljs-string">"input_ids"</span>]))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">""" [CLS] Which deep learning libraries back [UNK] Transformers? [SEP] [UNK] Transformers : State of the Art NLP [UNK] Transformers provides thousands of pretrained models to perform tasks on texts such as classification, information extraction, question answering, summarization, translation, text generation and more in over 100 languages. Its aim is to make cutting-edge NLP easier to use for everyone. [UNK] Transformers provides APIs to quickly download and use those pretrained models on a given text, fine-tune them on your own datasets and then share them with the community on our model hub. At the same time, each python module defining an architecture is fully standalone and can be modified to enable quick research experiments. Why should I use transformers? 1. Easy-to-use state-of-the-art models: - High performance on NLU and NLG tasks. - Low barrier to entry for educators and practitioners. - Few user-facing abstractions with just three classes to learn. - A unified API for using all our pretrained models. - Lower compute costs, smaller carbon footprint: 2. Researchers can share trained models instead of always retraining. - Practitioners can reduce compute time and production costs. - Dozens of architectures with over 10,000 pretrained models, some in more than 100 languages. 3. Choose the right framework for every part of a model's lifetime: - Train state-of-the-art models in 3 lines of code. - Move a single model between TF2.0/PyTorch frameworks at will. - Seamlessly pick the right framework for training, evaluation and production. 4. Easily customize a model or an example to your needs: - We provide examples for each architecture to reproduce the results published by its original authors. - Model internal [SEP] """</span></pre></div> <p>This means the model will have a hard time picking the correct answer. To fix this, the <code>question-answering</code> pipeline allows us to split the context into smaller chunks, specifying the maximum length. To make sure we don’t split the context at exactly the wrong place to make it possible to find the answer, it also includes some overlap between the chunks.</p> <p>We can have the tokenizer (fast or slow) do this for us by adding <code>return_overflowing_tokens=True</code>, and we can specify the overlap we want with the <code>stride</code> argument. Here is an example, using a smaller sentence:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sentence = <span class="hljs-string">"This sentence is not too long but we are going to split it anyway."</span> inputs = tokenizer( sentence, truncation=<span class="hljs-literal">True</span>, return_overflowing_tokens=<span class="hljs-literal">True</span>, max_length=<span class="hljs-number">6</span>, stride=<span class="hljs-number">2</span> ) <span class="hljs-keyword">for</span> ids <span class="hljs-keyword">in</span> inputs[<span class="hljs-string">"input_ids"</span>]: <span class="hljs-built_in">print</span>(tokenizer.decode(ids))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'[CLS] This sentence is not [SEP]'</span> <span class="hljs-string">'[CLS] is not too long [SEP]'</span> <span class="hljs-string">'[CLS] too long but we [SEP]'</span> <span class="hljs-string">'[CLS] but we are going [SEP]'</span> <span class="hljs-string">'[CLS] are going to split [SEP]'</span> <span class="hljs-string">'[CLS] to split it anyway [SEP]'</span> <span class="hljs-string">'[CLS] it anyway. [SEP]'</span></pre></div> <p>As we can see, the sentence has been split into chunks in such a way that each entry in <code>inputs["input_ids"]</code> has at most 6 tokens (we would need to add padding to have the last entry be the same size as the others) and there is an overlap of 2 tokens between each of the entries.</p> <p>Let’s take a closer look at the result of the tokenization:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(inputs.keys())</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>dict_keys([<span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'overflow_to_sample_mapping'</span>])</pre></div> <p>As expected, we get input IDs and an attention mask. The last key, <code>overflow_to_sample_mapping</code>, is a map that tells us which sentence each of the results corresponds to — here we have 7 results that all come from the (only) sentence we passed the tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(inputs[<span class="hljs-string">"overflow_to_sample_mapping"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]</pre></div> <p>This is more useful when we tokenize several sentences together. For instance, this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sentences = [ <span class="hljs-string">"This sentence is not too long but we are going to split it anyway."</span>, <span class="hljs-string">"This sentence is shorter but will still get split."</span>, ] inputs = tokenizer( sentences, truncation=<span class="hljs-literal">True</span>, return_overflowing_tokens=<span class="hljs-literal">True</span>, max_length=<span class="hljs-number">6</span>, stride=<span class="hljs-number">2</span> ) <span class="hljs-built_in">print</span>(inputs[<span class="hljs-string">"overflow_to_sample_mapping"</span>])</pre></div> <p>gets us:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]</pre></div> <p>which means the first sentence is split into 7 chunks as before, and the next 4 chunks come from the second sentence.</p> <p>Now let’s go back to our long context. By default the <code>question-answering</code> pipeline uses a maximum length of 384, as we mentioned earlier, and a stride of 128, which correspond to the way the model was fine-tuned (you can adjust those parameters by passing <code>max_seq_len</code> and <code>stride</code> arguments when calling the pipeline). We will thus use those parameters when tokenizing. We’ll also add padding (to have samples of the same length, so we can build tensors) as well as ask for the offsets:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs = tokenizer( question, long_context, stride=<span class="hljs-number">128</span>, max_length=<span class="hljs-number">384</span>, padding=<span class="hljs-string">"longest"</span>, truncation=<span class="hljs-string">"only_second"</span>, return_overflowing_tokens=<span class="hljs-literal">True</span>, return_offsets_mapping=<span class="hljs-literal">True</span>, )</pre></div> <p>Those <code>inputs</code> will contain the input IDs and attention masks the model expects, as well as the offsets and the <code>overflow_to_sample_mapping</code> we just talked about. Since those two are not parameters used by the model, we’ll pop them out of the <code>inputs</code> (and we won’t store the map, since it’s not useful here) before converting it to a tensor:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>_ = inputs.pop(<span class="hljs-string">"overflow_to_sample_mapping"</span>) offsets = inputs.pop(<span class="hljs-string">"offset_mapping"</span>) inputs = inputs.convert_to_tensors(<span class="hljs-string">"pt"</span>) <span class="hljs-built_in">print</span>(inputs[<span class="hljs-string">"input_ids"</span>].shape)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>torch.Size([<span class="hljs-number">2</span>, <span class="hljs-number">384</span>])</pre></div> <p>Our long context was split in two, which means that after it goes through our model, we will have two sets of start and end logits:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>outputs = model(**inputs) start_logits = outputs.start_logits end_logits = outputs.end_logits <span class="hljs-built_in">print</span>(start_logits.shape, end_logits.shape)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>torch.Size([<span class="hljs-number">2</span>, <span class="hljs-number">384</span>]) torch.Size([<span class="hljs-number">2</span>, <span class="hljs-number">384</span>])</pre></div> <p>Like before, we first mask the tokens that are not part of the context before taking the softmax. We also mask all the padding tokens (as flagged by the attention mask):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sequence_ids = inputs.sequence_ids() <span class="hljs-comment"># Mask everything apart from the tokens of the context</span> mask = [i != <span class="hljs-number">1</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> sequence_ids] <span class="hljs-comment"># Unmask the [CLS] token</span> mask[<span class="hljs-number">0</span>] = <span class="hljs-literal">False</span> <span class="hljs-comment"># Mask all the [PAD] tokens</span> mask = torch.logical_or(torch.tensor(mask)[<span class="hljs-literal">None</span>], (inputs[<span class="hljs-string">"attention_mask"</span>] == <span class="hljs-number">0</span>)) start_logits[mask] = -<span class="hljs-number">10000</span> end_logits[mask] = -<span class="hljs-number">10000</span></pre></div> <p>Then we can use the softmax to convert our logits to probabilities:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>start_probabilities = torch.nn.functional.softmax(start_logits, dim=-<span class="hljs-number">1</span>) end_probabilities = torch.nn.functional.softmax(end_logits, dim=-<span class="hljs-number">1</span>)</pre></div> <p>The next step is similar to what we did for the small context, but we repeat it for each of our two chunks. We attribute a score to all possible spans of answer, then take the span with the best score:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>candidates = [] <span class="hljs-keyword">for</span> start_probs, end_probs <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(start_probabilities, end_probabilities): scores = start_probs[:, <span class="hljs-literal">None</span>] * end_probs[<span class="hljs-literal">None</span>, :] idx = torch.triu(scores).argmax().item() start_idx = idx // scores.shape[<span class="hljs-number">1</span>] end_idx = idx % scores.shape[<span class="hljs-number">1</span>] score = scores[start_idx, end_idx].item() candidates.append((start_idx, end_idx, score)) <span class="hljs-built_in">print</span>(candidates)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[(<span class="hljs-number">0</span>, <span class="hljs-number">18</span>, <span class="hljs-number">0.33867</span>), (<span class="hljs-number">173</span>, <span class="hljs-number">184</span>, <span class="hljs-number">0.97149</span>)]</pre></div> <p>Those two candidates correspond to the best answers the model was able to find in each chunk. The model is way more confident the right answer is in the second part (which is a good sign!). Now we just have to map those two token spans to spans of characters in the context (we only need to map the second one to have our answer, but it’s interesting to see what the model has picked in the first chunk).</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Adapt the code above to return the scores and spans for the five most likely answers (in total, not per chunk).</p></div> <p>The <code>offsets</code> we grabbed earlier is actually a list of offsets, with one list per chunk of text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> candidate, offset <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(candidates, offsets): start_token, end_token, score = candidate start_char, _ = offset[start_token] _, end_char = offset[end_token] answer = long_context[start_char:end_char] result = {<span class="hljs-string">"answer"</span>: answer, <span class="hljs-string">"start"</span>: start_char, <span class="hljs-string">"end"</span>: end_char, <span class="hljs-string">"score"</span>: score} <span class="hljs-built_in">print</span>(result)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'answer'</span>: <span class="hljs-string">'\n🤗 Transformers: State of the Art NLP'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">37</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.33867</span>} {<span class="hljs-string">'answer'</span>: <span class="hljs-string">'Jax, PyTorch and TensorFlow'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">1892</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">1919</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.97149</span>}</pre></div> <p>If we ignore the first result, we get the same result as our pipeline for this long context — yay!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Use the best scores you computed before to show the five most likely answers (for the whole context, not each chunk). To check your results, go back to the first pipeline and pass in <code>top_k=5</code> when calling it.</p></div> <p>This concludes our deep dive into the tokenizer’s capabilities. We will put all of this in practice again in the next chapter, when we show you how to fine-tune a model on a range of common NLP tasks.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter6/3?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Fast tokenizers' special powers</a> <a href="/learn/nlp-course/chapter6/4?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Normalization and pre-tokenization<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;fast-tokenizers-in-the-qa-pipeline&quot;,&quot;url&quot;:&quot;#fast-tokenizers-in-the-qa-pipeline&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Using the `question-answering` pipeline&quot;,&quot;id&quot;:&quot;using-the-question-answering-pipeline&quot;,&quot;url&quot;:&quot;#using-the-question-answering-pipeline&quot;},{&quot;title&quot;:&quot;Using a model for question answering&quot;,&quot;id&quot;:&quot;using-a-model-for-question-answering&quot;,&quot;url&quot;:&quot;#using-a-model-for-question-answering&quot;},{&quot;title&quot;:&quot;Handling long contexts&quot;,&quot;id&quot;:&quot;handling-long-contexts&quot;,&quot;url&quot;:&quot;#handling-long-contexts&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#fast-tokenizers-in-the-qa-pipeline" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fast-tokenizers-in-the-qa-pipeline"><wbr>Fast tokenizers in the Q<wbr>A pipeline</a> <a href="#using-the-question-answering-pipeline" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-the-question-answering-pipeline"><wbr>Using the `question-answering` pipeline</a> <a href="#using-a-model-for-question-answering" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-a-model-for-question-answering"><wbr>Using a model for question answering</a> <a href="#handling-long-contexts" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-handling-long-contexts"><wbr>Handling long contexts</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter6/3b" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter6/3b"); } </script> <iframe name="__privateStripeMetricsController2440" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter6%2F3b%3Ffw%3Dpt&amp;title=Fast%20tokenizers%20in%20the%20QA%20pipeline%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:21.876Z
Training a new tokenizer from an old one - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter6/2?fw=pt
## [](#training-a-new-tokenizer-from-an-old-one)Training a new tokenizer from an old one [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-6-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section2.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section2.ipynb) If a language model is not available in the language you are interested in, or if your corpus is very different from the one your language model was trained on, you will most likely want to retrain the model from scratch using a tokenizer adapted to your data. That will require training a new tokenizer on your dataset. But what exactly does that mean? When we first looked at tokenizers in [Chapter 2](/course/chapter2), we saw that most Transformer models use a _subword tokenization algorithm_. To identify which subwords are of interest and occur most frequently in the corpus at hand, the tokenizer needs to take a hard look at all the texts in the corpus — a process we call _training_. The exact rules that govern this training depend on the type of tokenizer used, and we’ll go over the three main algorithms later in this chapter. ⚠️ Training a tokenizer is not the same as training a model! Model training uses stochastic gradient descent to make the loss a little bit smaller for each batch. It’s randomized by nature (meaning you have to set some seeds to get the same results when doing the same training twice). Training a tokenizer is a statistical process that tries to identify which subwords are the best to pick for a given corpus, and the exact rules used to pick them depend on the tokenization algorithm. It’s deterministic, meaning you always get the same results when training with the same algorithm on the same corpus. ## [](#assembling-a-corpus)Assembling a corpus There’s a very simple API in 🤗 Transformers that you can use to train a new tokenizer with the same characteristics as an existing one: `AutoTokenizer.train_new_from_iterator()`. To see this in action, let’s say we want to train GPT-2 from scratch, but in a language other than English. Our first task will be to gather lots of data in that language in a training corpus. To provide examples everyone will be able to understand, we won’t use a language like Russian or Chinese here, but rather a specialized English language: Python code. The [🤗 Datasets](https://github.com/huggingface/datasets) library can help us assemble a corpus of Python source code. We’ll use the usual `load_dataset()` function to download and cache the [CodeSearchNet](https://huggingface.co/datasets/code_search_net) dataset. This dataset was created for the [CodeSearchNet challenge](https://wandb.ai/github/CodeSearchNet/benchmark) and contains millions of functions from open source libraries on GitHub in several programming languages. Here, we will load the Python part of this dataset: ``` from datasets import load_dataset raw_datasets = load_dataset("code_search_net", "python")``` We can have a look at the training split to see which columns we have access to: ``` Dataset({ features: ['repository_name', 'func_path_in_repository', 'func_name', 'whole_func_string', 'language', 'func_code_string', 'func_code_tokens', 'func_documentation_string', 'func_documentation_tokens', 'split_name', 'func_code_url' ], num_rows: 412178 })``` We can see the dataset separates docstrings from code and suggests a tokenization of both. Here. we’ll just use the `whole_func_string` column to train our tokenizer. We can look at an example of one these functions by indexing into the `train` split: ``` print(raw_datasets["train"][123456]["whole_func_string"])``` which should print the following: ``` def handle_simple_responses( self, timeout_ms=None, info_cb=DEFAULT_MESSAGE_CALLBACK): """Accepts normal responses from the device. Args: timeout_ms: Timeout in milliseconds to wait for each response. info_cb: Optional callback for text sent from the bootloader. Returns: OKAY packet's message. """ return self._accept_responses('OKAY', info_cb, timeout_ms=timeout_ms)``` The first thing we need to do is transform the dataset into an _iterator_ of lists of texts — for instance, a list of list of texts. Using lists of texts will enable our tokenizer to go faster (training on batches of texts instead of processing individual texts one by one), and it should be an iterator if we want to avoid having everything in memory at once. If your corpus is huge, you will want to take advantage of the fact that 🤗 Datasets does not load everything into RAM but stores the elements of the dataset on disk. Doing the following would create a list of lists of 1,000 texts each, but would load everything in memory: Using a Python generator, we can avoid Python loading anything into memory until it’s actually necessary. To create such a generator, you just to need to replace the brackets with parentheses: ``` training_corpus = ( raw_datasets["train"][i : i + 1000]["whole_func_string"] for i in range(0, len(raw_datasets["train"]), 1000) )``` This line of code doesn’t fetch any elements of the dataset; it just creates an object you can use in a Python `for` loop. The texts will only be loaded when you need them (that is, when you’re at the step of the `for` loop that requires them), and only 1,000 texts at a time will be loaded. This way you won’t exhaust all your memory even if you are processing a huge dataset. The problem with a generator object is that it can only be used once. So, instead of this giving us the list of the first 10 digits twice: ``` gen = (i for i in range(10)) print(list(gen)) print(list(gen))``` we get them once and then an empty list: ``` [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] []``` That’s why we define a function that returns a generator instead: ``` def get_training_corpus(): return ( raw_datasets["train"][i : i + 1000]["whole_func_string"] for i in range(0, len(raw_datasets["train"]), 1000) ) training_corpus = get_training_corpus()``` You can also define your generator inside a `for` loop by using the `yield` statement: ``` def get_training_corpus(): dataset = raw_datasets["train"] for start_idx in range(0, len(dataset), 1000): samples = dataset[start_idx : start_idx + 1000] yield samples["whole_func_string"]``` which will produce the exact same generator as before, but allows you to use more complex logic than you can in a list comprehension. ## [](#training-a-new-tokenizer)Training a new tokenizer Now that we have our corpus in the form of an iterator of batches of texts, we are ready to train a new tokenizer. To do this, we first need to load the tokenizer we want to pair with our model (here, GPT-2): ``` from transformers import AutoTokenizer old_tokenizer = AutoTokenizer.from_pretrained("gpt2")``` Even though we are going to train a new tokenizer, it’s a good idea to do this to avoid starting entirely from scratch. This way, we won’t have to specify anything about the tokenization algorithm or the special tokens we want to use; our new tokenizer will be exactly the same as GPT-2, and the only thing that will change is the vocabulary, which will be determined by the training on our corpus. First let’s have a look at how this tokenizer would treat an example function: ``` example = '''def add_numbers(a, b): """Add the two numbers `a` and `b`.""" return a + b''' tokens = old_tokenizer.tokenize(example) tokens``` ``` ['def', 'Ġadd', '_', 'n', 'umbers', '(', 'a', ',', 'Ġb', '):', 'Ċ', 'Ġ', 'Ġ', 'Ġ', 'Ġ"""', 'Add', 'Ġthe', 'Ġtwo', 'Ġnumbers', 'Ġ`', 'a', '`', 'Ġand', 'Ġ`', 'b', '`', '."', '""', 'Ċ', 'Ġ', 'Ġ', 'Ġ', 'Ġreturn', 'Ġa', 'Ġ+', 'Ġb']``` This tokenizer has a few special symbols, like `Ġ` and `Ċ`, which denote spaces and newlines, respectively. As we can see, this is not too efficient: the tokenizer returns individual tokens for each space, when it could group together indentation levels (since having sets of four or eight spaces is going to be very common in code). It also split the function name a bit weirdly, not being used to seeing words with the `_` character. Let’s train a new tokenizer and see if it solves those issues. For this, we’ll use the method `train_new_from_iterator()`: ``` tokenizer = old_tokenizer.train_new_from_iterator(training_corpus, 52000)``` This command might take a bit of time if your corpus is very large, but for this dataset of 1.6 GB of texts it’s blazing fast (1 minute 16 seconds on an AMD Ryzen 9 3900X CPU with 12 cores). Note that `AutoTokenizer.train_new_from_iterator()` only works if the tokenizer you are using is a “fast” tokenizer. As you’ll see in the next section, the 🤗 Transformers library contains two types of tokenizers: some are written purely in Python and others (the fast ones) are backed by the 🤗 Tokenizers library, which is written in the [Rust](https://www.rust-lang.org/) programming language. Python is the language most often used for data science and deep learning applications, but when anything needs to be parallelized to be fast, it has to be written in another language. For instance, the matrix multiplications that are at the core of the model computation are written in CUDA, an optimized C library for GPUs. Training a brand new tokenizer in pure Python would be excruciatingly slow, which is why we developed the 🤗 Tokenizers library. Note that just as you didn’t have to learn the CUDA language to be able to execute your model on a batch of inputs on a GPU, you won’t need to learn Rust to use a fast tokenizer. The 🤗 Tokenizers library provides Python bindings for many methods that internally call some piece of code in Rust; for example, to parallelize the training of your new tokenizer or, as we saw in [Chapter 3](/course/chapter3), the tokenization of a batch of inputs. Most of the Transformer models have a fast tokenizer available (there are some exceptions that you can check [here](https://huggingface.co/transformers/#supported-frameworks)), and the `AutoTokenizer` API always selects the fast tokenizer for you if it’s available. In the next section we’ll take a look at some of the other special features fast tokenizers have, which will be really useful for tasks like token classification and question answering. Before diving into that, however, let’s try our brand new tokenizer on the previous example: ``` tokens = tokenizer.tokenize(example) tokens``` ``` ['def', 'Ġadd', '_', 'numbers', '(', 'a', ',', 'Ġb', '):', 'ĊĠĠĠ', 'Ġ"""', 'Add', 'Ġthe', 'Ġtwo', 'Ġnumbers', 'Ġ`', 'a', '`', 'Ġand', 'Ġ`', 'b', '`."""', 'ĊĠĠĠ', 'Ġreturn', 'Ġa', 'Ġ+', 'Ġb']``` Here we again see the special symbols `Ġ` and `Ċ` that denote spaces and newlines, but we can also see that our tokenizer learned some tokens that are highly specific to a corpus of Python functions: for example, there is a `ĊĠĠĠ` token that represents an indentation, and a `Ġ"""` token that represents the three quotes that start a docstring. The tokenizer also correctly split the function name on `_`. This is quite a compact representation; comparatively, using the plain English tokenizer on the same example will give us a longer sentence: ``` print(len(tokens)) print(len(old_tokenizer.tokenize(example)))``` Let’s look at another example: ``` example = """class LinearLayer(): def __init__(self, input_size, output_size): self.weight = torch.randn(input_size, output_size) self.bias = torch.zeros(output_size) def __call__(self, x): return x @ self.weights + self.bias """ tokenizer.tokenize(example)``` ``` ['class', 'ĠLinear', 'Layer', '():', 'ĊĠĠĠ', 'Ġdef', 'Ġ__', 'init', '__(', 'self', ',', 'Ġinput', '_', 'size', ',', 'Ġoutput', '_', 'size', '):', 'ĊĠĠĠĠĠĠĠ', 'Ġself', '.', 'weight', 'Ġ=', 'Ġtorch', '.', 'randn', '(', 'input', '_', 'size', ',', 'Ġoutput', '_', 'size', ')', 'ĊĠĠĠĠĠĠĠ', 'Ġself', '.', 'bias', 'Ġ=', 'Ġtorch', '.', 'zeros', '(', 'output', '_', 'size', ')', 'ĊĊĠĠĠ', 'Ġdef', 'Ġ__', 'call', '__(', 'self', ',', 'Ġx', '):', 'ĊĠĠĠĠĠĠĠ', 'Ġreturn', 'Ġx', 'Ġ@', 'Ġself', '.', 'weights', 'Ġ+', 'Ġself', '.', 'bias', 'ĊĠĠĠĠ']``` In addition to the token corresponding to an indentation, here we can also see a token for a double indentation: `ĊĠĠĠĠĠĠĠ`. The special Python words like `class`, `init`, `call`, `self`, and `return` are each tokenized as one token, and we can see that as well as splitting on `_` and `.` the tokenizer correctly splits even camel-cased names: `LinearLayer` is tokenized as `["ĠLinear", "Layer"]`. ## [](#saving-the-tokenizer)Saving the tokenizer To make sure we can use it later, we need to save our new tokenizer. Like for models, this is done with the `save_pretrained()` method: ``` tokenizer.save_pretrained("code-search-net-tokenizer")``` This will create a new folder named _code-search-net-tokenizer_, which will contain all the files the tokenizer needs to be reloaded. If you want to share this tokenizer with your colleagues and friends, you can upload it to the Hub by logging into your account. If you’re working in a notebook, there’s a convenience function to help you with this: ``` from huggingface_hub import notebook_login notebook_login()``` This will display a widget where you can enter your Hugging Face login credentials. If you aren’t working in a notebook, just type the following line in your terminal: Once you’ve logged in, you can push your tokenizer by executing the following command: ``` tokenizer.push_to_hub("code-search-net-tokenizer")``` This will create a new repository in your namespace with the name `code-search-net-tokenizer`, containing the tokenizer file. You can then load the tokenizer from anywhere with the `from_pretrained()` method: ``` tokenizer = AutoTokenizer.from_pretrained("huggingface-course/code-search-net-tokenizer")``` You’re now all set for training a language model from scratch and fine-tuning it on your task at hand! We’ll get to that in [Chapter 7](/course/chapter7), but first, in the rest of this chapter we’ll take a closer look at fast tokenizers and explore in detail what actually happens when we call the method `train_new_from_iterator()`.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Training a new tokenizer from an old one - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter6/2"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Training a new tokenizer from an old one - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;training-a-new-tokenizer-from-an-old-one&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;assembling-a-corpus&quot;,&quot;title&quot;:&quot;Assembling a corpus&quot;},{&quot;local&quot;:&quot;training-a-new-tokenizer&quot;,&quot;title&quot;:&quot;Training a new tokenizer&quot;},{&quot;local&quot;:&quot;saving-the-tokenizer&quot;,&quot;title&quot;:&quot;Saving the tokenizer&quot;}],&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter6/2&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Training a new tokenizer from an old one</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/1?fw=pt">Introduction </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter6/2?fw=pt">Training a new tokenizer from an old one </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3?fw=pt">Fast tokenizers' special powers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3b?fw=pt">Fast tokenizers in the QA pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/4?fw=pt">Normalization and pre-tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/5?fw=pt">Byte-Pair Encoding tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/6?fw=pt">WordPiece tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/7?fw=pt">Unigram tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/8?fw=pt">Building a tokenizer, block by block </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/9?fw=pt">Tokenizers, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="training-a-new-tokenizer-from-an-old-one" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-a-new-tokenizer-from-an-old-one"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training a new tokenizer from an old one</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-6-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section2.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section2.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>If a language model is not available in the language you are interested in, or if your corpus is very different from the one your language model was trained on, you will most likely want to retrain the model from scratch using a tokenizer adapted to your data. That will require training a new tokenizer on your dataset. But what exactly does that mean? When we first looked at tokenizers in <a href="/course/chapter2">Chapter 2</a>, we saw that most Transformer models use a <em>subword tokenization algorithm</em>. To identify which subwords are of interest and occur most frequently in the corpus at hand, the tokenizer needs to take a hard look at all the texts in the corpus — a process we call <em>training</em>. The exact rules that govern this training depend on the type of tokenizer used, and we’ll go over the three main algorithms later in this chapter.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/DJimQynXZsQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>⚠️ Training a tokenizer is not the same as training a model! Model training uses stochastic gradient descent to make the loss a little bit smaller for each batch. It’s randomized by nature (meaning you have to set some seeds to get the same results when doing the same training twice). Training a tokenizer is a statistical process that tries to identify which subwords are the best to pick for a given corpus, and the exact rules used to pick them depend on the tokenization algorithm. It’s deterministic, meaning you always get the same results when training with the same algorithm on the same corpus.</p></div> <h2 class="relative group"><a id="assembling-a-corpus" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#assembling-a-corpus"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Assembling a corpus</span></h2> <p>There’s a very simple API in 🤗 Transformers that you can use to train a new tokenizer with the same characteristics as an existing one: <code>AutoTokenizer.train_new_from_iterator()</code>. To see this in action, let’s say we want to train GPT-2 from scratch, but in a language other than English. Our first task will be to gather lots of data in that language in a training corpus. To provide examples everyone will be able to understand, we won’t use a language like Russian or Chinese here, but rather a specialized English language: Python code.</p> <p>The <a href="https://github.com/huggingface/datasets" rel="nofollow">🤗 Datasets</a> library can help us assemble a corpus of Python source code. We’ll use the usual <code>load_dataset()</code> function to download and cache the <a href="https://huggingface.co/datasets/code_search_net" rel="nofollow">CodeSearchNet</a> dataset. This dataset was created for the <a href="https://wandb.ai/github/CodeSearchNet/benchmark" rel="nofollow">CodeSearchNet challenge</a> and contains millions of functions from open source libraries on GitHub in several programming languages. Here, we will load the Python part of this dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-comment"># This can take a few minutes to load, so grab a coffee or tea while you wait!</span> raw_datasets = load_dataset(<span class="hljs-string">"code_search_net"</span>, <span class="hljs-string">"python"</span>)</pre></div> <p>We can have a look at the training split to see which columns we have access to:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>raw_datasets[<span class="hljs-string">"train"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Dataset({ features: [<span class="hljs-string">'repository_name'</span>, <span class="hljs-string">'func_path_in_repository'</span>, <span class="hljs-string">'func_name'</span>, <span class="hljs-string">'whole_func_string'</span>, <span class="hljs-string">'language'</span>, <span class="hljs-string">'func_code_string'</span>, <span class="hljs-string">'func_code_tokens'</span>, <span class="hljs-string">'func_documentation_string'</span>, <span class="hljs-string">'func_documentation_tokens'</span>, <span class="hljs-string">'split_name'</span>, <span class="hljs-string">'func_code_url'</span> ], num_rows: <span class="hljs-number">412178</span> })</pre></div> <p>We can see the dataset separates docstrings from code and suggests a tokenization of both. Here. we’ll just use the <code>whole_func_string</code> column to train our tokenizer. We can look at an example of one these functions by indexing into the <code>train</code> split:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">123456</span>][<span class="hljs-string">"whole_func_string"</span>])</pre></div> <p>which should print the following:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">handle_simple_responses</span>(<span class="hljs-params"> self, timeout_ms=<span class="hljs-literal">None</span>, info_cb=DEFAULT_MESSAGE_CALLBACK</span>): <span class="hljs-string">"""Accepts normal responses from the device. Args: timeout_ms: Timeout in milliseconds to wait for each response. info_cb: Optional callback for text sent from the bootloader. Returns: OKAY packet's message. """</span> <span class="hljs-keyword">return</span> self._accept_responses(<span class="hljs-string">'OKAY'</span>, info_cb, timeout_ms=timeout_ms)</pre></div> <p>The first thing we need to do is transform the dataset into an <em>iterator</em> of lists of texts — for instance, a list of list of texts. Using lists of texts will enable our tokenizer to go faster (training on batches of texts instead of processing individual texts one by one), and it should be an iterator if we want to avoid having everything in memory at once. If your corpus is huge, you will want to take advantage of the fact that 🤗 Datasets does not load everything into RAM but stores the elements of the dataset on disk.</p> <p>Doing the following would create a list of lists of 1,000 texts each, but would load everything in memory:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># Don't uncomment the following line unless your dataset is small!</span> <span class="hljs-comment"># training_corpus = [raw_datasets["train"][i: i + 1000]["whole_func_string"] for i in range(0, len(raw_datasets["train"]), 1000)]</span></pre></div> <p>Using a Python generator, we can avoid Python loading anything into memory until it’s actually necessary. To create such a generator, you just to need to replace the brackets with parentheses:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>training_corpus = ( raw_datasets[<span class="hljs-string">"train"</span>][i : i + <span class="hljs-number">1000</span>][<span class="hljs-string">"whole_func_string"</span>] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, <span class="hljs-built_in">len</span>(raw_datasets[<span class="hljs-string">"train"</span>]), <span class="hljs-number">1000</span>) )</pre></div> <p>This line of code doesn’t fetch any elements of the dataset; it just creates an object you can use in a Python <code>for</code> loop. The texts will only be loaded when you need them (that is, when you’re at the step of the <code>for</code> loop that requires them), and only 1,000 texts at a time will be loaded. This way you won’t exhaust all your memory even if you are processing a huge dataset.</p> <p>The problem with a generator object is that it can only be used once. So, instead of this giving us the list of the first 10 digits twice:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>gen = (i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">10</span>)) <span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(gen)) <span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(gen))</pre></div> <p>we get them once and then an empty list:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>] []</pre></div> <p>That’s why we define a function that returns a generator instead:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">get_training_corpus</span>(): <span class="hljs-keyword">return</span> ( raw_datasets[<span class="hljs-string">"train"</span>][i : i + <span class="hljs-number">1000</span>][<span class="hljs-string">"whole_func_string"</span>] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, <span class="hljs-built_in">len</span>(raw_datasets[<span class="hljs-string">"train"</span>]), <span class="hljs-number">1000</span>) ) training_corpus = get_training_corpus()</pre></div> <p>You can also define your generator inside a <code>for</code> loop by using the <code>yield</code> statement:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">get_training_corpus</span>(): dataset = raw_datasets[<span class="hljs-string">"train"</span>] <span class="hljs-keyword">for</span> start_idx <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, <span class="hljs-built_in">len</span>(dataset), <span class="hljs-number">1000</span>): samples = dataset[start_idx : start_idx + <span class="hljs-number">1000</span>] <span class="hljs-keyword">yield</span> samples[<span class="hljs-string">"whole_func_string"</span>]</pre></div> <p>which will produce the exact same generator as before, but allows you to use more complex logic than you can in a list comprehension.</p> <h2 class="relative group"><a id="training-a-new-tokenizer" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-a-new-tokenizer"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training a new tokenizer</span></h2> <p>Now that we have our corpus in the form of an iterator of batches of texts, we are ready to train a new tokenizer. To do this, we first need to load the tokenizer we want to pair with our model (here, GPT-2):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer old_tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"gpt2"</span>)</pre></div> <p>Even though we are going to train a new tokenizer, it’s a good idea to do this to avoid starting entirely from scratch. This way, we won’t have to specify anything about the tokenization algorithm or the special tokens we want to use; our new tokenizer will be exactly the same as GPT-2, and the only thing that will change is the vocabulary, which will be determined by the training on our corpus.</p> <p>First let’s have a look at how this tokenizer would treat an example function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>example = <span class="hljs-string">'''def add_numbers(a, b): """Add the two numbers `a` and `b`.""" return a + b'''</span> tokens = old_tokenizer.tokenize(example) tokens</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'def'</span>, <span class="hljs-string">'Ġadd'</span>, <span class="hljs-string">'_'</span>, <span class="hljs-string">'n'</span>, <span class="hljs-string">'umbers'</span>, <span class="hljs-string">'('</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">','</span>, <span class="hljs-string">'Ġb'</span>, <span class="hljs-string">'):'</span>, <span class="hljs-string">'Ċ'</span>, <span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'Ġ"""'</span>, <span class="hljs-string">'Add'</span>, <span class="hljs-string">'Ġthe'</span>, <span class="hljs-string">'Ġtwo'</span>, <span class="hljs-string">'Ġnumbers'</span>, <span class="hljs-string">'Ġ`'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'`'</span>, <span class="hljs-string">'Ġand'</span>, <span class="hljs-string">'Ġ`'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'`'</span>, <span class="hljs-string">'."'</span>, <span class="hljs-string">'""'</span>, <span class="hljs-string">'Ċ'</span>, <span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'Ġreturn'</span>, <span class="hljs-string">'Ġa'</span>, <span class="hljs-string">'Ġ+'</span>, <span class="hljs-string">'Ġb'</span>]</pre></div> <p>This tokenizer has a few special symbols, like <code>Ġ</code> and <code>Ċ</code>, which denote spaces and newlines, respectively. As we can see, this is not too efficient: the tokenizer returns individual tokens for each space, when it could group together indentation levels (since having sets of four or eight spaces is going to be very common in code). It also split the function name a bit weirdly, not being used to seeing words with the <code>_</code> character.</p> <p>Let’s train a new tokenizer and see if it solves those issues. For this, we’ll use the method <code>train_new_from_iterator()</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer = old_tokenizer.train_new_from_iterator(training_corpus, <span class="hljs-number">52000</span>)</pre></div> <p>This command might take a bit of time if your corpus is very large, but for this dataset of 1.6 GB of texts it’s blazing fast (1 minute 16 seconds on an AMD Ryzen 9 3900X CPU with 12 cores).</p> <p>Note that <code>AutoTokenizer.train_new_from_iterator()</code> only works if the tokenizer you are using is a “fast” tokenizer. As you’ll see in the next section, the 🤗 Transformers library contains two types of tokenizers: some are written purely in Python and others (the fast ones) are backed by the 🤗 Tokenizers library, which is written in the <a href="https://www.rust-lang.org" rel="nofollow">Rust</a> programming language. Python is the language most often used for data science and deep learning applications, but when anything needs to be parallelized to be fast, it has to be written in another language. For instance, the matrix multiplications that are at the core of the model computation are written in CUDA, an optimized C library for GPUs.</p> <p>Training a brand new tokenizer in pure Python would be excruciatingly slow, which is why we developed the 🤗 Tokenizers library. Note that just as you didn’t have to learn the CUDA language to be able to execute your model on a batch of inputs on a GPU, you won’t need to learn Rust to use a fast tokenizer. The 🤗 Tokenizers library provides Python bindings for many methods that internally call some piece of code in Rust; for example, to parallelize the training of your new tokenizer or, as we saw in <a href="/course/chapter3">Chapter 3</a>, the tokenization of a batch of inputs.</p> <p>Most of the Transformer models have a fast tokenizer available (there are some exceptions that you can check <a href="https://huggingface.co/transformers/#supported-frameworks" rel="nofollow">here</a>), and the <code>AutoTokenizer</code> API always selects the fast tokenizer for you if it’s available. In the next section we’ll take a look at some of the other special features fast tokenizers have, which will be really useful for tasks like token classification and question answering. Before diving into that, however, let’s try our brand new tokenizer on the previous example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokens = tokenizer.tokenize(example) tokens</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'def'</span>, <span class="hljs-string">'Ġadd'</span>, <span class="hljs-string">'_'</span>, <span class="hljs-string">'numbers'</span>, <span class="hljs-string">'('</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">','</span>, <span class="hljs-string">'Ġb'</span>, <span class="hljs-string">'):'</span>, <span class="hljs-string">'ĊĠĠĠ'</span>, <span class="hljs-string">'Ġ"""'</span>, <span class="hljs-string">'Add'</span>, <span class="hljs-string">'Ġthe'</span>, <span class="hljs-string">'Ġtwo'</span>, <span class="hljs-string">'Ġnumbers'</span>, <span class="hljs-string">'Ġ`'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'`'</span>, <span class="hljs-string">'Ġand'</span>, <span class="hljs-string">'Ġ`'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'`."""'</span>, <span class="hljs-string">'ĊĠĠĠ'</span>, <span class="hljs-string">'Ġreturn'</span>, <span class="hljs-string">'Ġa'</span>, <span class="hljs-string">'Ġ+'</span>, <span class="hljs-string">'Ġb'</span>]</pre></div> <p>Here we again see the special symbols <code>Ġ</code> and <code>Ċ</code> that denote spaces and newlines, but we can also see that our tokenizer learned some tokens that are highly specific to a corpus of Python functions: for example, there is a <code>ĊĠĠĠ</code> token that represents an indentation, and a <code>Ġ"""</code> token that represents the three quotes that start a docstring. The tokenizer also correctly split the function name on <code>_</code>. This is quite a compact representation; comparatively, using the plain English tokenizer on the same example will give us a longer sentence:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(<span class="hljs-built_in">len</span>(tokens)) <span class="hljs-built_in">print</span>(<span class="hljs-built_in">len</span>(old_tokenizer.tokenize(example)))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">27</span> <span class="hljs-number">36</span></pre></div> <p>Let’s look at another example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>example = <span class="hljs-string">"""class LinearLayer(): def __init__(self, input_size, output_size): self.weight = torch.randn(input_size, output_size) self.bias = torch.zeros(output_size) def __call__(self, x): return x @ self.weights + self.bias """</span> tokenizer.tokenize(example)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'class'</span>, <span class="hljs-string">'ĠLinear'</span>, <span class="hljs-string">'Layer'</span>, <span class="hljs-string">'():'</span>, <span class="hljs-string">'ĊĠĠĠ'</span>, <span class="hljs-string">'Ġdef'</span>, <span class="hljs-string">'Ġ__'</span>, <span class="hljs-string">'init'</span>, <span class="hljs-string">'__('</span>, <span class="hljs-string">'self'</span>, <span class="hljs-string">','</span>, <span class="hljs-string">'Ġinput'</span>, <span class="hljs-string">'_'</span>, <span class="hljs-string">'size'</span>, <span class="hljs-string">','</span>, <span class="hljs-string">'Ġoutput'</span>, <span class="hljs-string">'_'</span>, <span class="hljs-string">'size'</span>, <span class="hljs-string">'):'</span>, <span class="hljs-string">'ĊĠĠĠĠĠĠĠ'</span>, <span class="hljs-string">'Ġself'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'weight'</span>, <span class="hljs-string">'Ġ='</span>, <span class="hljs-string">'Ġtorch'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'randn'</span>, <span class="hljs-string">'('</span>, <span class="hljs-string">'input'</span>, <span class="hljs-string">'_'</span>, <span class="hljs-string">'size'</span>, <span class="hljs-string">','</span>, <span class="hljs-string">'Ġoutput'</span>, <span class="hljs-string">'_'</span>, <span class="hljs-string">'size'</span>, <span class="hljs-string">')'</span>, <span class="hljs-string">'ĊĠĠĠĠĠĠĠ'</span>, <span class="hljs-string">'Ġself'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'bias'</span>, <span class="hljs-string">'Ġ='</span>, <span class="hljs-string">'Ġtorch'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'zeros'</span>, <span class="hljs-string">'('</span>, <span class="hljs-string">'output'</span>, <span class="hljs-string">'_'</span>, <span class="hljs-string">'size'</span>, <span class="hljs-string">')'</span>, <span class="hljs-string">'ĊĊĠĠĠ'</span>, <span class="hljs-string">'Ġdef'</span>, <span class="hljs-string">'Ġ__'</span>, <span class="hljs-string">'call'</span>, <span class="hljs-string">'__('</span>, <span class="hljs-string">'self'</span>, <span class="hljs-string">','</span>, <span class="hljs-string">'Ġx'</span>, <span class="hljs-string">'):'</span>, <span class="hljs-string">'ĊĠĠĠĠĠĠĠ'</span>, <span class="hljs-string">'Ġreturn'</span>, <span class="hljs-string">'Ġx'</span>, <span class="hljs-string">'Ġ@'</span>, <span class="hljs-string">'Ġself'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'weights'</span>, <span class="hljs-string">'Ġ+'</span>, <span class="hljs-string">'Ġself'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'bias'</span>, <span class="hljs-string">'ĊĠĠĠĠ'</span>]</pre></div> <p>In addition to the token corresponding to an indentation, here we can also see a token for a double indentation: <code>ĊĠĠĠĠĠĠĠ</code>. The special Python words like <code>class</code>, <code>init</code>, <code>call</code>, <code>self</code>, and <code>return</code> are each tokenized as one token, and we can see that as well as splitting on <code>_</code> and <code>.</code> the tokenizer correctly splits even camel-cased names: <code>LinearLayer</code> is tokenized as <code>["ĠLinear", "Layer"]</code>.</p> <h2 class="relative group"><a id="saving-the-tokenizer" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#saving-the-tokenizer"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Saving the tokenizer</span></h2> <p>To make sure we can use it later, we need to save our new tokenizer. Like for models, this is done with the <code>save_pretrained()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.save_pretrained(<span class="hljs-string">"code-search-net-tokenizer"</span>)</pre></div> <p>This will create a new folder named <em>code-search-net-tokenizer</em>, which will contain all the files the tokenizer needs to be reloaded. If you want to share this tokenizer with your colleagues and friends, you can upload it to the Hub by logging into your account. If you’re working in a notebook, there’s a convenience function to help you with this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login notebook_login()</pre></div> <p>This will display a widget where you can enter your Hugging Face login credentials. If you aren’t working in a notebook, just type the following line in your terminal:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-cli login</pre></div> <p>Once you’ve logged in, you can push your tokenizer by executing the following command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.push_to_hub(<span class="hljs-string">"code-search-net-tokenizer"</span>)</pre></div> <p>This will create a new repository in your namespace with the name <code>code-search-net-tokenizer</code>, containing the tokenizer file. You can then load the tokenizer from anywhere with the <code>from_pretrained()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># Replace "huggingface-course" below with your actual namespace to use your own tokenizer</span> tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"huggingface-course/code-search-net-tokenizer"</span>)</pre></div> <p>You’re now all set for training a language model from scratch and fine-tuning it on your task at hand! We’ll get to that in <a href="/course/chapter7">Chapter 7</a>, but first, in the rest of this chapter we’ll take a closer look at fast tokenizers and explore in detail what actually happens when we call the method <code>train_new_from_iterator()</code>.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter6/1?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Introduction</a> <a href="/learn/nlp-course/chapter6/3?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Fast tokenizers' special powers<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;training-a-new-tokenizer-from-an-old-one&quot;,&quot;url&quot;:&quot;#training-a-new-tokenizer-from-an-old-one&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Assembling a corpus&quot;,&quot;id&quot;:&quot;assembling-a-corpus&quot;,&quot;url&quot;:&quot;#assembling-a-corpus&quot;},{&quot;title&quot;:&quot;Training a new tokenizer&quot;,&quot;id&quot;:&quot;training-a-new-tokenizer&quot;,&quot;url&quot;:&quot;#training-a-new-tokenizer&quot;},{&quot;title&quot;:&quot;Saving the tokenizer&quot;,&quot;id&quot;:&quot;saving-the-tokenizer&quot;,&quot;url&quot;:&quot;#saving-the-tokenizer&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#training-a-new-tokenizer-from-an-old-one" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-a-new-tokenizer-from-an-old-one"><wbr>Training a new tokenizer from an old one</a> <a href="#assembling-a-corpus" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-assembling-a-corpus"><wbr>Assembling a corpus</a> <a href="#training-a-new-tokenizer" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-a-new-tokenizer"><wbr>Training a new tokenizer</a> <a href="#saving-the-tokenizer" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-saving-the-tokenizer"><wbr>Saving the tokenizer</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter6/2" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter6/2"); } </script> <iframe name="__privateStripeMetricsController6250" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter6%2F2%3Ffw%3Dpt&amp;title=Training%20a%20new%20tokenizer%20from%20an%20old%20one%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:22.061Z
Fast tokenizers' special powers - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter6/3?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#fast-tokenizers-special-powers)Fast tokenizers' special powers [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-6-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section3_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section3_pt.ipynb) In this section we will take a closer look at the capabilities of the tokenizers in 🤗 Transformers. Up to now we have only used them to tokenize inputs or decode IDs back into text, but tokenizers — especially those backed by the 🤗 Tokenizers library — can do a lot more. To illustrate these additional features, we will explore how to reproduce the results of the `token-classification` (that we called `ner`) and `question-answering` pipelines that we first encountered in [Chapter 1](/course/chapter1). In the following discussion, we will often make the distinction between “slow” and “fast” tokenizers. Slow tokenizers are those written in Python inside the 🤗 Transformers library, while the fast versions are the ones provided by 🤗 Tokenizers, which are written in Rust. If you remember the table from [Chapter 5](/course/chapter5/3) that reported how long it took a fast and a slow tokenizer to tokenize the Drug Review Dataset, you should have an idea of why we call them fast and slow: | | Fast tokenizer | Slow tokenizer | | --- | --- | --- | | `batched=True` | 10.8s | 4min41s | | `batched=False` | 59.2s | 5min3s | ⚠️ When tokenizing a single sentence, you won’t always see a difference in speed between the slow and fast versions of the same tokenizer. In fact, the fast version might actually be slower! It’s only when tokenizing lots of texts in parallel at the same time that you will be able to clearly see the difference. ## [](#batch-encoding)Batch encoding The output of a tokenizer isn’t a simple Python dictionary; what we get is actually a special `BatchEncoding` object. It’s a subclass of a dictionary (which is why we were able to index into that result without any problem before), but with additional methods that are mostly used by fast tokenizers. Besides their parallelization capabilities, the key functionality of fast tokenizers is that they always keep track of the original span of texts the final tokens come from — a feature we call _offset mapping_. This in turn unlocks features like mapping each word to the tokens it generated or mapping each character of the original text to the token it’s inside, and vice versa. Let’s take a look at an example: ``` from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") example = "My name is Sylvain and I work at Hugging Face in Brooklyn." encoding = tokenizer(example) print(type(encoding))``` As mentioned previously, we get a `BatchEncoding` object in the tokenizer’s output: ``` <class 'transformers.tokenization_utils_base.BatchEncoding'>``` Since the `AutoTokenizer` class picks a fast tokenizer by default, we can use the additional methods this `BatchEncoding` object provides. We have two ways to check if our tokenizer is a fast or a slow one. We can either check the attribute `is_fast` of the `tokenizer`: or check the same attribute of our `encoding`: Let’s see what a fast tokenizer enables us to do. First, we can access the tokens without having to convert the IDs back to tokens: ``` ['[CLS]', 'My', 'name', 'is', 'S', '##yl', '##va', '##in', 'and', 'I', 'work', 'at', 'Hu', '##gging', 'Face', 'in', 'Brooklyn', '.', '[SEP]']``` In this case the token at index 5 is `##yl`, which is part of the word “Sylvain” in the original sentence. We can also use the `word_ids()` method to get the index of the word each token comes from: ``` [None, 0, 1, 2, 3, 3, 3, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, None]``` We can see that the tokenizer’s special tokens `[CLS]` and `[SEP]` are mapped to `None`, and then each token is mapped to the word it originates from. This is especially useful to determine if a token is at the start of a word or if two tokens are in the same word. We could rely on the `##` prefix for that, but it only works for BERT-like tokenizers; this method works for any type of tokenizer as long as it’s a fast one. In the next chapter, we’ll see how we can use this capability to apply the labels we have for each word properly to the tokens in tasks like named entity recognition (NER) and part-of-speech (POS) tagging. We can also use it to mask all the tokens coming from the same word in masked language modeling (a technique called _whole word masking_). The notion of what a word is complicated. For instance, does “I’ll” (a contraction of “I will”) count as one or two words? It actually depends on the tokenizer and the pre-tokenization operation it applies. Some tokenizers just split on spaces, so they will consider this as one word. Others use punctuation on top of spaces, so will consider it two words. ✏️ **Try it out!** Create a tokenizer from the `bert-base-cased` and `roberta-base` checkpoints and tokenize ”81s” with them. What do you observe? What are the word IDs? Similarly, there is a `sentence_ids()` method that we can use to map a token to the sentence it came from (though in this case, the `token_type_ids` returned by the tokenizer can give us the same information). Lastly, we can map any word or token to characters in the original text, and vice versa, via the `word_to_chars()` or `token_to_chars()` and `char_to_word()` or `char_to_token()` methods. For instance, the `word_ids()` method told us that `##yl` is part of the word at index 3, but which word is it in the sentence? We can find out like this: ``` start, end = encoding.word_to_chars(3) example[start:end]``` As we mentioned previously, this is all powered by the fact the fast tokenizer keeps track of the span of text each token comes from in a list of _offsets_. To illustrate their use, next we’ll show you how to replicate the results of the `token-classification` pipeline manually. ✏️ **Try it out!** Create your own example text and see if you can understand which tokens are associated with word ID, and also how to extract the character spans for a single word. For bonus points, try using two sentences as input and see if the sentence IDs make sense to you. ## [](#inside-the-token-classification-pipeline)Inside the `token-classification` pipeline In [Chapter 1](/course/chapter1) we got our first taste of applying NER — where the task is to identify which parts of the text correspond to entities like persons, locations, or organizations — with the 🤗 Transformers `pipeline()` function. Then, in [Chapter 2](/course/chapter2), we saw how a pipeline groups together the three stages necessary to get the predictions from a raw text: tokenization, passing the inputs through the model, and post-processing. The first two steps in the `token-classification` pipeline are the same as in any other pipeline, but the post-processing is a little more complex — let’s see how! ### [](#getting-the-base-results-with-the-pipeline)Getting the base results with the pipeline First, let’s grab a token classification pipeline so we can get some results to compare manually. The model used by default is [`dbmdz/bert-large-cased-finetuned-conll03-english`](https://huggingface.co/dbmdz/bert-large-cased-finetuned-conll03-english); it performs NER on sentences: ``` from transformers import pipeline token_classifier = pipeline("token-classification") token_classifier("My name is Sylvain and I work at Hugging Face in Brooklyn.")``` ``` [{'entity': 'I-PER', 'score': 0.9993828, 'index': 4, 'word': 'S', 'start': 11, 'end': 12}, {'entity': 'I-PER', 'score': 0.99815476, 'index': 5, 'word': '##yl', 'start': 12, 'end': 14}, {'entity': 'I-PER', 'score': 0.99590725, 'index': 6, 'word': '##va', 'start': 14, 'end': 16}, {'entity': 'I-PER', 'score': 0.9992327, 'index': 7, 'word': '##in', 'start': 16, 'end': 18}, {'entity': 'I-ORG', 'score': 0.97389334, 'index': 12, 'word': 'Hu', 'start': 33, 'end': 35}, {'entity': 'I-ORG', 'score': 0.976115, 'index': 13, 'word': '##gging', 'start': 35, 'end': 40}, {'entity': 'I-ORG', 'score': 0.98879766, 'index': 14, 'word': 'Face', 'start': 41, 'end': 45}, {'entity': 'I-LOC', 'score': 0.99321055, 'index': 16, 'word': 'Brooklyn', 'start': 49, 'end': 57}]``` The model properly identified each token generated by “Sylvain” as a person, each token generated by “Hugging Face” as an organization, and the token “Brooklyn” as a location. We can also ask the pipeline to group together the tokens that correspond to the same entity: ``` from transformers import pipeline token_classifier = pipeline("token-classification", aggregation_strategy="simple") token_classifier("My name is Sylvain and I work at Hugging Face in Brooklyn.")``` ``` [{'entity_group': 'PER', 'score': 0.9981694, 'word': 'Sylvain', 'start': 11, 'end': 18}, {'entity_group': 'ORG', 'score': 0.97960204, 'word': 'Hugging Face', 'start': 33, 'end': 45}, {'entity_group': 'LOC', 'score': 0.99321055, 'word': 'Brooklyn', 'start': 49, 'end': 57}]``` The `aggregation_strategy` picked will change the scores computed for each grouped entity. With `"simple"` the score is just the mean of the scores of each token in the given entity: for instance, the score of “Sylvain” is the mean of the scores we saw in the previous example for the tokens `S`, `##yl`, `##va`, and `##in`. Other strategies available are: - `"first"`, where the score of each entity is the score of the first token of that entity (so for “Sylvain” it would be 0.993828, the score of the token `S`) - `"max"`, where the score of each entity is the maximum score of the tokens in that entity (so for “Hugging Face” it would be 0.98879766, the score of “Face”) - `"average"`, where the score of each entity is the average of the scores of the words composing that entity (so for “Sylvain” there would be no difference from the `"simple"` strategy, but “Hugging Face” would have a score of 0.9819, the average of the scores for “Hugging”, 0.975, and “Face”, 0.98879) Now let’s see how to obtain these results without using the `pipeline()` function! ### [](#from-inputs-to-predictions)From inputs to predictions First we need to tokenize our input and pass it through the model. This is done exactly as in [Chapter 2](/course/chapter2); we instantiate the tokenizer and the model using the `AutoXxx` classes and then use them on our example: ``` from transformers import AutoTokenizer, AutoModelForTokenClassification model_checkpoint = "dbmdz/bert-large-cased-finetuned-conll03-english" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) model = AutoModelForTokenClassification.from_pretrained(model_checkpoint) example = "My name is Sylvain and I work at Hugging Face in Brooklyn." inputs = tokenizer(example, return_tensors="pt") outputs = model(**inputs)``` Since we’re using `AutoModelForTokenClassification` here, we get one set of logits for each token in the input sequence: ``` print(inputs["input_ids"].shape) print(outputs.logits.shape)``` ``` torch.Size([1, 19]) torch.Size([1, 19, 9])``` We have a batch with 1 sequence of 19 tokens and the model has 9 different labels, so the output of the model has a shape of 1 x 19 x 9. Like for the text classification pipeline, we use a softmax function to convert those logits to probabilities, and we take the argmax to get predictions (note that we can take the argmax on the logits because the softmax does not change the order): ``` import torch probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)[0].tolist() predictions = outputs.logits.argmax(dim=-1)[0].tolist() print(predictions)``` ``` [0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 0, 6, 6, 6, 0, 8, 0, 0]``` The `model.config.id2label` attribute contains the mapping of indexes to labels that we can use to make sense of the predictions: ``` {0: 'O', 1: 'B-MISC', 2: 'I-MISC', 3: 'B-PER', 4: 'I-PER', 5: 'B-ORG', 6: 'I-ORG', 7: 'B-LOC', 8: 'I-LOC'}``` As we saw earlier, there are 9 labels: `O` is the label for the tokens that are not in any named entity (it stands for “outside”), and we then have two labels for each type of entity (miscellaneous, person, organization, and location). The label `B-XXX` indicates the token is at the beginning of an entity `XXX` and the label `I-XXX` indicates the token is inside the entity `XXX`. For instance, in the current example we would expect our model to classify the token `S` as `B-PER` (beginning of a person entity) and the tokens `##yl`, `##va` and `##in` as `I-PER` (inside a person entity). You might think the model was wrong in this case as it gave the label `I-PER` to all four of these tokens, but that’s not entirely true. There are actually two formats for those `B-` and `I-` labels: _IOB1_ and _IOB2_. The IOB2 format (in pink below), is the one we introduced whereas in the IOB1 format (in blue), the labels beginning with `B-` are only ever used to separate two adjacent entities of the same type. The model we are using was fine-tuned on a dataset using that format, which is why it assigns the label `I-PER` to the `S` token. ![IOB1 vs IOB2 format](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/IOB_versions.svg) ![IOB1 vs IOB2 format](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/IOB_versions-dark.svg) With this map, we are ready to reproduce (almost entirely) the results of the first pipeline — we can just grab the score and label of each token that was not classified as `O`: ``` results = [] tokens = inputs.tokens() for idx, pred in enumerate(predictions): label = model.config.id2label[pred] if label != "O": results.append( {"entity": label, "score": probabilities[idx][pred], "word": tokens[idx]} ) print(results)``` ``` [{'entity': 'I-PER', 'score': 0.9993828, 'index': 4, 'word': 'S'}, {'entity': 'I-PER', 'score': 0.99815476, 'index': 5, 'word': '##yl'}, {'entity': 'I-PER', 'score': 0.99590725, 'index': 6, 'word': '##va'}, {'entity': 'I-PER', 'score': 0.9992327, 'index': 7, 'word': '##in'}, {'entity': 'I-ORG', 'score': 0.97389334, 'index': 12, 'word': 'Hu'}, {'entity': 'I-ORG', 'score': 0.976115, 'index': 13, 'word': '##gging'}, {'entity': 'I-ORG', 'score': 0.98879766, 'index': 14, 'word': 'Face'}, {'entity': 'I-LOC', 'score': 0.99321055, 'index': 16, 'word': 'Brooklyn'}]``` This is very similar to what we had before, with one exception: the pipeline also gave us information about the `start` and `end` of each entity in the original sentence. This is where our offset mapping will come into play. To get the offsets, we just have to set `return_offsets_mapping=True` when we apply the tokenizer to our inputs: ``` inputs_with_offsets = tokenizer(example, return_offsets_mapping=True) inputs_with_offsets["offset_mapping"]``` ``` [(0, 0), (0, 2), (3, 7), (8, 10), (11, 12), (12, 14), (14, 16), (16, 18), (19, 22), (23, 24), (25, 29), (30, 32), (33, 35), (35, 40), (41, 45), (46, 48), (49, 57), (57, 58), (0, 0)]``` Each tuple is the span of text corresponding to each token, where `(0, 0)` is reserved for the special tokens. We saw before that the token at index 5 is `##yl`, which has `(12, 14)` as offsets here. If we grab the corresponding slice in our example: we get the proper span of text without the `##`: Using this, we can now complete the previous results: ``` results = [] inputs_with_offsets = tokenizer(example, return_offsets_mapping=True) tokens = inputs_with_offsets.tokens() offsets = inputs_with_offsets["offset_mapping"] for idx, pred in enumerate(predictions): label = model.config.id2label[pred] if label != "O": start, end = offsets[idx] results.append( { "entity": label, "score": probabilities[idx][pred], "word": tokens[idx], "start": start, "end": end, } ) print(results)``` ``` [{'entity': 'I-PER', 'score': 0.9993828, 'index': 4, 'word': 'S', 'start': 11, 'end': 12}, {'entity': 'I-PER', 'score': 0.99815476, 'index': 5, 'word': '##yl', 'start': 12, 'end': 14}, {'entity': 'I-PER', 'score': 0.99590725, 'index': 6, 'word': '##va', 'start': 14, 'end': 16}, {'entity': 'I-PER', 'score': 0.9992327, 'index': 7, 'word': '##in', 'start': 16, 'end': 18}, {'entity': 'I-ORG', 'score': 0.97389334, 'index': 12, 'word': 'Hu', 'start': 33, 'end': 35}, {'entity': 'I-ORG', 'score': 0.976115, 'index': 13, 'word': '##gging', 'start': 35, 'end': 40}, {'entity': 'I-ORG', 'score': 0.98879766, 'index': 14, 'word': 'Face', 'start': 41, 'end': 45}, {'entity': 'I-LOC', 'score': 0.99321055, 'index': 16, 'word': 'Brooklyn', 'start': 49, 'end': 57}]``` This is the same as what we got from the first pipeline! ### [](#grouping-entities)Grouping entities Using the offsets to determine the start and end keys for each entity is handy, but that information isn’t strictly necessary. When we want to group the entities together, however, the offsets will save us a lot of messy code. For example, if we wanted to group together the tokens `Hu`, `##gging`, and `Face`, we could make special rules that say the first two should be attached while removing the `##`, and the `Face` should be added with a space since it does not begin with `##` — but that would only work for this particular type of tokenizer. We would have to write another set of rules for a SentencePiece or a Byte-Pair-Encoding tokenizer (discussed later in this chapter). With the offsets, all that custom code goes away: we just can take the span in the original text that begins with the first token and ends with the last token. So, in the case of the tokens `Hu`, `##gging`, and `Face`, we should start at character 33 (the beginning of `Hu`) and end before character 45 (the end of `Face`): To write the code that post-processes the predictions while grouping entities, we will group together entities that are consecutive and labeled with `I-XXX`, except for the first one, which can be labeled as `B-XXX` or `I-XXX` (so, we stop grouping an entity when we get a `O`, a new type of entity, or a `B-XXX` that tells us an entity of the same type is starting): ``` import numpy as np results = [] inputs_with_offsets = tokenizer(example, return_offsets_mapping=True) tokens = inputs_with_offsets.tokens() offsets = inputs_with_offsets["offset_mapping"] idx = 0 while idx < len(predictions): pred = predictions[idx] label = model.config.id2label[pred] if label != "O": label = label[2:] start, _ = offsets[idx] all_scores = [] while ( idx < len(predictions) and model.config.id2label[predictions[idx]] == f"I-{label}" ): all_scores.append(probabilities[idx][pred]) _, end = offsets[idx] idx += 1 score = np.mean(all_scores).item() word = example[start:end] results.append( { "entity_group": label, "score": score, "word": word, "start": start, "end": end, } ) idx += 1 print(results)``` And we get the same results as with our second pipeline! ``` [{'entity_group': 'PER', 'score': 0.9981694, 'word': 'Sylvain', 'start': 11, 'end': 18}, {'entity_group': 'ORG', 'score': 0.97960204, 'word': 'Hugging Face', 'start': 33, 'end': 45}, {'entity_group': 'LOC', 'score': 0.99321055, 'word': 'Brooklyn', 'start': 49, 'end': 57}]``` Another example of a task where these offsets are extremely useful is question answering. Diving into that pipeline, which we’ll do in the next section, will also enable us to take a look at one last feature of the tokenizers in the 🤗 Transformers library: dealing with overflowing tokens when we truncate an input to a given length.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Fast tokenizers' special powers - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter6/3"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Fast tokenizers' special powers - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;fast-tokenizers-special-powers&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;batch-encoding&quot;,&quot;title&quot;:&quot;Batch encoding&quot;},{&quot;local&quot;:&quot;inside-the-token-classification-pipeline&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;getting-the-base-results-with-the-pipeline&quot;,&quot;title&quot;:&quot;Getting the base results with the pipeline&quot;},{&quot;local&quot;:&quot;from-inputs-to-predictions&quot;,&quot;title&quot;:&quot;From inputs to predictions&quot;},{&quot;local&quot;:&quot;grouping-entities&quot;,&quot;title&quot;:&quot;Grouping entities&quot;}],&quot;title&quot;:&quot;Inside the `token-classification` pipeline&quot;}],&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter6/3&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Fast tokenizers' special powers</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/2?fw=pt">Training a new tokenizer from an old one </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter6/3?fw=pt">Fast tokenizers' special powers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3b?fw=pt">Fast tokenizers in the QA pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/4?fw=pt">Normalization and pre-tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/5?fw=pt">Byte-Pair Encoding tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/6?fw=pt">WordPiece tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/7?fw=pt">Unigram tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/8?fw=pt">Building a tokenizer, block by block </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/9?fw=pt">Tokenizers, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="fast-tokenizers-special-powers" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fast-tokenizers-special-powers"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fast tokenizers' special powers</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-6-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section3_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section3_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>In this section we will take a closer look at the capabilities of the tokenizers in 🤗 Transformers. Up to now we have only used them to tokenize inputs or decode IDs back into text, but tokenizers — especially those backed by the 🤗 Tokenizers library — can do a lot more. To illustrate these additional features, we will explore how to reproduce the results of the <code>token-classification</code> (that we called <code>ner</code>) and <code>question-answering</code> pipelines that we first encountered in <a href="/course/chapter1">Chapter 1</a>.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/g8quOxoqhHQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>In the following discussion, we will often make the distinction between “slow” and “fast” tokenizers. Slow tokenizers are those written in Python inside the 🤗 Transformers library, while the fast versions are the ones provided by 🤗 Tokenizers, which are written in Rust. If you remember the table from <a href="/course/chapter5/3">Chapter 5</a> that reported how long it took a fast and a slow tokenizer to tokenize the Drug Review Dataset, you should have an idea of why we call them fast and slow:</p> <table><thead><tr><th align="center"></th> <th align="center">Fast tokenizer</th> <th align="center">Slow tokenizer</th></tr></thead> <tbody><tr><td align="center"><code>batched=True</code></td> <td align="center">10.8s</td> <td align="center">4min41s</td></tr> <tr><td align="center"><code>batched=False</code></td> <td align="center">59.2s</td> <td align="center">5min3s</td></tr></tbody></table> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>⚠️ When tokenizing a single sentence, you won’t always see a difference in speed between the slow and fast versions of the same tokenizer. In fact, the fast version might actually be slower! It’s only when tokenizing lots of texts in parallel at the same time that you will be able to clearly see the difference.</p></div> <h2 class="relative group"><a id="batch-encoding" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#batch-encoding"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Batch encoding</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/3umI3tm27Vw" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The output of a tokenizer isn’t a simple Python dictionary; what we get is actually a special <code>BatchEncoding</code> object. It’s a subclass of a dictionary (which is why we were able to index into that result without any problem before), but with additional methods that are mostly used by fast tokenizers.</p> <p>Besides their parallelization capabilities, the key functionality of fast tokenizers is that they always keep track of the original span of texts the final tokens come from — a feature we call <em>offset mapping</em>. This in turn unlocks features like mapping each word to the tokens it generated or mapping each character of the original text to the token it’s inside, and vice versa.</p> <p>Let’s take a look at an example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>) example = <span class="hljs-string">"My name is Sylvain and I work at Hugging Face in Brooklyn."</span> encoding = tokenizer(example) <span class="hljs-built_in">print</span>(<span class="hljs-built_in">type</span>(encoding))</pre></div> <p>As mentioned previously, we get a <code>BatchEncoding</code> object in the tokenizer’s output:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>&lt;<span class="hljs-keyword">class</span> <span class="hljs-string">'transformers.tokenization_utils_base.BatchEncoding'</span>&gt;</pre></div> <p>Since the <code>AutoTokenizer</code> class picks a fast tokenizer by default, we can use the additional methods this <code>BatchEncoding</code> object provides. We have two ways to check if our tokenizer is a fast or a slow one. We can either check the attribute <code>is_fast</code> of the <code>tokenizer</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.is_fast</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-literal">True</span></pre></div> <p>or check the same attribute of our <code>encoding</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>encoding.is_fast</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-literal">True</span></pre></div> <p>Let’s see what a fast tokenizer enables us to do. First, we can access the tokens without having to convert the IDs back to tokens:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>encoding.tokens()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'[CLS]'</span>, <span class="hljs-string">'My'</span>, <span class="hljs-string">'name'</span>, <span class="hljs-string">'is'</span>, <span class="hljs-string">'S'</span>, <span class="hljs-string">'##yl'</span>, <span class="hljs-string">'##va'</span>, <span class="hljs-string">'##in'</span>, <span class="hljs-string">'and'</span>, <span class="hljs-string">'I'</span>, <span class="hljs-string">'work'</span>, <span class="hljs-string">'at'</span>, <span class="hljs-string">'Hu'</span>, <span class="hljs-string">'##gging'</span>, <span class="hljs-string">'Face'</span>, <span class="hljs-string">'in'</span>, <span class="hljs-string">'Brooklyn'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'[SEP]'</span>]</pre></div> <p>In this case the token at index 5 is <code>##yl</code>, which is part of the word “Sylvain” in the original sentence. We can also use the <code>word_ids()</code> method to get the index of the word each token comes from:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>encoding.word_ids()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-literal">None</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>, <span class="hljs-number">11</span>, <span class="hljs-number">12</span>, <span class="hljs-literal">None</span>]</pre></div> <p>We can see that the tokenizer’s special tokens <code>[CLS]</code> and <code>[SEP]</code> are mapped to <code>None</code>, and then each token is mapped to the word it originates from. This is especially useful to determine if a token is at the start of a word or if two tokens are in the same word. We could rely on the <code>##</code> prefix for that, but it only works for BERT-like tokenizers; this method works for any type of tokenizer as long as it’s a fast one. In the next chapter, we’ll see how we can use this capability to apply the labels we have for each word properly to the tokens in tasks like named entity recognition (NER) and part-of-speech (POS) tagging. We can also use it to mask all the tokens coming from the same word in masked language modeling (a technique called <em>whole word masking</em>).</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>The notion of what a word is complicated. For instance, does “I’ll” (a contraction of “I will”) count as one or two words? It actually depends on the tokenizer and the pre-tokenization operation it applies. Some tokenizers just split on spaces, so they will consider this as one word. Others use punctuation on top of spaces, so will consider it two words.</p> <p>✏️ <strong>Try it out!</strong> Create a tokenizer from the <code>bert-base-cased</code> and <code>roberta-base</code> checkpoints and tokenize ”81s” with them. What do you observe? What are the word IDs?</p></div> <p>Similarly, there is a <code>sentence_ids()</code> method that we can use to map a token to the sentence it came from (though in this case, the <code>token_type_ids</code> returned by the tokenizer can give us the same information).</p> <p>Lastly, we can map any word or token to characters in the original text, and vice versa, via the <code>word_to_chars()</code> or <code>token_to_chars()</code> and <code>char_to_word()</code> or <code>char_to_token()</code> methods. For instance, the <code>word_ids()</code> method told us that <code>##yl</code> is part of the word at index 3, but which word is it in the sentence? We can find out like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>start, end = encoding.word_to_chars(<span class="hljs-number">3</span>) example[start:end]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Sylvain</pre></div> <p>As we mentioned previously, this is all powered by the fact the fast tokenizer keeps track of the span of text each token comes from in a list of <em>offsets</em>. To illustrate their use, next we’ll show you how to replicate the results of the <code>token-classification</code> pipeline manually.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Create your own example text and see if you can understand which tokens are associated with word ID, and also how to extract the character spans for a single word. For bonus points, try using two sentences as input and see if the sentence IDs make sense to you.</p></div> <h2 class="relative group"><a id="inside-the-token-classification-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#inside-the-token-classification-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Inside the <code>token-classification</code> pipeline</span></h2> <p>In <a href="/course/chapter1">Chapter 1</a> we got our first taste of applying NER — where the task is to identify which parts of the text correspond to entities like persons, locations, or organizations — with the 🤗 Transformers <code>pipeline()</code> function. Then, in <a href="/course/chapter2">Chapter 2</a>, we saw how a pipeline groups together the three stages necessary to get the predictions from a raw text: tokenization, passing the inputs through the model, and post-processing. The first two steps in the <code>token-classification</code> pipeline are the same as in any other pipeline, but the post-processing is a little more complex — let’s see how!</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/0E7ltQB7fM8" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <h3 class="relative group"><a id="getting-the-base-results-with-the-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#getting-the-base-results-with-the-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Getting the base results with the pipeline</span></h3> <p>First, let’s grab a token classification pipeline so we can get some results to compare manually. The model used by default is <a href="https://huggingface.co/dbmdz/bert-large-cased-finetuned-conll03-english" rel="nofollow"><code>dbmdz/bert-large-cased-finetuned-conll03-english</code></a>; it performs NER on sentences:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline token_classifier = pipeline(<span class="hljs-string">"token-classification"</span>) token_classifier(<span class="hljs-string">"My name is Sylvain and I work at Hugging Face in Brooklyn."</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9993828</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">4</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'S'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">11</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">12</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99815476</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">5</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'##yl'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">12</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">14</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99590725</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">6</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'##va'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">14</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">16</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9992327</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">7</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'##in'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">16</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">18</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.97389334</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">12</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Hu'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">33</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">35</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.976115</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">13</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'##gging'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">35</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">40</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.98879766</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">14</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Face'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">41</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">45</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-LOC'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99321055</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">16</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Brooklyn'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">49</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">57</span>}]</pre></div> <p>The model properly identified each token generated by “Sylvain” as a person, each token generated by “Hugging Face” as an organization, and the token “Brooklyn” as a location. We can also ask the pipeline to group together the tokens that correspond to the same entity:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline token_classifier = pipeline(<span class="hljs-string">"token-classification"</span>, aggregation_strategy=<span class="hljs-string">"simple"</span>) token_classifier(<span class="hljs-string">"My name is Sylvain and I work at Hugging Face in Brooklyn."</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'entity_group'</span>: <span class="hljs-string">'PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9981694</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Sylvain'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">11</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">18</span>}, {<span class="hljs-string">'entity_group'</span>: <span class="hljs-string">'ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.97960204</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Hugging Face'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">33</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">45</span>}, {<span class="hljs-string">'entity_group'</span>: <span class="hljs-string">'LOC'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99321055</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Brooklyn'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">49</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">57</span>}]</pre></div> <p>The <code>aggregation_strategy</code> picked will change the scores computed for each grouped entity. With <code>"simple"</code> the score is just the mean of the scores of each token in the given entity: for instance, the score of “Sylvain” is the mean of the scores we saw in the previous example for the tokens <code>S</code>, <code>##yl</code>, <code>##va</code>, and <code>##in</code>. Other strategies available are:</p> <ul><li><code>"first"</code>, where the score of each entity is the score of the first token of that entity (so for “Sylvain” it would be 0.993828, the score of the token <code>S</code>)</li> <li><code>"max"</code>, where the score of each entity is the maximum score of the tokens in that entity (so for “Hugging Face” it would be 0.98879766, the score of “Face”)</li> <li><code>"average"</code>, where the score of each entity is the average of the scores of the words composing that entity (so for “Sylvain” there would be no difference from the <code>"simple"</code> strategy, but “Hugging Face” would have a score of 0.9819, the average of the scores for “Hugging”, 0.975, and “Face”, 0.98879)</li></ul> <p>Now let’s see how to obtain these results without using the <code>pipeline()</code> function!</p> <h3 class="relative group"><a id="from-inputs-to-predictions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#from-inputs-to-predictions"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>From inputs to predictions</span></h3> <p>First we need to tokenize our input and pass it through the model. This is done exactly as in <a href="/course/chapter2">Chapter 2</a>; we instantiate the tokenizer and the model using the <code>AutoXxx</code> classes and then use them on our example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForTokenClassification model_checkpoint = <span class="hljs-string">"dbmdz/bert-large-cased-finetuned-conll03-english"</span> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) model = AutoModelForTokenClassification.from_pretrained(model_checkpoint) example = <span class="hljs-string">"My name is Sylvain and I work at Hugging Face in Brooklyn."</span> inputs = tokenizer(example, return_tensors=<span class="hljs-string">"pt"</span>) outputs = model(**inputs)</pre></div> <p>Since we’re using <code>AutoModelForTokenClassification</code> here, we get one set of logits for each token in the input sequence:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(inputs[<span class="hljs-string">"input_ids"</span>].shape) <span class="hljs-built_in">print</span>(outputs.logits.shape)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>torch.Size([<span class="hljs-number">1</span>, <span class="hljs-number">19</span>]) torch.Size([<span class="hljs-number">1</span>, <span class="hljs-number">19</span>, <span class="hljs-number">9</span>])</pre></div> <p>We have a batch with 1 sequence of 19 tokens and the model has 9 different labels, so the output of the model has a shape of 1 x 19 x 9. Like for the text classification pipeline, we use a softmax function to convert those logits to probabilities, and we take the argmax to get predictions (note that we can take the argmax on the logits because the softmax does not change the order):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch probabilities = torch.nn.functional.softmax(outputs.logits, dim=-<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>].tolist() predictions = outputs.logits.argmax(dim=-<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>].tolist() <span class="hljs-built_in">print</span>(predictions)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">6</span>, <span class="hljs-number">0</span>, <span class="hljs-number">8</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]</pre></div> <p>The <code>model.config.id2label</code> attribute contains the mapping of indexes to labels that we can use to make sense of the predictions:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model.config.id2label</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-number">0</span>: <span class="hljs-string">'O'</span>, <span class="hljs-number">1</span>: <span class="hljs-string">'B-MISC'</span>, <span class="hljs-number">2</span>: <span class="hljs-string">'I-MISC'</span>, <span class="hljs-number">3</span>: <span class="hljs-string">'B-PER'</span>, <span class="hljs-number">4</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-number">5</span>: <span class="hljs-string">'B-ORG'</span>, <span class="hljs-number">6</span>: <span class="hljs-string">'I-ORG'</span>, <span class="hljs-number">7</span>: <span class="hljs-string">'B-LOC'</span>, <span class="hljs-number">8</span>: <span class="hljs-string">'I-LOC'</span>}</pre></div> <p>As we saw earlier, there are 9 labels: <code>O</code> is the label for the tokens that are not in any named entity (it stands for “outside”), and we then have two labels for each type of entity (miscellaneous, person, organization, and location). The label <code>B-XXX</code> indicates the token is at the beginning of an entity <code>XXX</code> and the label <code>I-XXX</code> indicates the token is inside the entity <code>XXX</code>. For instance, in the current example we would expect our model to classify the token <code>S</code> as <code>B-PER</code> (beginning of a person entity) and the tokens <code>##yl</code>, <code>##va</code> and <code>##in</code> as <code>I-PER</code> (inside a person entity).</p> <p>You might think the model was wrong in this case as it gave the label <code>I-PER</code> to all four of these tokens, but that’s not entirely true. There are actually two formats for those <code>B-</code> and <code>I-</code> labels: <em>IOB1</em> and <em>IOB2</em>. The IOB2 format (in pink below), is the one we introduced whereas in the IOB1 format (in blue), the labels beginning with <code>B-</code> are only ever used to separate two adjacent entities of the same type. The model we are using was fine-tuned on a dataset using that format, which is why it assigns the label <code>I-PER</code> to the <code>S</code> token.</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/IOB_versions.svg" alt="IOB1 vs IOB2 format"> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/IOB_versions-dark.svg" alt="IOB1 vs IOB2 format"></div> <p>With this map, we are ready to reproduce (almost entirely) the results of the first pipeline — we can just grab the score and label of each token that was not classified as <code>O</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>results = [] tokens = inputs.tokens() <span class="hljs-keyword">for</span> idx, pred <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(predictions): label = model.config.id2label[pred] <span class="hljs-keyword">if</span> label != <span class="hljs-string">"O"</span>: results.append( {<span class="hljs-string">"entity"</span>: label, <span class="hljs-string">"score"</span>: probabilities[idx][pred], <span class="hljs-string">"word"</span>: tokens[idx]} ) <span class="hljs-built_in">print</span>(results)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9993828</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">4</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'S'</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99815476</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">5</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'##yl'</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99590725</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">6</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'##va'</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9992327</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">7</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'##in'</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.97389334</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">12</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Hu'</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.976115</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">13</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'##gging'</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.98879766</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">14</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Face'</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-LOC'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99321055</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">16</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Brooklyn'</span>}]</pre></div> <p>This is very similar to what we had before, with one exception: the pipeline also gave us information about the <code>start</code> and <code>end</code> of each entity in the original sentence. This is where our offset mapping will come into play. To get the offsets, we just have to set <code>return_offsets_mapping=True</code> when we apply the tokenizer to our inputs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs_with_offsets = tokenizer(example, return_offsets_mapping=<span class="hljs-literal">True</span>) inputs_with_offsets[<span class="hljs-string">"offset_mapping"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">0</span>, <span class="hljs-number">2</span>), (<span class="hljs-number">3</span>, <span class="hljs-number">7</span>), (<span class="hljs-number">8</span>, <span class="hljs-number">10</span>), (<span class="hljs-number">11</span>, <span class="hljs-number">12</span>), (<span class="hljs-number">12</span>, <span class="hljs-number">14</span>), (<span class="hljs-number">14</span>, <span class="hljs-number">16</span>), (<span class="hljs-number">16</span>, <span class="hljs-number">18</span>), (<span class="hljs-number">19</span>, <span class="hljs-number">22</span>), (<span class="hljs-number">23</span>, <span class="hljs-number">24</span>), (<span class="hljs-number">25</span>, <span class="hljs-number">29</span>), (<span class="hljs-number">30</span>, <span class="hljs-number">32</span>), (<span class="hljs-number">33</span>, <span class="hljs-number">35</span>), (<span class="hljs-number">35</span>, <span class="hljs-number">40</span>), (<span class="hljs-number">41</span>, <span class="hljs-number">45</span>), (<span class="hljs-number">46</span>, <span class="hljs-number">48</span>), (<span class="hljs-number">49</span>, <span class="hljs-number">57</span>), (<span class="hljs-number">57</span>, <span class="hljs-number">58</span>), (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)]</pre></div> <p>Each tuple is the span of text corresponding to each token, where <code>(0, 0)</code> is reserved for the special tokens. We saw before that the token at index 5 is <code>##yl</code>, which has <code>(12, 14)</code> as offsets here. If we grab the corresponding slice in our example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>example[<span class="hljs-number">12</span>:<span class="hljs-number">14</span>]</pre></div> <p>we get the proper span of text without the <code>##</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>yl</pre></div> <p>Using this, we can now complete the previous results:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>results = [] inputs_with_offsets = tokenizer(example, return_offsets_mapping=<span class="hljs-literal">True</span>) tokens = inputs_with_offsets.tokens() offsets = inputs_with_offsets[<span class="hljs-string">"offset_mapping"</span>] <span class="hljs-keyword">for</span> idx, pred <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(predictions): label = model.config.id2label[pred] <span class="hljs-keyword">if</span> label != <span class="hljs-string">"O"</span>: start, end = offsets[idx] results.append( { <span class="hljs-string">"entity"</span>: label, <span class="hljs-string">"score"</span>: probabilities[idx][pred], <span class="hljs-string">"word"</span>: tokens[idx], <span class="hljs-string">"start"</span>: start, <span class="hljs-string">"end"</span>: end, } ) <span class="hljs-built_in">print</span>(results)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9993828</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">4</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'S'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">11</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">12</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99815476</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">5</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'##yl'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">12</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">14</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99590725</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">6</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'##va'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">14</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">16</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9992327</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">7</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'##in'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">16</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">18</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.97389334</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">12</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Hu'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">33</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">35</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.976115</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">13</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'##gging'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">35</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">40</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.98879766</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">14</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Face'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">41</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">45</span>}, {<span class="hljs-string">'entity'</span>: <span class="hljs-string">'I-LOC'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99321055</span>, <span class="hljs-string">'index'</span>: <span class="hljs-number">16</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Brooklyn'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">49</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">57</span>}]</pre></div> <p>This is the same as what we got from the first pipeline!</p> <h3 class="relative group"><a id="grouping-entities" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#grouping-entities"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Grouping entities</span></h3> <p>Using the offsets to determine the start and end keys for each entity is handy, but that information isn’t strictly necessary. When we want to group the entities together, however, the offsets will save us a lot of messy code. For example, if we wanted to group together the tokens <code>Hu</code>, <code>##gging</code>, and <code>Face</code>, we could make special rules that say the first two should be attached while removing the <code>##</code>, and the <code>Face</code> should be added with a space since it does not begin with <code>##</code> — but that would only work for this particular type of tokenizer. We would have to write another set of rules for a SentencePiece or a Byte-Pair-Encoding tokenizer (discussed later in this chapter).</p> <p>With the offsets, all that custom code goes away: we just can take the span in the original text that begins with the first token and ends with the last token. So, in the case of the tokens <code>Hu</code>, <code>##gging</code>, and <code>Face</code>, we should start at character 33 (the beginning of <code>Hu</code>) and end before character 45 (the end of <code>Face</code>):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>example[<span class="hljs-number">33</span>:<span class="hljs-number">45</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Hugging Face</pre></div> <p>To write the code that post-processes the predictions while grouping entities, we will group together entities that are consecutive and labeled with <code>I-XXX</code>, except for the first one, which can be labeled as <code>B-XXX</code> or <code>I-XXX</code> (so, we stop grouping an entity when we get a <code>O</code>, a new type of entity, or a <code>B-XXX</code> that tells us an entity of the same type is starting):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np results = [] inputs_with_offsets = tokenizer(example, return_offsets_mapping=<span class="hljs-literal">True</span>) tokens = inputs_with_offsets.tokens() offsets = inputs_with_offsets[<span class="hljs-string">"offset_mapping"</span>] idx = <span class="hljs-number">0</span> <span class="hljs-keyword">while</span> idx &lt; <span class="hljs-built_in">len</span>(predictions): pred = predictions[idx] label = model.config.id2label[pred] <span class="hljs-keyword">if</span> label != <span class="hljs-string">"O"</span>: <span class="hljs-comment"># Remove the B- or I-</span> label = label[<span class="hljs-number">2</span>:] start, _ = offsets[idx] <span class="hljs-comment"># Grab all the tokens labeled with I-label</span> all_scores = [] <span class="hljs-keyword">while</span> ( idx &lt; <span class="hljs-built_in">len</span>(predictions) <span class="hljs-keyword">and</span> model.config.id2label[predictions[idx]] == <span class="hljs-string">f"I-<span class="hljs-subst">{label}</span>"</span> ): all_scores.append(probabilities[idx][pred]) _, end = offsets[idx] idx += <span class="hljs-number">1</span> <span class="hljs-comment"># The score is the mean of all the scores of the tokens in that grouped entity</span> score = np.mean(all_scores).item() word = example[start:end] results.append( { <span class="hljs-string">"entity_group"</span>: label, <span class="hljs-string">"score"</span>: score, <span class="hljs-string">"word"</span>: word, <span class="hljs-string">"start"</span>: start, <span class="hljs-string">"end"</span>: end, } ) idx += <span class="hljs-number">1</span> <span class="hljs-built_in">print</span>(results)</pre></div> <p>And we get the same results as with our second pipeline!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'entity_group'</span>: <span class="hljs-string">'PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9981694</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Sylvain'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">11</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">18</span>}, {<span class="hljs-string">'entity_group'</span>: <span class="hljs-string">'ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.97960204</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Hugging Face'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">33</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">45</span>}, {<span class="hljs-string">'entity_group'</span>: <span class="hljs-string">'LOC'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.99321055</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Brooklyn'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">49</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">57</span>}]</pre></div> <p>Another example of a task where these offsets are extremely useful is question answering. Diving into that pipeline, which we’ll do in the next section, will also enable us to take a look at one last feature of the tokenizers in the 🤗 Transformers library: dealing with overflowing tokens when we truncate an input to a given length.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter6/2?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Training a new tokenizer from an old one</a> <a href="/learn/nlp-course/chapter6/3b?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Fast tokenizers in the QA pipeline<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;fast-tokenizers-special-powers&quot;,&quot;url&quot;:&quot;#fast-tokenizers-special-powers&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Batch encoding&quot;,&quot;id&quot;:&quot;batch-encoding&quot;,&quot;url&quot;:&quot;#batch-encoding&quot;},{&quot;title&quot;:&quot;Inside the `token-classification` pipeline&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;inside-the-token-classification-pipeline&quot;,&quot;url&quot;:&quot;#inside-the-token-classification-pipeline&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Getting the base results with the pipeline&quot;,&quot;id&quot;:&quot;getting-the-base-results-with-the-pipeline&quot;,&quot;url&quot;:&quot;#getting-the-base-results-with-the-pipeline&quot;},{&quot;title&quot;:&quot;From inputs to predictions&quot;,&quot;id&quot;:&quot;from-inputs-to-predictions&quot;,&quot;url&quot;:&quot;#from-inputs-to-predictions&quot;},{&quot;title&quot;:&quot;Grouping entities&quot;,&quot;id&quot;:&quot;grouping-entities&quot;,&quot;url&quot;:&quot;#grouping-entities&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#fast-tokenizers-special-powers" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fast-tokenizers-special-powers"><wbr>Fast tokenizers' special powers</a> <a href="#batch-encoding" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-batch-encoding"><wbr>Batch encoding</a> <a href="#inside-the-token-classification-pipeline" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-inside-the-token-classification-pipeline"><wbr>Inside the `token-classification` pipeline</a> <a href="#getting-the-base-results-with-the-pipeline" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-getting-the-base-results-with-the-pipeline"><wbr>Getting the base results with the pipeline</a> <a href="#from-inputs-to-predictions" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-from-inputs-to-predictions"><wbr>From inputs to predictions</a> <a href="#grouping-entities" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-grouping-entities"><wbr>Grouping entities</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter6/3" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter6/3"); } </script> <iframe name="__privateStripeMetricsController0090" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter6%2F3%3Ffw%3Dpt&amp;title=Fast%20tokenizers'%20special%20powers%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:22.269Z
Normalization and pre-tokenization - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter6/4?fw=pt
## [](#normalization-and-pre-tokenization)Normalization and pre-tokenization [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-6-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section4.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section4.ipynb) Before we dive more deeply into the three most common subword tokenization algorithms used with Transformer models (Byte-Pair Encoding \[BPE\], WordPiece, and Unigram), we’ll first take a look at the preprocessing that each tokenizer applies to text. Here’s a high-level overview of the steps in the tokenization pipeline: ![The tokenization pipeline.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/tokenization_pipeline.svg) ![The tokenization pipeline.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/tokenization_pipeline-dark.svg) Before splitting a text into subtokens (according to its model), the tokenizer performs two steps: _normalization_ and _pre-tokenization_. ## [](#normalization)Normalization The normalization step involves some general cleanup, such as removing needless whitespace, lowercasing, and/or removing accents. If you’re familiar with [Unicode normalization](http://www.unicode.org/reports/tr15/) (such as NFC or NFKC), this is also something the tokenizer may apply. The 🤗 Transformers `tokenizer` has an attribute called `backend_tokenizer` that provides access to the underlying tokenizer from the 🤗 Tokenizers library: ``` from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") print(type(tokenizer.backend_tokenizer))``` ``` <class 'tokenizers.Tokenizer'>``` The `normalizer` attribute of the `tokenizer` object has a `normalize_str()` method that we can use to see how the normalization is performed: ``` print(tokenizer.backend_tokenizer.normalizer.normalize_str("Héllò hôw are ü?"))``` In this example, since we picked the `bert-base-uncased` checkpoint, the normalization applied lowercasing and removed the accents. ✏️ **Try it out!** Load a tokenizer from the `bert-base-cased` checkpoint and pass the same example to it. What are the main differences you can see between the cased and uncased versions of the tokenizer? ## [](#pre-tokenization)Pre-tokenization As we will see in the next sections, a tokenizer cannot be trained on raw text alone. Instead, we first need to split the texts into small entities, like words. That’s where the pre-tokenization step comes in. As we saw in [Chapter 2](/course/chapter2), a word-based tokenizer can simply split a raw text into words on whitespace and punctuation. Those words will be the boundaries of the subtokens the tokenizer can learn during its training. To see how a fast tokenizer performs pre-tokenization, we can use the `pre_tokenize_str()` method of the `pre_tokenizer` attribute of the `tokenizer` object: ``` tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str("Hello, how are you?")``` ``` [('Hello', (0, 5)), (',', (5, 6)), ('how', (7, 10)), ('are', (11, 14)), ('you', (16, 19)), ('?', (19, 20))]``` Notice how the tokenizer is already keeping track of the offsets, which is how it can give us the offset mapping we used in the previous section. Here the tokenizer ignores the two spaces and replaces them with just one, but the offset jumps between `are` and `you` to account for that. Since we’re using a BERT tokenizer, the pre-tokenization involves splitting on whitespace and punctuation. Other tokenizers can have different rules for this step. For example, if we use the GPT-2 tokenizer: ``` tokenizer = AutoTokenizer.from_pretrained("gpt2") tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str("Hello, how are you?")``` it will split on whitespace and punctuation as well, but it will keep the spaces and replace them with a `Ġ` symbol, enabling it to recover the original spaces if we decode the tokens: ``` [('Hello', (0, 5)), (',', (5, 6)), ('Ġhow', (6, 10)), ('Ġare', (10, 14)), ('Ġ', (14, 15)), ('Ġyou', (15, 19)), ('?', (19, 20))]``` Also note that unlike the BERT tokenizer, this tokenizer does not ignore the double space. For a last example, let’s have a look at the T5 tokenizer, which is based on the SentencePiece algorithm: ``` tokenizer = AutoTokenizer.from_pretrained("t5-small") tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str("Hello, how are you?")``` ``` [('▁Hello,', (0, 6)), ('▁how', (7, 10)), ('▁are', (11, 14)), ('▁you?', (16, 20))]``` Like the GPT-2 tokenizer, this one keeps spaces and replaces them with a specific token (`_`), but the T5 tokenizer only splits on whitespace, not punctuation. Also note that it added a space by default at the beginning of the sentence (before `Hello`) and ignored the double space between `are` and `you`. Now that we’ve seen a little of how some different tokenizers process text, we can start to explore the underlying algorithms themselves. We’ll begin with a quick look at the broadly widely applicable SentencePiece; then, over the next three sections, we’ll examine how the three main algorithms used for subword tokenization work. ## [](#sentencepiece)SentencePiece [SentencePiece](https://github.com/google/sentencepiece) is a tokenization algorithm for the preprocessing of text that you can use with any of the models we will see in the next three sections. It considers the text as a sequence of Unicode characters, and replaces spaces with a special character, `▁`. Used in conjunction with the Unigram algorithm (see [section 7](/course/chapter7/7)), it doesn’t even require a pre-tokenization step, which is very useful for languages where the space character is not used (like Chinese or Japanese). The other main feature of SentencePiece is _reversible tokenization_: since there is no special treatment of spaces, decoding the tokens is done simply by concatenating them and replacing the `_`s with spaces — this results in the normalized text. As we saw earlier, the BERT tokenizer removes repeating spaces, so its tokenization is not reversible. ## [](#algorithm-overview)Algorithm overview In the following sections, we’ll dive into the three main subword tokenization algorithms: BPE (used by GPT-2 and others), WordPiece (used for example by BERT), and Unigram (used by T5 and others). Before we get started, here’s a quick overview of how they each work. Don’t hesitate to come back to this table after reading each of the next sections if it doesn’t make sense to you yet. | Model | BPE | WordPiece | Unigram | | --- | --- | --- | --- | | Training | Starts from a small vocabulary and learns rules to merge tokens | Starts from a small vocabulary and learns rules to merge tokens | Starts from a large vocabulary and learns rules to remove tokens | | Training step | Merges the tokens corresponding to the most common pair | Merges the tokens corresponding to the pair with the best score based on the frequency of the pair, privileging pairs where each individual token is less frequent | Removes all the tokens in the vocabulary that will minimize the loss computed on the whole corpus | | Learns | Merge rules and a vocabulary | Just a vocabulary | A vocabulary with a score for each token | | Encoding | Splits a word into characters and applies the merges learned during training | Finds the longest subword starting from the beginning that is in the vocabulary, then does the same for the rest of the word | Finds the most likely split into tokens, using the scores learned during training | Now let’s dive into BPE!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Normalization and pre-tokenization - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter6/4"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Normalization and pre-tokenization - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;normalization-and-pre-tokenization&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;normalization&quot;,&quot;title&quot;:&quot;Normalization&quot;},{&quot;local&quot;:&quot;pre-tokenization&quot;,&quot;title&quot;:&quot;Pre-tokenization&quot;},{&quot;local&quot;:&quot;sentencepiece&quot;,&quot;title&quot;:&quot;SentencePiece&quot;},{&quot;local&quot;:&quot;algorithm-overview&quot;,&quot;title&quot;:&quot;Algorithm overview&quot;}],&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter6/4&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Normalization and pre-tokenization</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/2?fw=pt">Training a new tokenizer from an old one </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3?fw=pt">Fast tokenizers' special powers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3b?fw=pt">Fast tokenizers in the QA pipeline </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter6/4?fw=pt">Normalization and pre-tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/5?fw=pt">Byte-Pair Encoding tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/6?fw=pt">WordPiece tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/7?fw=pt">Unigram tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/8?fw=pt">Building a tokenizer, block by block </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/9?fw=pt">Tokenizers, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="normalization-and-pre-tokenization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#normalization-and-pre-tokenization"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Normalization and pre-tokenization</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-6-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section4.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section4.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>Before we dive more deeply into the three most common subword tokenization algorithms used with Transformer models (Byte-Pair Encoding [BPE], WordPiece, and Unigram), we’ll first take a look at the preprocessing that each tokenizer applies to text. Here’s a high-level overview of the steps in the tokenization pipeline:</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/tokenization_pipeline.svg" alt="The tokenization pipeline."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/tokenization_pipeline-dark.svg" alt="The tokenization pipeline."></div> <p>Before splitting a text into subtokens (according to its model), the tokenizer performs two steps: <em>normalization</em> and <em>pre-tokenization</em>.</p> <h2 class="relative group"><a id="normalization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#normalization"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Normalization</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/4IIC2jI9CaU" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The normalization step involves some general cleanup, such as removing needless whitespace, lowercasing, and/or removing accents. If you’re familiar with <a href="http://www.unicode.org/reports/tr15/" rel="nofollow">Unicode normalization</a> (such as NFC or NFKC), this is also something the tokenizer may apply.</p> <p>The 🤗 Transformers <code>tokenizer</code> has an attribute called <code>backend_tokenizer</code> that provides access to the underlying tokenizer from the 🤗 Tokenizers library:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-uncased"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-built_in">type</span>(tokenizer.backend_tokenizer))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>&lt;<span class="hljs-keyword">class</span> <span class="hljs-string">'tokenizers.Tokenizer'</span>&gt;</pre></div> <p>The <code>normalizer</code> attribute of the <code>tokenizer</code> object has a <code>normalize_str()</code> method that we can use to see how the normalization is performed:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(tokenizer.backend_tokenizer.normalizer.normalize_str(<span class="hljs-string">"Héllò hôw are ü?"</span>))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'hello how are u?'</span></pre></div> <p>In this example, since we picked the <code>bert-base-uncased</code> checkpoint, the normalization applied lowercasing and removed the accents.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Load a tokenizer from the <code>bert-base-cased</code> checkpoint and pass the same example to it. What are the main differences you can see between the cased and uncased versions of the tokenizer?</p></div> <h2 class="relative group"><a id="pre-tokenization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pre-tokenization"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Pre-tokenization</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/grlLV8AIXug" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>As we will see in the next sections, a tokenizer cannot be trained on raw text alone. Instead, we first need to split the texts into small entities, like words. That’s where the pre-tokenization step comes in. As we saw in <a href="/course/chapter2">Chapter 2</a>, a word-based tokenizer can simply split a raw text into words on whitespace and punctuation. Those words will be the boundaries of the subtokens the tokenizer can learn during its training.</p> <p>To see how a fast tokenizer performs pre-tokenization, we can use the <code>pre_tokenize_str()</code> method of the <code>pre_tokenizer</code> attribute of the <code>tokenizer</code> object:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(<span class="hljs-string">"Hello, how are you?"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[(<span class="hljs-string">'Hello'</span>, (<span class="hljs-number">0</span>, <span class="hljs-number">5</span>)), (<span class="hljs-string">','</span>, (<span class="hljs-number">5</span>, <span class="hljs-number">6</span>)), (<span class="hljs-string">'how'</span>, (<span class="hljs-number">7</span>, <span class="hljs-number">10</span>)), (<span class="hljs-string">'are'</span>, (<span class="hljs-number">11</span>, <span class="hljs-number">14</span>)), (<span class="hljs-string">'you'</span>, (<span class="hljs-number">16</span>, <span class="hljs-number">19</span>)), (<span class="hljs-string">'?'</span>, (<span class="hljs-number">19</span>, <span class="hljs-number">20</span>))]</pre></div> <p>Notice how the tokenizer is already keeping track of the offsets, which is how it can give us the offset mapping we used in the previous section. Here the tokenizer ignores the two spaces and replaces them with just one, but the offset jumps between <code>are</code> and <code>you</code> to account for that.</p> <p>Since we’re using a BERT tokenizer, the pre-tokenization involves splitting on whitespace and punctuation. Other tokenizers can have different rules for this step. For example, if we use the GPT-2 tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"gpt2"</span>) tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(<span class="hljs-string">"Hello, how are you?"</span>)</pre></div> <p>it will split on whitespace and punctuation as well, but it will keep the spaces and replace them with a <code>Ġ</code> symbol, enabling it to recover the original spaces if we decode the tokens:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[(<span class="hljs-string">'Hello'</span>, (<span class="hljs-number">0</span>, <span class="hljs-number">5</span>)), (<span class="hljs-string">','</span>, (<span class="hljs-number">5</span>, <span class="hljs-number">6</span>)), (<span class="hljs-string">'Ġhow'</span>, (<span class="hljs-number">6</span>, <span class="hljs-number">10</span>)), (<span class="hljs-string">'Ġare'</span>, (<span class="hljs-number">10</span>, <span class="hljs-number">14</span>)), (<span class="hljs-string">'Ġ'</span>, (<span class="hljs-number">14</span>, <span class="hljs-number">15</span>)), (<span class="hljs-string">'Ġyou'</span>, (<span class="hljs-number">15</span>, <span class="hljs-number">19</span>)), (<span class="hljs-string">'?'</span>, (<span class="hljs-number">19</span>, <span class="hljs-number">20</span>))]</pre></div> <p>Also note that unlike the BERT tokenizer, this tokenizer does not ignore the double space.</p> <p>For a last example, let’s have a look at the T5 tokenizer, which is based on the SentencePiece algorithm:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"t5-small"</span>) tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(<span class="hljs-string">"Hello, how are you?"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[(<span class="hljs-string">'▁Hello,'</span>, (<span class="hljs-number">0</span>, <span class="hljs-number">6</span>)), (<span class="hljs-string">'▁how'</span>, (<span class="hljs-number">7</span>, <span class="hljs-number">10</span>)), (<span class="hljs-string">'▁are'</span>, (<span class="hljs-number">11</span>, <span class="hljs-number">14</span>)), (<span class="hljs-string">'▁you?'</span>, (<span class="hljs-number">16</span>, <span class="hljs-number">20</span>))]</pre></div> <p>Like the GPT-2 tokenizer, this one keeps spaces and replaces them with a specific token (<code>_</code>), but the T5 tokenizer only splits on whitespace, not punctuation. Also note that it added a space by default at the beginning of the sentence (before <code>Hello</code>) and ignored the double space between <code>are</code> and <code>you</code>.</p> <p>Now that we’ve seen a little of how some different tokenizers process text, we can start to explore the underlying algorithms themselves. We’ll begin with a quick look at the broadly widely applicable SentencePiece; then, over the next three sections, we’ll examine how the three main algorithms used for subword tokenization work.</p> <h2 class="relative group"><a id="sentencepiece" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#sentencepiece"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>SentencePiece</span></h2> <p><a href="https://github.com/google/sentencepiece" rel="nofollow">SentencePiece</a> is a tokenization algorithm for the preprocessing of text that you can use with any of the models we will see in the next three sections. It considers the text as a sequence of Unicode characters, and replaces spaces with a special character, <code>▁</code>. Used in conjunction with the Unigram algorithm (see <a href="/course/chapter7/7">section 7</a>), it doesn’t even require a pre-tokenization step, which is very useful for languages where the space character is not used (like Chinese or Japanese).</p> <p>The other main feature of SentencePiece is <em>reversible tokenization</em>: since there is no special treatment of spaces, decoding the tokens is done simply by concatenating them and replacing the <code>_</code>s with spaces — this results in the normalized text. As we saw earlier, the BERT tokenizer removes repeating spaces, so its tokenization is not reversible.</p> <h2 class="relative group"><a id="algorithm-overview" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#algorithm-overview"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Algorithm overview</span></h2> <p>In the following sections, we’ll dive into the three main subword tokenization algorithms: BPE (used by GPT-2 and others), WordPiece (used for example by BERT), and Unigram (used by T5 and others). Before we get started, here’s a quick overview of how they each work. Don’t hesitate to come back to this table after reading each of the next sections if it doesn’t make sense to you yet.</p> <table><thead><tr><th align="center">Model</th> <th align="center">BPE</th> <th align="center">WordPiece</th> <th align="center">Unigram</th></tr></thead> <tbody><tr><td align="center">Training</td> <td align="center">Starts from a small vocabulary and learns rules to merge tokens</td> <td align="center">Starts from a small vocabulary and learns rules to merge tokens</td> <td align="center">Starts from a large vocabulary and learns rules to remove tokens</td></tr> <tr><td align="center">Training step</td> <td align="center">Merges the tokens corresponding to the most common pair</td> <td align="center">Merges the tokens corresponding to the pair with the best score based on the frequency of the pair, privileging pairs where each individual token is less frequent</td> <td align="center">Removes all the tokens in the vocabulary that will minimize the loss computed on the whole corpus</td></tr> <tr><td align="center">Learns</td> <td align="center">Merge rules and a vocabulary</td> <td align="center">Just a vocabulary</td> <td align="center">A vocabulary with a score for each token</td></tr> <tr><td align="center">Encoding</td> <td align="center">Splits a word into characters and applies the merges learned during training</td> <td align="center">Finds the longest subword starting from the beginning that is in the vocabulary, then does the same for the rest of the word</td> <td align="center">Finds the most likely split into tokens, using the scores learned during training</td></tr></tbody></table> <p>Now let’s dive into BPE!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter6/3b?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Fast tokenizers in the QA pipeline</a> <a href="/learn/nlp-course/chapter6/5?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Byte-Pair Encoding tokenization<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;normalization-and-pre-tokenization&quot;,&quot;url&quot;:&quot;#normalization-and-pre-tokenization&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Normalization&quot;,&quot;id&quot;:&quot;normalization&quot;,&quot;url&quot;:&quot;#normalization&quot;},{&quot;title&quot;:&quot;Pre-tokenization&quot;,&quot;id&quot;:&quot;pre-tokenization&quot;,&quot;url&quot;:&quot;#pre-tokenization&quot;},{&quot;title&quot;:&quot;SentencePiece&quot;,&quot;id&quot;:&quot;sentencepiece&quot;,&quot;url&quot;:&quot;#sentencepiece&quot;},{&quot;title&quot;:&quot;Algorithm overview&quot;,&quot;id&quot;:&quot;algorithm-overview&quot;,&quot;url&quot;:&quot;#algorithm-overview&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#normalization-and-pre-tokenization" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-normalization-and-pre-tokenization"><wbr>Normalization and pre-tokenization</a> <a href="#normalization" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-normalization"><wbr>Normalization</a> <a href="#pre-tokenization" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-pre-tokenization"><wbr>Pre-tokenization</a> <a href="#sentencepiece" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-sentencepiece"><wbr>Sentence<wbr>Piece</a> <a href="#algorithm-overview" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-algorithm-overview"><wbr>Algorithm overview</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter6/4" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter6/4"); } </script> <iframe name="__privateStripeMetricsController2230" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter6%2F4%3Ffw%3Dpt&amp;title=Normalization%20and%20pre-tokenization%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:22.989Z
Byte-Pair Encoding tokenization - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter6/5?fw=pt
## [](#byte-pair-encoding-tokenization)Byte-Pair Encoding tokenization [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-6-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section5.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section5.ipynb) Byte-Pair Encoding (BPE) was initially developed as an algorithm to compress texts, and then used by OpenAI for tokenization when pretraining the GPT model. It’s used by a lot of Transformer models, including GPT, GPT-2, RoBERTa, BART, and DeBERTa. 💡 This section covers BPE in depth, going as far as showing a full implementation. You can skip to the end if you just want a general overview of the tokenization algorithm. ## [](#training-algorithm)Training algorithm BPE training starts by computing the unique set of words used in the corpus (after the normalization and pre-tokenization steps are completed), then building the vocabulary by taking all the symbols used to write those words. As a very simple example, let’s say our corpus uses these five words: ``` "hug", "pug", "pun", "bun", "hugs"``` The base vocabulary will then be `["b", "g", "h", "n", "p", "s", "u"]`. For real-world cases, that base vocabulary will contain all the ASCII characters, at the very least, and probably some Unicode characters as well. If an example you are tokenizing uses a character that is not in the training corpus, that character will be converted to the unknown token. That’s one reason why lots of NLP models are very bad at analyzing content with emojis, for instance. The GPT-2 and RoBERTa tokenizers (which are pretty similar) have a clever way to deal with this: they don’t look at words as being written with Unicode characters, but with bytes. This way the base vocabulary has a small size (256), but every character you can think of will still be included and not end up being converted to the unknown token. This trick is called _byte-level BPE_. After getting this base vocabulary, we add new tokens until the desired vocabulary size is reached by learning _merges_, which are rules to merge two elements of the existing vocabulary together into a new one. So, at the beginning these merges will create tokens with two characters, and then, as training progresses, longer subwords. At any step during the tokenizer training, the BPE algorithm will search for the most frequent pair of existing tokens (by “pair,” here we mean two consecutive tokens in a word). That most frequent pair is the one that will be merged, and we rinse and repeat for the next step. Going back to our previous example, let’s assume the words had the following frequencies: ``` ("hug", 10), ("pug", 5), ("pun", 12), ("bun", 4), ("hugs", 5)``` meaning `"hug"` was present 10 times in the corpus, `"pug"` 5 times, `"pun"` 12 times, `"bun"` 4 times, and `"hugs"` 5 times. We start the training by splitting each word into characters (the ones that form our initial vocabulary) so we can see each word as a list of tokens: ``` ("h" "u" "g", 10), ("p" "u" "g", 5), ("p" "u" "n", 12), ("b" "u" "n", 4), ("h" "u" "g" "s", 5)``` Then we look at pairs. The pair `("h", "u")` is present in the words `"hug"` and `"hugs"`, so 15 times total in the corpus. It’s not the most frequent pair, though: that honor belongs to `("u", "g")`, which is present in `"hug"`, `"pug"`, and `"hugs"`, for a grand total of 20 times in the vocabulary. Thus, the first merge rule learned by the tokenizer is `("u", "g") -> "ug"`, which means that `"ug"` will be added to the vocabulary, and the pair should be merged in all the words of the corpus. At the end of this stage, the vocabulary and corpus look like this: ``` Vocabulary: ["b", "g", "h", "n", "p", "s", "u", "ug"] Corpus: ("h" "ug", 10), ("p" "ug", 5), ("p" "u" "n", 12), ("b" "u" "n", 4), ("h" "ug" "s", 5)``` Now we have some pairs that result in a token longer than two characters: the pair `("h", "ug")`, for instance (present 15 times in the corpus). The most frequent pair at this stage is `("u", "n")`, however, present 16 times in the corpus, so the second merge rule learned is `("u", "n") -> "un"`. Adding that to the vocabulary and merging all existing occurrences leads us to: ``` Vocabulary: ["b", "g", "h", "n", "p", "s", "u", "ug", "un"] Corpus: ("h" "ug", 10), ("p" "ug", 5), ("p" "un", 12), ("b" "un", 4), ("h" "ug" "s", 5)``` Now the most frequent pair is `("h", "ug")`, so we learn the merge rule `("h", "ug") -> "hug"`, which gives us our first three-letter token. After the merge, the corpus looks like this: ``` Vocabulary: ["b", "g", "h", "n", "p", "s", "u", "ug", "un", "hug"] Corpus: ("hug", 10), ("p" "ug", 5), ("p" "un", 12), ("b" "un", 4), ("hug" "s", 5)``` And we continue like this until we reach the desired vocabulary size. ✏️ **Now your turn!** What do you think the next merge rule will be? ## [](#tokenization-algorithm)Tokenization algorithm Tokenization follows the training process closely, in the sense that new inputs are tokenized by applying the following steps: 1. Normalization 2. Pre-tokenization 3. Splitting the words into individual characters 4. Applying the merge rules learned in order on those splits Let’s take the example we used during training, with the three merge rules learned: ``` ("u", "g") -> "ug" ("u", "n") -> "un" ("h", "ug") -> "hug"``` The word `"bug"` will be tokenized as `["b", "ug"]`. `"mug"`, however, will be tokenized as `["[UNK]", "ug"]` since the letter `"m"` was not in the base vocabulary. Likewise, the word `"thug"` will be tokenized as `["[UNK]", "hug"]`: the letter `"t"` is not in the base vocabulary, and applying the merge rules results first in `"u"` and `"g"` being merged and then `"hu"` and `"g"` being merged. ✏️ **Now your turn!** How do you think the word `"unhug"` will be tokenized? ## [](#implementing-bpe)Implementing BPE Now let’s take a look at an implementation of the BPE algorithm. This won’t be an optimized version you can actually use on a big corpus; we just want to show you the code so you can understand the algorithm a little bit better. First we need a corpus, so let’s create a simple one with a few sentences: ``` corpus = [ "This is the Hugging Face Course.", "This chapter is about tokenization.", "This section shows several tokenizer algorithms.", "Hopefully, you will be able to understand how they are trained and generate tokens.", ]``` Next, we need to pre-tokenize that corpus into words. Since we are replicating a BPE tokenizer (like GPT-2), we will use the `gpt2` tokenizer for the pre-tokenization: ``` from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("gpt2")``` Then we compute the frequencies of each word in the corpus as we do the pre-tokenization: ``` from collections import defaultdict word_freqs = defaultdict(int) for text in corpus: words_with_offsets = tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(text) new_words = [word for word, offset in words_with_offsets] for word in new_words: word_freqs[word] += 1 print(word_freqs)``` ``` defaultdict(int, {'This': 3, 'Ġis': 2, 'Ġthe': 1, 'ĠHugging': 1, 'ĠFace': 1, 'ĠCourse': 1, '.': 4, 'Ġchapter': 1, 'Ġabout': 1, 'Ġtokenization': 1, 'Ġsection': 1, 'Ġshows': 1, 'Ġseveral': 1, 'Ġtokenizer': 1, 'Ġalgorithms': 1, 'Hopefully': 1, ',': 1, 'Ġyou': 1, 'Ġwill': 1, 'Ġbe': 1, 'Ġable': 1, 'Ġto': 1, 'Ġunderstand': 1, 'Ġhow': 1, 'Ġthey': 1, 'Ġare': 1, 'Ġtrained': 1, 'Ġand': 1, 'Ġgenerate': 1, 'Ġtokens': 1})``` The next step is to compute the base vocabulary, formed by all the characters used in the corpus: ``` alphabet = [] for word in word_freqs.keys(): for letter in word: if letter not in alphabet: alphabet.append(letter) alphabet.sort() print(alphabet)``` ``` [ ',', '.', 'C', 'F', 'H', 'T', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'y', 'z', 'Ġ']``` We also add the special tokens used by the model at the beginning of that vocabulary. In the case of GPT-2, the only special token is `"<|endoftext|>"`: ``` vocab = ["<|endoftext|>"] + alphabet.copy()``` We now need to split each word into individual characters, to be able to start training: ``` splits = {word: [c for c in word] for word in word_freqs.keys()}``` Now that we are ready for training, let’s write a function that computes the frequency of each pair. We’ll need to use this at each step of the training: ``` def compute_pair_freqs(splits): pair_freqs = defaultdict(int) for word, freq in word_freqs.items(): split = splits[word] if len(split) == 1: continue for i in range(len(split) - 1): pair = (split[i], split[i + 1]) pair_freqs[pair] += freq return pair_freqs``` Let’s have a look at a part of this dictionary after the initial splits: ``` pair_freqs = compute_pair_freqs(splits) for i, key in enumerate(pair_freqs.keys()): print(f"{key}: {pair_freqs[key]}") if i >= 5: break``` ``` ('T', 'h'): 3 ('h', 'i'): 3 ('i', 's'): 5 ('Ġ', 'i'): 2 ('Ġ', 't'): 7 ('t', 'h'): 3``` Now, finding the most frequent pair only takes a quick loop: ``` best_pair = "" max_freq = None for pair, freq in pair_freqs.items(): if max_freq is None or max_freq < freq: best_pair = pair max_freq = freq print(best_pair, max_freq)``` So the first merge to learn is `('Ġ', 't') -> 'Ġt'`, and we add `'Ġt'` to the vocabulary: ``` merges = {("Ġ", "t"): "Ġt"} vocab.append("Ġt")``` To continue, we need to apply that merge in our `splits` dictionary. Let’s write another function for this: ``` def merge_pair(a, b, splits): for word in word_freqs: split = splits[word] if len(split) == 1: continue i = 0 while i < len(split) - 1: if split[i] == a and split[i + 1] == b: split = split[:i] + [a + b] + split[i + 2 :] else: i += 1 splits[word] = split return splits``` And we can have a look at the result of the first merge: ``` splits = merge_pair("Ġ", "t", splits) print(splits["Ġtrained"])``` ``` ['Ġt', 'r', 'a', 'i', 'n', 'e', 'd']``` Now we have everything we need to loop until we have learned all the merges we want. Let’s aim for a vocab size of 50: ``` vocab_size = 50 while len(vocab) < vocab_size: pair_freqs = compute_pair_freqs(splits) best_pair = "" max_freq = None for pair, freq in pair_freqs.items(): if max_freq is None or max_freq < freq: best_pair = pair max_freq = freq splits = merge_pair(*best_pair, splits) merges[best_pair] = best_pair[0] + best_pair[1] vocab.append(best_pair[0] + best_pair[1])``` As a result, we’ve learned 19 merge rules (the initial vocabulary had a size of 31 — 30 characters in the alphabet, plus the special token): ``` {('Ġ', 't'): 'Ġt', ('i', 's'): 'is', ('e', 'r'): 'er', ('Ġ', 'a'): 'Ġa', ('Ġt', 'o'): 'Ġto', ('e', 'n'): 'en', ('T', 'h'): 'Th', ('Th', 'is'): 'This', ('o', 'u'): 'ou', ('s', 'e'): 'se', ('Ġto', 'k'): 'Ġtok', ('Ġtok', 'en'): 'Ġtoken', ('n', 'd'): 'nd', ('Ġ', 'is'): 'Ġis', ('Ġt', 'h'): 'Ġth', ('Ġth', 'e'): 'Ġthe', ('i', 'n'): 'in', ('Ġa', 'b'): 'Ġab', ('Ġtoken', 'i'): 'Ġtokeni'}``` And the vocabulary is composed of the special token, the initial alphabet, and all the results of the merges: ``` ['<|endoftext|>', ',', '.', 'C', 'F', 'H', 'T', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'y', 'z', 'Ġ', 'Ġt', 'is', 'er', 'Ġa', 'Ġto', 'en', 'Th', 'This', 'ou', 'se', 'Ġtok', 'Ġtoken', 'nd', 'Ġis', 'Ġth', 'Ġthe', 'in', 'Ġab', 'Ġtokeni']``` 💡 Using `train_new_from_iterator()` on the same corpus won’t result in the exact same vocabulary. This is because when there is a choice of the most frequent pair, we selected the first one encountered, while the 🤗 Tokenizers library selects the first one based on its inner IDs. To tokenize a new text, we pre-tokenize it, split it, then apply all the merge rules learned: ``` def tokenize(text): pre_tokenize_result = tokenizer._tokenizer.pre_tokenizer.pre_tokenize_str(text) pre_tokenized_text = [word for word, offset in pre_tokenize_result] splits = [[l for l in word] for word in pre_tokenized_text] for pair, merge in merges.items(): for idx, split in enumerate(splits): i = 0 while i < len(split) - 1: if split[i] == pair[0] and split[i + 1] == pair[1]: split = split[:i] + [merge] + split[i + 2 :] else: i += 1 splits[idx] = split return sum(splits, [])``` We can try this on any text composed of characters in the alphabet: ``` tokenize("This is not a token.")``` ``` ['This', 'Ġis', 'Ġ', 'n', 'o', 't', 'Ġa', 'Ġtoken', '.']``` ⚠️ Our implementation will throw an error if there is an unknown character since we didn’t do anything to handle them. GPT-2 doesn’t actually have an unknown token (it’s impossible to get an unknown character when using byte-level BPE), but this could happen here because we did not include all the possible bytes in the initial vocabulary. This aspect of BPE is beyond the scope of this section, so we’ve left the details out. That’s it for the BPE algorithm! Next, we’ll have a look at WordPiece.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Byte-Pair Encoding tokenization - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter6/5"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Byte-Pair Encoding tokenization - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;byte-pair-encoding-tokenization&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;training-algorithm&quot;,&quot;title&quot;:&quot;Training algorithm&quot;},{&quot;local&quot;:&quot;tokenization-algorithm&quot;,&quot;title&quot;:&quot;Tokenization algorithm&quot;},{&quot;local&quot;:&quot;implementing-bpe&quot;,&quot;title&quot;:&quot;Implementing BPE&quot;}],&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter6/5&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Byte-Pair Encoding tokenization</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/2?fw=pt">Training a new tokenizer from an old one </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3?fw=pt">Fast tokenizers' special powers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3b?fw=pt">Fast tokenizers in the QA pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/4?fw=pt">Normalization and pre-tokenization </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter6/5?fw=pt">Byte-Pair Encoding tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/6?fw=pt">WordPiece tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/7?fw=pt">Unigram tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/8?fw=pt">Building a tokenizer, block by block </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/9?fw=pt">Tokenizers, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="byte-pair-encoding-tokenization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#byte-pair-encoding-tokenization"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Byte-Pair Encoding tokenization</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-6-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section5.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section5.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>Byte-Pair Encoding (BPE) was initially developed as an algorithm to compress texts, and then used by OpenAI for tokenization when pretraining the GPT model. It’s used by a lot of Transformer models, including GPT, GPT-2, RoBERTa, BART, and DeBERTa.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/HEikzVL-lZU" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 This section covers BPE in depth, going as far as showing a full implementation. You can skip to the end if you just want a general overview of the tokenization algorithm.</p></div> <h2 class="relative group"><a id="training-algorithm" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-algorithm"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training algorithm</span></h2> <p>BPE training starts by computing the unique set of words used in the corpus (after the normalization and pre-tokenization steps are completed), then building the vocabulary by taking all the symbols used to write those words. As a very simple example, let’s say our corpus uses these five words:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">"hug"</span>, <span class="hljs-string">"pug"</span>, <span class="hljs-string">"pun"</span>, <span class="hljs-string">"bun"</span>, <span class="hljs-string">"hugs"</span></pre></div> <p>The base vocabulary will then be <code>["b", "g", "h", "n", "p", "s", "u"]</code>. For real-world cases, that base vocabulary will contain all the ASCII characters, at the very least, and probably some Unicode characters as well. If an example you are tokenizing uses a character that is not in the training corpus, that character will be converted to the unknown token. That’s one reason why lots of NLP models are very bad at analyzing content with emojis, for instance.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>The GPT-2 and RoBERTa tokenizers (which are pretty similar) have a clever way to deal with this: they don’t look at words as being written with Unicode characters, but with bytes. This way the base vocabulary has a small size (256), but every character you can think of will still be included and not end up being converted to the unknown token. This trick is called <em>byte-level BPE</em>.</p></div> <p>After getting this base vocabulary, we add new tokens until the desired vocabulary size is reached by learning <em>merges</em>, which are rules to merge two elements of the existing vocabulary together into a new one. So, at the beginning these merges will create tokens with two characters, and then, as training progresses, longer subwords.</p> <p>At any step during the tokenizer training, the BPE algorithm will search for the most frequent pair of existing tokens (by “pair,” here we mean two consecutive tokens in a word). That most frequent pair is the one that will be merged, and we rinse and repeat for the next step.</p> <p>Going back to our previous example, let’s assume the words had the following frequencies:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-string">"hug"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">10</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"pug"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"pun"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">12</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"bun"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"hugs"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>)</pre></div> <p>meaning <code>"hug"</code> was present 10 times in the corpus, <code>"pug"</code> 5 times, <code>"pun"</code> 12 times, <code>"bun"</code> 4 times, and <code>"hugs"</code> 5 times. We start the training by splitting each word into characters (the ones that form our initial vocabulary) so we can see each word as a list of tokens:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-string">"h"</span> <span class="hljs-string">"u"</span> <span class="hljs-string">"g"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">10</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"p"</span> <span class="hljs-string">"u"</span> <span class="hljs-string">"g"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"p"</span> <span class="hljs-string">"u"</span> <span class="hljs-string">"n"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">12</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"b"</span> <span class="hljs-string">"u"</span> <span class="hljs-string">"n"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"h"</span> <span class="hljs-string">"u"</span> <span class="hljs-string">"g"</span> <span class="hljs-string">"s"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>)</pre></div> <p>Then we look at pairs. The pair <code>("h", "u")</code> is present in the words <code>"hug"</code> and <code>"hugs"</code>, so 15 times total in the corpus. It’s not the most frequent pair, though: that honor belongs to <code>("u", "g")</code>, which is present in <code>"hug"</code>, <code>"pug"</code>, and <code>"hugs"</code>, for a grand total of 20 times in the vocabulary.</p> <p>Thus, the first merge rule learned by the tokenizer is <code>("u", "g") -&gt; "ug"</code>, which means that <code>"ug"</code> will be added to the vocabulary, and the pair should be merged in all the words of the corpus. At the end of this stage, the vocabulary and corpus look like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-symbol">Vocabulary:</span> [<span class="hljs-string">"b"</span>, <span class="hljs-string">"g"</span>, <span class="hljs-string">"h"</span>, <span class="hljs-string">"n"</span>, <span class="hljs-string">"p"</span>, <span class="hljs-string">"s"</span>, <span class="hljs-string">"u"</span>, <span class="hljs-string">"ug"</span>] <span class="hljs-symbol">Corpus:</span> (<span class="hljs-string">"h"</span> <span class="hljs-string">"ug"</span>, <span class="hljs-number">10</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"ug"</span>, <span class="hljs-number">5</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"u"</span> <span class="hljs-string">"n"</span>, <span class="hljs-number">12</span>), (<span class="hljs-string">"b"</span> <span class="hljs-string">"u"</span> <span class="hljs-string">"n"</span>, <span class="hljs-number">4</span>), (<span class="hljs-string">"h"</span> <span class="hljs-string">"ug"</span> <span class="hljs-string">"s"</span>, <span class="hljs-number">5</span>)</pre></div> <p>Now we have some pairs that result in a token longer than two characters: the pair <code>("h", "ug")</code>, for instance (present 15 times in the corpus). The most frequent pair at this stage is <code>("u", "n")</code>, however, present 16 times in the corpus, so the second merge rule learned is <code>("u", "n") -&gt; "un"</code>. Adding that to the vocabulary and merging all existing occurrences leads us to:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-symbol">Vocabulary:</span> [<span class="hljs-string">"b"</span>, <span class="hljs-string">"g"</span>, <span class="hljs-string">"h"</span>, <span class="hljs-string">"n"</span>, <span class="hljs-string">"p"</span>, <span class="hljs-string">"s"</span>, <span class="hljs-string">"u"</span>, <span class="hljs-string">"ug"</span>, <span class="hljs-string">"un"</span>] <span class="hljs-symbol">Corpus:</span> (<span class="hljs-string">"h"</span> <span class="hljs-string">"ug"</span>, <span class="hljs-number">10</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"ug"</span>, <span class="hljs-number">5</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"un"</span>, <span class="hljs-number">12</span>), (<span class="hljs-string">"b"</span> <span class="hljs-string">"un"</span>, <span class="hljs-number">4</span>), (<span class="hljs-string">"h"</span> <span class="hljs-string">"ug"</span> <span class="hljs-string">"s"</span>, <span class="hljs-number">5</span>)</pre></div> <p>Now the most frequent pair is <code>("h", "ug")</code>, so we learn the merge rule <code>("h", "ug") -&gt; "hug"</code>, which gives us our first three-letter token. After the merge, the corpus looks like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-symbol">Vocabulary:</span> [<span class="hljs-string">"b"</span>, <span class="hljs-string">"g"</span>, <span class="hljs-string">"h"</span>, <span class="hljs-string">"n"</span>, <span class="hljs-string">"p"</span>, <span class="hljs-string">"s"</span>, <span class="hljs-string">"u"</span>, <span class="hljs-string">"ug"</span>, <span class="hljs-string">"un"</span>, <span class="hljs-string">"hug"</span>] <span class="hljs-symbol">Corpus:</span> (<span class="hljs-string">"hug"</span>, <span class="hljs-number">10</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"ug"</span>, <span class="hljs-number">5</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"un"</span>, <span class="hljs-number">12</span>), (<span class="hljs-string">"b"</span> <span class="hljs-string">"un"</span>, <span class="hljs-number">4</span>), (<span class="hljs-string">"hug"</span> <span class="hljs-string">"s"</span>, <span class="hljs-number">5</span>)</pre></div> <p>And we continue like this until we reach the desired vocabulary size.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Now your turn!</strong> What do you think the next merge rule will be?</p></div> <h2 class="relative group"><a id="tokenization-algorithm" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tokenization-algorithm"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Tokenization algorithm</span></h2> <p>Tokenization follows the training process closely, in the sense that new inputs are tokenized by applying the following steps:</p> <ol><li>Normalization</li> <li>Pre-tokenization</li> <li>Splitting the words into individual characters</li> <li>Applying the merge rules learned in order on those splits</li></ol> <p>Let’s take the example we used during training, with the three merge rules learned:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-function"><span class="hljs-params">(<span class="hljs-string">"u"</span>, <span class="hljs-string">"g"</span>)</span> -&gt;</span> <span class="hljs-string">"ug"</span> <span class="hljs-function"><span class="hljs-params">(<span class="hljs-string">"u"</span>, <span class="hljs-string">"n"</span>)</span> -&gt;</span> <span class="hljs-string">"un"</span> <span class="hljs-function"><span class="hljs-params">(<span class="hljs-string">"h"</span>, <span class="hljs-string">"ug"</span>)</span> -&gt;</span> <span class="hljs-string">"hug"</span></pre></div> <p>The word <code>"bug"</code> will be tokenized as <code>["b", "ug"]</code>. <code>"mug"</code>, however, will be tokenized as <code>["[UNK]", "ug"]</code> since the letter <code>"m"</code> was not in the base vocabulary. Likewise, the word <code>"thug"</code> will be tokenized as <code>["[UNK]", "hug"]</code>: the letter <code>"t"</code> is not in the base vocabulary, and applying the merge rules results first in <code>"u"</code> and <code>"g"</code> being merged and then <code>"hu"</code> and <code>"g"</code> being merged.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Now your turn!</strong> How do you think the word <code>"unhug"</code> will be tokenized?</p></div> <h2 class="relative group"><a id="implementing-bpe" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#implementing-bpe"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Implementing BPE</span></h2> <p>Now let’s take a look at an implementation of the BPE algorithm. This won’t be an optimized version you can actually use on a big corpus; we just want to show you the code so you can understand the algorithm a little bit better.</p> <p>First we need a corpus, so let’s create a simple one with a few sentences:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>corpus = [ <span class="hljs-string">"This is the Hugging Face Course."</span>, <span class="hljs-string">"This chapter is about tokenization."</span>, <span class="hljs-string">"This section shows several tokenizer algorithms."</span>, <span class="hljs-string">"Hopefully, you will be able to understand how they are trained and generate tokens."</span>, ]</pre></div> <p>Next, we need to pre-tokenize that corpus into words. Since we are replicating a BPE tokenizer (like GPT-2), we will use the <code>gpt2</code> tokenizer for the pre-tokenization:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"gpt2"</span>)</pre></div> <p>Then we compute the frequencies of each word in the corpus as we do the pre-tokenization:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> defaultdict word_freqs = defaultdict(<span class="hljs-built_in">int</span>) <span class="hljs-keyword">for</span> text <span class="hljs-keyword">in</span> corpus: words_with_offsets = tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(text) new_words = [word <span class="hljs-keyword">for</span> word, offset <span class="hljs-keyword">in</span> words_with_offsets] <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> new_words: word_freqs[word] += <span class="hljs-number">1</span> <span class="hljs-built_in">print</span>(word_freqs)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>defaultdict(<span class="hljs-built_in">int</span>, {<span class="hljs-string">'This'</span>: <span class="hljs-number">3</span>, <span class="hljs-string">'Ġis'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'Ġthe'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'ĠHugging'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'ĠFace'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'ĠCourse'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'.'</span>: <span class="hljs-number">4</span>, <span class="hljs-string">'Ġchapter'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġabout'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġtokenization'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġsection'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġshows'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġseveral'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġtokenizer'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġalgorithms'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Hopefully'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">','</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġyou'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġwill'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġbe'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġable'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġto'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġunderstand'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġhow'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġthey'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġare'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġtrained'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġand'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġgenerate'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Ġtokens'</span>: <span class="hljs-number">1</span>})</pre></div> <p>The next step is to compute the base vocabulary, formed by all the characters used in the corpus:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>alphabet = [] <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> word_freqs.keys(): <span class="hljs-keyword">for</span> letter <span class="hljs-keyword">in</span> word: <span class="hljs-keyword">if</span> letter <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> alphabet: alphabet.append(letter) alphabet.sort() <span class="hljs-built_in">print</span>(alphabet)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[ <span class="hljs-string">','</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'C'</span>, <span class="hljs-string">'F'</span>, <span class="hljs-string">'H'</span>, <span class="hljs-string">'T'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>, <span class="hljs-string">'d'</span>, <span class="hljs-string">'e'</span>, <span class="hljs-string">'f'</span>, <span class="hljs-string">'g'</span>, <span class="hljs-string">'h'</span>, <span class="hljs-string">'i'</span>, <span class="hljs-string">'k'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'m'</span>, <span class="hljs-string">'n'</span>, <span class="hljs-string">'o'</span>, <span class="hljs-string">'p'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'t'</span>, <span class="hljs-string">'u'</span>, <span class="hljs-string">'v'</span>, <span class="hljs-string">'w'</span>, <span class="hljs-string">'y'</span>, <span class="hljs-string">'z'</span>, <span class="hljs-string">'Ġ'</span>]</pre></div> <p>We also add the special tokens used by the model at the beginning of that vocabulary. In the case of GPT-2, the only special token is <code>"&lt;|endoftext|&gt;"</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>vocab = [<span class="hljs-string">"&lt;|endoftext|&gt;"</span>] + alphabet.copy()</pre></div> <p>We now need to split each word into individual characters, to be able to start training:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>splits = {word: [c <span class="hljs-keyword">for</span> c <span class="hljs-keyword">in</span> word] <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> word_freqs.keys()}</pre></div> <p>Now that we are ready for training, let’s write a function that computes the frequency of each pair. We’ll need to use this at each step of the training:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_pair_freqs</span>(<span class="hljs-params">splits</span>): pair_freqs = defaultdict(<span class="hljs-built_in">int</span>) <span class="hljs-keyword">for</span> word, freq <span class="hljs-keyword">in</span> word_freqs.items(): split = splits[word] <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(split) == <span class="hljs-number">1</span>: <span class="hljs-keyword">continue</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(split) - <span class="hljs-number">1</span>): pair = (split[i], split[i + <span class="hljs-number">1</span>]) pair_freqs[pair] += freq <span class="hljs-keyword">return</span> pair_freqs</pre></div> <p>Let’s have a look at a part of this dictionary after the initial splits:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pair_freqs = compute_pair_freqs(splits) <span class="hljs-keyword">for</span> i, key <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(pair_freqs.keys()): <span class="hljs-built_in">print</span>(<span class="hljs-string">f"<span class="hljs-subst">{key}</span>: <span class="hljs-subst">{pair_freqs[key]}</span>"</span>) <span class="hljs-keyword">if</span> i &gt;= <span class="hljs-number">5</span>: <span class="hljs-keyword">break</span></pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-string">'T'</span>, <span class="hljs-string">'h'</span>): <span class="hljs-number">3</span> (<span class="hljs-string">'h'</span>, <span class="hljs-string">'i'</span>): <span class="hljs-number">3</span> (<span class="hljs-string">'i'</span>, <span class="hljs-string">'s'</span>): <span class="hljs-number">5</span> (<span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'i'</span>): <span class="hljs-number">2</span> (<span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'t'</span>): <span class="hljs-number">7</span> (<span class="hljs-string">'t'</span>, <span class="hljs-string">'h'</span>): <span class="hljs-number">3</span></pre></div> <p>Now, finding the most frequent pair only takes a quick loop:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>best_pair = <span class="hljs-string">""</span> max_freq = <span class="hljs-literal">None</span> <span class="hljs-keyword">for</span> pair, freq <span class="hljs-keyword">in</span> pair_freqs.items(): <span class="hljs-keyword">if</span> max_freq <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">or</span> max_freq &lt; freq: best_pair = pair max_freq = freq <span class="hljs-built_in">print</span>(best_pair, max_freq)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'t'</span>) <span class="hljs-number">7</span></pre></div> <p>So the first merge to learn is <code>('Ġ', 't') -&gt; 'Ġt'</code>, and we add <code>'Ġt'</code> to the vocabulary:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>merges = {(<span class="hljs-string">"Ġ"</span>, <span class="hljs-string">"t"</span>): <span class="hljs-string">"Ġt"</span>} vocab.append(<span class="hljs-string">"Ġt"</span>)</pre></div> <p>To continue, we need to apply that merge in our <code>splits</code> dictionary. Let’s write another function for this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">merge_pair</span>(<span class="hljs-params">a, b, splits</span>): <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> word_freqs: split = splits[word] <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(split) == <span class="hljs-number">1</span>: <span class="hljs-keyword">continue</span> i = <span class="hljs-number">0</span> <span class="hljs-keyword">while</span> i &lt; <span class="hljs-built_in">len</span>(split) - <span class="hljs-number">1</span>: <span class="hljs-keyword">if</span> split[i] == a <span class="hljs-keyword">and</span> split[i + <span class="hljs-number">1</span>] == b: split = split[:i] + [a + b] + split[i + <span class="hljs-number">2</span> :] <span class="hljs-keyword">else</span>: i += <span class="hljs-number">1</span> splits[word] = split <span class="hljs-keyword">return</span> splits</pre></div> <p>And we can have a look at the result of the first merge:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>splits = merge_pair(<span class="hljs-string">"Ġ"</span>, <span class="hljs-string">"t"</span>, splits) <span class="hljs-built_in">print</span>(splits[<span class="hljs-string">"Ġtrained"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'Ġt'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'i'</span>, <span class="hljs-string">'n'</span>, <span class="hljs-string">'e'</span>, <span class="hljs-string">'d'</span>]</pre></div> <p>Now we have everything we need to loop until we have learned all the merges we want. Let’s aim for a vocab size of 50:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>vocab_size = <span class="hljs-number">50</span> <span class="hljs-keyword">while</span> <span class="hljs-built_in">len</span>(vocab) &lt; vocab_size: pair_freqs = compute_pair_freqs(splits) best_pair = <span class="hljs-string">""</span> max_freq = <span class="hljs-literal">None</span> <span class="hljs-keyword">for</span> pair, freq <span class="hljs-keyword">in</span> pair_freqs.items(): <span class="hljs-keyword">if</span> max_freq <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">or</span> max_freq &lt; freq: best_pair = pair max_freq = freq splits = merge_pair(*best_pair, splits) merges[best_pair] = best_pair[<span class="hljs-number">0</span>] + best_pair[<span class="hljs-number">1</span>] vocab.append(best_pair[<span class="hljs-number">0</span>] + best_pair[<span class="hljs-number">1</span>])</pre></div> <p>As a result, we’ve learned 19 merge rules (the initial vocabulary had a size of 31 — 30 characters in the alphabet, plus the special token):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(merges)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{(<span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'t'</span>): <span class="hljs-string">'Ġt'</span>, (<span class="hljs-string">'i'</span>, <span class="hljs-string">'s'</span>): <span class="hljs-string">'is'</span>, (<span class="hljs-string">'e'</span>, <span class="hljs-string">'r'</span>): <span class="hljs-string">'er'</span>, (<span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'a'</span>): <span class="hljs-string">'Ġa'</span>, (<span class="hljs-string">'Ġt'</span>, <span class="hljs-string">'o'</span>): <span class="hljs-string">'Ġto'</span>, (<span class="hljs-string">'e'</span>, <span class="hljs-string">'n'</span>): <span class="hljs-string">'en'</span>, (<span class="hljs-string">'T'</span>, <span class="hljs-string">'h'</span>): <span class="hljs-string">'Th'</span>, (<span class="hljs-string">'Th'</span>, <span class="hljs-string">'is'</span>): <span class="hljs-string">'This'</span>, (<span class="hljs-string">'o'</span>, <span class="hljs-string">'u'</span>): <span class="hljs-string">'ou'</span>, (<span class="hljs-string">'s'</span>, <span class="hljs-string">'e'</span>): <span class="hljs-string">'se'</span>, (<span class="hljs-string">'Ġto'</span>, <span class="hljs-string">'k'</span>): <span class="hljs-string">'Ġtok'</span>, (<span class="hljs-string">'Ġtok'</span>, <span class="hljs-string">'en'</span>): <span class="hljs-string">'Ġtoken'</span>, (<span class="hljs-string">'n'</span>, <span class="hljs-string">'d'</span>): <span class="hljs-string">'nd'</span>, (<span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'is'</span>): <span class="hljs-string">'Ġis'</span>, (<span class="hljs-string">'Ġt'</span>, <span class="hljs-string">'h'</span>): <span class="hljs-string">'Ġth'</span>, (<span class="hljs-string">'Ġth'</span>, <span class="hljs-string">'e'</span>): <span class="hljs-string">'Ġthe'</span>, (<span class="hljs-string">'i'</span>, <span class="hljs-string">'n'</span>): <span class="hljs-string">'in'</span>, (<span class="hljs-string">'Ġa'</span>, <span class="hljs-string">'b'</span>): <span class="hljs-string">'Ġab'</span>, (<span class="hljs-string">'Ġtoken'</span>, <span class="hljs-string">'i'</span>): <span class="hljs-string">'Ġtokeni'</span>}</pre></div> <p>And the vocabulary is composed of the special token, the initial alphabet, and all the results of the merges:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(vocab)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'&lt;|endoftext|&gt;'</span>, <span class="hljs-string">','</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'C'</span>, <span class="hljs-string">'F'</span>, <span class="hljs-string">'H'</span>, <span class="hljs-string">'T'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>, <span class="hljs-string">'d'</span>, <span class="hljs-string">'e'</span>, <span class="hljs-string">'f'</span>, <span class="hljs-string">'g'</span>, <span class="hljs-string">'h'</span>, <span class="hljs-string">'i'</span>, <span class="hljs-string">'k'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'m'</span>, <span class="hljs-string">'n'</span>, <span class="hljs-string">'o'</span>, <span class="hljs-string">'p'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'t'</span>, <span class="hljs-string">'u'</span>, <span class="hljs-string">'v'</span>, <span class="hljs-string">'w'</span>, <span class="hljs-string">'y'</span>, <span class="hljs-string">'z'</span>, <span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'Ġt'</span>, <span class="hljs-string">'is'</span>, <span class="hljs-string">'er'</span>, <span class="hljs-string">'Ġa'</span>, <span class="hljs-string">'Ġto'</span>, <span class="hljs-string">'en'</span>, <span class="hljs-string">'Th'</span>, <span class="hljs-string">'This'</span>, <span class="hljs-string">'ou'</span>, <span class="hljs-string">'se'</span>, <span class="hljs-string">'Ġtok'</span>, <span class="hljs-string">'Ġtoken'</span>, <span class="hljs-string">'nd'</span>, <span class="hljs-string">'Ġis'</span>, <span class="hljs-string">'Ġth'</span>, <span class="hljs-string">'Ġthe'</span>, <span class="hljs-string">'in'</span>, <span class="hljs-string">'Ġab'</span>, <span class="hljs-string">'Ġtokeni'</span>]</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 Using <code>train_new_from_iterator()</code> on the same corpus won’t result in the exact same vocabulary. This is because when there is a choice of the most frequent pair, we selected the first one encountered, while the 🤗 Tokenizers library selects the first one based on its inner IDs.</p></div> <p>To tokenize a new text, we pre-tokenize it, split it, then apply all the merge rules learned:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize</span>(<span class="hljs-params">text</span>): pre_tokenize_result = tokenizer._tokenizer.pre_tokenizer.pre_tokenize_str(text) pre_tokenized_text = [word <span class="hljs-keyword">for</span> word, offset <span class="hljs-keyword">in</span> pre_tokenize_result] splits = [[l <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> word] <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> pre_tokenized_text] <span class="hljs-keyword">for</span> pair, merge <span class="hljs-keyword">in</span> merges.items(): <span class="hljs-keyword">for</span> idx, split <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(splits): i = <span class="hljs-number">0</span> <span class="hljs-keyword">while</span> i &lt; <span class="hljs-built_in">len</span>(split) - <span class="hljs-number">1</span>: <span class="hljs-keyword">if</span> split[i] == pair[<span class="hljs-number">0</span>] <span class="hljs-keyword">and</span> split[i + <span class="hljs-number">1</span>] == pair[<span class="hljs-number">1</span>]: split = split[:i] + [merge] + split[i + <span class="hljs-number">2</span> :] <span class="hljs-keyword">else</span>: i += <span class="hljs-number">1</span> splits[idx] = split <span class="hljs-keyword">return</span> <span class="hljs-built_in">sum</span>(splits, [])</pre></div> <p>We can try this on any text composed of characters in the alphabet:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenize(<span class="hljs-string">"This is not a token."</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'This'</span>, <span class="hljs-string">'Ġis'</span>, <span class="hljs-string">'Ġ'</span>, <span class="hljs-string">'n'</span>, <span class="hljs-string">'o'</span>, <span class="hljs-string">'t'</span>, <span class="hljs-string">'Ġa'</span>, <span class="hljs-string">'Ġtoken'</span>, <span class="hljs-string">'.'</span>]</pre></div> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>⚠️ Our implementation will throw an error if there is an unknown character since we didn’t do anything to handle them. GPT-2 doesn’t actually have an unknown token (it’s impossible to get an unknown character when using byte-level BPE), but this could happen here because we did not include all the possible bytes in the initial vocabulary. This aspect of BPE is beyond the scope of this section, so we’ve left the details out.</p></div> <p>That’s it for the BPE algorithm! Next, we’ll have a look at WordPiece.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter6/4?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Normalization and pre-tokenization</a> <a href="/learn/nlp-course/chapter6/6?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">WordPiece tokenization<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;byte-pair-encoding-tokenization&quot;,&quot;url&quot;:&quot;#byte-pair-encoding-tokenization&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Training algorithm&quot;,&quot;id&quot;:&quot;training-algorithm&quot;,&quot;url&quot;:&quot;#training-algorithm&quot;},{&quot;title&quot;:&quot;Tokenization algorithm&quot;,&quot;id&quot;:&quot;tokenization-algorithm&quot;,&quot;url&quot;:&quot;#tokenization-algorithm&quot;},{&quot;title&quot;:&quot;Implementing BPE&quot;,&quot;id&quot;:&quot;implementing-bpe&quot;,&quot;url&quot;:&quot;#implementing-bpe&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#byte-pair-encoding-tokenization" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-byte-pair-encoding-tokenization"><wbr>Byte-<wbr>Pair <wbr>Encoding tokenization</a> <a href="#training-algorithm" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-algorithm"><wbr>Training algorithm</a> <a href="#tokenization-algorithm" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tokenization-algorithm"><wbr>Tokenization algorithm</a> <a href="#implementing-bpe" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-implementing-bpe"><wbr>Implementing BPE</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter6/5" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter6/5"); } </script> <iframe name="__privateStripeMetricsController8270" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter6%2F5%3Ffw%3Dpt&amp;title=Byte-Pair%20Encoding%20tokenization%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:23.465Z
WordPiece tokenization - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter6/6?fw=pt
## [](#wordpiece-tokenization)WordPiece tokenization [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-6-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section6.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section6.ipynb) WordPiece is the tokenization algorithm Google developed to pretrain BERT. It has since been reused in quite a few Transformer models based on BERT, such as DistilBERT, MobileBERT, Funnel Transformers, and MPNET. It’s very similar to BPE in terms of the training, but the actual tokenization is done differently. 💡 This section covers WordPiece in depth, going as far as showing a full implementation. You can skip to the end if you just want a general overview of the tokenization algorithm. ## [](#training-algorithm)Training algorithm ⚠️ Google never open-sourced its implementation of the training algorithm of WordPiece, so what follows is our best guess based on the published literature. It may not be 100% accurate. Like BPE, WordPiece starts from a small vocabulary including the special tokens used by the model and the initial alphabet. Since it identifies subwords by adding a prefix (like `##` for BERT), each word is initially split by adding that prefix to all the characters inside the word. So, for instance, `"word"` gets split like this: Thus, the initial alphabet contains all the characters present at the beginning of a word and the characters present inside a word preceded by the WordPiece prefix. Then, again like BPE, WordPiece learns merge rules. The main difference is the way the pair to be merged is selected. Instead of selecting the most frequent pair, WordPiece computes a score for each pair, using the following formula: score\=(freq\_of\_pair)/(freq\_of\_first\_element×freq\_of\_second\_element)\\mathrm{score} = (\\mathrm{freq\\\_of\\\_pair}) / (\\mathrm{freq\\\_of\\\_first\\\_element} \\times \\mathrm{freq\\\_of\\\_second\\\_element}) By dividing the frequency of the pair by the product of the frequencies of each of its parts, the algorithm prioritizes the merging of pairs where the individual parts are less frequent in the vocabulary. For instance, it won’t necessarily merge `("un", "##able")` even if that pair occurs very frequently in the vocabulary, because the two pairs `"un"` and `"##able"` will likely each appear in a lot of other words and have a high frequency. In contrast, a pair like `("hu", "##gging")` will probably be merged faster (assuming the word “hugging” appears often in the vocabulary) since `"hu"` and `"##gging"` are likely to be less frequent individually. Let’s look at the same vocabulary we used in the BPE training example: ``` ("hug", 10), ("pug", 5), ("pun", 12), ("bun", 4), ("hugs", 5)``` The splits here will be: ``` ("h" "##u" "##g", 10), ("p" "##u" "##g", 5), ("p" "##u" "##n", 12), ("b" "##u" "##n", 4), ("h" "##u" "##g" "##s", 5)``` so the initial vocabulary will be `["b", "h", "p", "##g", "##n", "##s", "##u"]` (if we forget about special tokens for now). The most frequent pair is `("##u", "##g")` (present 20 times), but the individual frequency of `"##u"` is very high, so its score is not the highest (it’s 1 / 36). All pairs with a `"##u"` actually have that same score (1 / 36), so the best score goes to the pair `("##g", "##s")` — the only one without a `"##u"` — at 1 / 20, and the first merge learned is `("##g", "##s") -> ("##gs")`. Note that when we merge, we remove the `##` between the two tokens, so we add `"##gs"` to the vocabulary and apply the merge in the words of the corpus: ``` Vocabulary: ["b", "h", "p", "##g", "##n", "##s", "##u", "##gs"] Corpus: ("h" "##u" "##g", 10), ("p" "##u" "##g", 5), ("p" "##u" "##n", 12), ("b" "##u" "##n", 4), ("h" "##u" "##gs", 5)``` At this point, `"##u"` is in all the possible pairs, so they all end up with the same score. Let’s say that in this case, the first pair is merged, so `("h", "##u") -> "hu"`. This takes us to: ``` Vocabulary: ["b", "h", "p", "##g", "##n", "##s", "##u", "##gs", "hu"] Corpus: ("hu" "##g", 10), ("p" "##u" "##g", 5), ("p" "##u" "##n", 12), ("b" "##u" "##n", 4), ("hu" "##gs", 5)``` Then the next best score is shared by `("hu", "##g")` and `("hu", "##gs")` (with 1/15, compared to 1/21 for all the other pairs), so the first pair with the biggest score is merged: ``` Vocabulary: ["b", "h", "p", "##g", "##n", "##s", "##u", "##gs", "hu", "hug"] Corpus: ("hug", 10), ("p" "##u" "##g", 5), ("p" "##u" "##n", 12), ("b" "##u" "##n", 4), ("hu" "##gs", 5)``` and we continue like this until we reach the desired vocabulary size. ✏️ **Now your turn!** What will the next merge rule be? ## [](#tokenization-algorithm)Tokenization algorithm Tokenization differs in WordPiece and BPE in that WordPiece only saves the final vocabulary, not the merge rules learned. Starting from the word to tokenize, WordPiece finds the longest subword that is in the vocabulary, then splits on it. For instance, if we use the vocabulary learned in the example above, for the word `"hugs"` the longest subword starting from the beginning that is inside the vocabulary is `"hug"`, so we split there and get `["hug", "##s"]`. We then continue with `"##s"`, which is in the vocabulary, so the tokenization of `"hugs"` is `["hug", "##s"]`. With BPE, we would have applied the merges learned in order and tokenized this as `["hu", "##gs"]`, so the encoding is different. As another example, let’s see how the word `"bugs"` would be tokenized. `"b"` is the longest subword starting at the beginning of the word that is in the vocabulary, so we split there and get `["b", "##ugs"]`. Then `"##u"` is the longest subword starting at the beginning of `"##ugs"` that is in the vocabulary, so we split there and get `["b", "##u, "##gs"]`. Finally, `"##gs"` is in the vocabulary, so this last list is the tokenization of `"bugs"`. When the tokenization gets to a stage where it’s not possible to find a subword in the vocabulary, the whole word is tokenized as unknown — so, for instance, `"mug"` would be tokenized as `["[UNK]"]`, as would `"bum"` (even if we can begin with `"b"` and `"##u"`, `"##m"` is not the vocabulary, and the resulting tokenization will just be `["[UNK]"]`, not `["b", "##u", "[UNK]"]`). This is another difference from BPE, which would only classify the individual characters not in the vocabulary as unknown. ✏️ **Now your turn!** How will the word `"pugs"` be tokenized? ## [](#implementing-wordpiece)Implementing WordPiece Now let’s take a look at an implementation of the WordPiece algorithm. Like with BPE, this is just pedagogical, and you won’t able to use this on a big corpus. We will use the same corpus as in the BPE example: ``` corpus = [ "This is the Hugging Face Course.", "This chapter is about tokenization.", "This section shows several tokenizer algorithms.", "Hopefully, you will be able to understand how they are trained and generate tokens.", ]``` First, we need to pre-tokenize the corpus into words. Since we are replicating a WordPiece tokenizer (like BERT), we will use the `bert-base-cased` tokenizer for the pre-tokenization: ``` from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")``` Then we compute the frequencies of each word in the corpus as we do the pre-tokenization: ``` from collections import defaultdict word_freqs = defaultdict(int) for text in corpus: words_with_offsets = tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(text) new_words = [word for word, offset in words_with_offsets] for word in new_words: word_freqs[word] += 1 word_freqs``` ``` defaultdict( int, {'This': 3, 'is': 2, 'the': 1, 'Hugging': 1, 'Face': 1, 'Course': 1, '.': 4, 'chapter': 1, 'about': 1, 'tokenization': 1, 'section': 1, 'shows': 1, 'several': 1, 'tokenizer': 1, 'algorithms': 1, 'Hopefully': 1, ',': 1, 'you': 1, 'will': 1, 'be': 1, 'able': 1, 'to': 1, 'understand': 1, 'how': 1, 'they': 1, 'are': 1, 'trained': 1, 'and': 1, 'generate': 1, 'tokens': 1})``` As we saw before, the alphabet is the unique set composed of all the first letters of words, and all the other letters that appear in words prefixed by `##`: ``` alphabet = [] for word in word_freqs.keys(): if word[0] not in alphabet: alphabet.append(word[0]) for letter in word[1:]: if f"##{letter}" not in alphabet: alphabet.append(f"##{letter}") alphabet.sort() alphabet print(alphabet)``` ``` ['##a', '##b', '##c', '##d', '##e', '##f', '##g', '##h', '##i', '##k', '##l', '##m', '##n', '##o', '##p', '##r', '##s', '##t', '##u', '##v', '##w', '##y', '##z', ',', '.', 'C', 'F', 'H', 'T', 'a', 'b', 'c', 'g', 'h', 'i', 's', 't', 'u', 'w', 'y']``` We also add the special tokens used by the model at the beginning of that vocabulary. In the case of BERT, it’s the list `["[PAD]", "[UNK]", "[CLS]", "[SEP]", "[MASK]"]`: ``` vocab = ["[PAD]", "[UNK]", "[CLS]", "[SEP]", "[MASK]"] + alphabet.copy()``` Next we need to split each word, with all the letters that are not the first prefixed by `##`: ``` splits = { word: [c if i == 0 else f"##{c}" for i, c in enumerate(word)] for word in word_freqs.keys() }``` Now that we are ready for training, let’s write a function that computes the score of each pair. We’ll need to use this at each step of the training: ``` def compute_pair_scores(splits): letter_freqs = defaultdict(int) pair_freqs = defaultdict(int) for word, freq in word_freqs.items(): split = splits[word] if len(split) == 1: letter_freqs[split[0]] += freq continue for i in range(len(split) - 1): pair = (split[i], split[i + 1]) letter_freqs[split[i]] += freq pair_freqs[pair] += freq letter_freqs[split[-1]] += freq scores = { pair: freq / (letter_freqs[pair[0]] * letter_freqs[pair[1]]) for pair, freq in pair_freqs.items() } return scores``` Let’s have a look at a part of this dictionary after the initial splits: ``` pair_scores = compute_pair_scores(splits) for i, key in enumerate(pair_scores.keys()): print(f"{key}: {pair_scores[key]}") if i >= 5: break``` ``` ('T', '##h'): 0.125 ('##h', '##i'): 0.03409090909090909 ('##i', '##s'): 0.02727272727272727 ('i', '##s'): 0.1 ('t', '##h'): 0.03571428571428571 ('##h', '##e'): 0.011904761904761904``` Now, finding the pair with the best score only takes a quick loop: ``` best_pair = "" max_score = None for pair, score in pair_scores.items(): if max_score is None or max_score < score: best_pair = pair max_score = score print(best_pair, max_score)``` So the first merge to learn is `('a', '##b') -> 'ab'`, and we add `'ab'` to the vocabulary: To continue, we need to apply that merge in our `splits` dictionary. Let’s write another function for this: ``` def merge_pair(a, b, splits): for word in word_freqs: split = splits[word] if len(split) == 1: continue i = 0 while i < len(split) - 1: if split[i] == a and split[i + 1] == b: merge = a + b[2:] if b.startswith("##") else a + b split = split[:i] + [merge] + split[i + 2 :] else: i += 1 splits[word] = split return splits``` And we can have a look at the result of the first merge: ``` splits = merge_pair("a", "##b", splits) splits["about"]``` ``` ['ab', '##o', '##u', '##t']``` Now we have everything we need to loop until we have learned all the merges we want. Let’s aim for a vocab size of 70: ``` vocab_size = 70 while len(vocab) < vocab_size: scores = compute_pair_scores(splits) best_pair, max_score = "", None for pair, score in scores.items(): if max_score is None or max_score < score: best_pair = pair max_score = score splits = merge_pair(*best_pair, splits) new_token = ( best_pair[0] + best_pair[1][2:] if best_pair[1].startswith("##") else best_pair[0] + best_pair[1] ) vocab.append(new_token)``` We can then look at the generated vocabulary: ``` ['[PAD]', '[UNK]', '[CLS]', '[SEP]', '[MASK]', '##a', '##b', '##c', '##d', '##e', '##f', '##g', '##h', '##i', '##k', '##l', '##m', '##n', '##o', '##p', '##r', '##s', '##t', '##u', '##v', '##w', '##y', '##z', ',', '.', 'C', 'F', 'H', 'T', 'a', 'b', 'c', 'g', 'h', 'i', 's', 't', 'u', 'w', 'y', 'ab', '##fu', 'Fa', 'Fac', '##ct', '##ful', '##full', '##fully', 'Th', 'ch', '##hm', 'cha', 'chap', 'chapt', '##thm', 'Hu', 'Hug', 'Hugg', 'sh', 'th', 'is', '##thms', '##za', '##zat', '##ut']``` As we can see, compared to BPE, this tokenizer learns parts of words as tokens a bit faster. 💡 Using `train_new_from_iterator()` on the same corpus won’t result in the exact same vocabulary. This is because the 🤗 Tokenizers library does not implement WordPiece for the training (since we are not completely sure of its internals), but uses BPE instead. To tokenize a new text, we pre-tokenize it, split it, then apply the tokenization algorithm on each word. That is, we look for the biggest subword starting at the beginning of the first word and split it, then we repeat the process on the second part, and so on for the rest of that word and the following words in the text: ``` def encode_word(word): tokens = [] while len(word) > 0: i = len(word) while i > 0 and word[:i] not in vocab: i -= 1 if i == 0: return ["[UNK]"] tokens.append(word[:i]) word = word[i:] if len(word) > 0: word = f"##{word}" return tokens``` Let’s test it on one word that’s in the vocabulary, and another that isn’t: ``` print(encode_word("Hugging")) print(encode_word("HOgging"))``` ``` ['Hugg', '##i', '##n', '##g'] ['[UNK]']``` Now, let’s write a function that tokenizes a text: ``` def tokenize(text): pre_tokenize_result = tokenizer._tokenizer.pre_tokenizer.pre_tokenize_str(text) pre_tokenized_text = [word for word, offset in pre_tokenize_result] encoded_words = [encode_word(word) for word in pre_tokenized_text] return sum(encoded_words, [])``` We can try it on any text: ``` tokenize("This is the Hugging Face course!")``` ``` ['Th', '##i', '##s', 'is', 'th', '##e', 'Hugg', '##i', '##n', '##g', 'Fac', '##e', 'c', '##o', '##u', '##r', '##s', '##e', '[UNK]']``` That’s it for the WordPiece algorithm! Now let’s take a look at Unigram.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="WordPiece tokenization - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter6/6"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>WordPiece tokenization - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;wordpiece-tokenization&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;training-algorithm&quot;,&quot;title&quot;:&quot;Training algorithm&quot;},{&quot;local&quot;:&quot;tokenization-algorithm&quot;,&quot;title&quot;:&quot;Tokenization algorithm&quot;},{&quot;local&quot;:&quot;implementing-wordpiece&quot;,&quot;title&quot;:&quot;Implementing WordPiece&quot;}],&quot;title&quot;:&quot;WordPiece tokenization&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter6/6&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;WordPiece tokenization&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">WordPiece tokenization</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/2?fw=pt">Training a new tokenizer from an old one </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3?fw=pt">Fast tokenizers' special powers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3b?fw=pt">Fast tokenizers in the QA pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/4?fw=pt">Normalization and pre-tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/5?fw=pt">Byte-Pair Encoding tokenization </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter6/6?fw=pt">WordPiece tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/7?fw=pt">Unigram tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/8?fw=pt">Building a tokenizer, block by block </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/9?fw=pt">Tokenizers, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="wordpiece-tokenization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#wordpiece-tokenization"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>WordPiece tokenization</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-6-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section6.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section6.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>WordPiece is the tokenization algorithm Google developed to pretrain BERT. It has since been reused in quite a few Transformer models based on BERT, such as DistilBERT, MobileBERT, Funnel Transformers, and MPNET. It’s very similar to BPE in terms of the training, but the actual tokenization is done differently.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/qpv6ms_t_1A" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 This section covers WordPiece in depth, going as far as showing a full implementation. You can skip to the end if you just want a general overview of the tokenization algorithm.</p></div> <h2 class="relative group"><a id="training-algorithm" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-algorithm"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training algorithm</span></h2> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>⚠️ Google never open-sourced its implementation of the training algorithm of WordPiece, so what follows is our best guess based on the published literature. It may not be 100% accurate.</p></div> <p>Like BPE, WordPiece starts from a small vocabulary including the special tokens used by the model and the initial alphabet. Since it identifies subwords by adding a prefix (like <code>##</code> for BERT), each word is initially split by adding that prefix to all the characters inside the word. So, for instance, <code>"word"</code> gets split like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>w ##o ##r ##d</pre></div> <p>Thus, the initial alphabet contains all the characters present at the beginning of a word and the characters present inside a word preceded by the WordPiece prefix.</p> <p>Then, again like BPE, WordPiece learns merge rules. The main difference is the way the pair to be merged is selected. Instead of selecting the most frequent pair, WordPiece computes a score for each pair, using the following formula: <span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mrow><mi mathvariant="normal">s</mi><mi mathvariant="normal">c</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">e</mi></mrow><mo>=</mo><mo stretchy="false">(</mo><mrow><mi mathvariant="normal">f</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">q</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">f</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">p</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">r</mi></mrow><mo stretchy="false">)</mo><mi mathvariant="normal">/</mi><mo stretchy="false">(</mo><mrow><mi mathvariant="normal">f</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">q</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">f</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">f</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">s</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">l</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">m</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">t</mi></mrow><mo>×</mo><mrow><mi mathvariant="normal">f</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">q</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">f</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">s</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">c</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">_</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">l</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">m</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">t</mi></mrow><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\mathrm{score} = (\mathrm{freq\_of\_pair}) / (\mathrm{freq\_of\_first\_element} \times \mathrm{freq\_of\_second\_element})</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord"><span class="mord mathrm">score</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.06em;vertical-align:-0.31em;"></span><span class="mopen">(</span><span class="mord"><span class="mord mathrm">freq_of_pair</span></span><span class="mclose">)</span><span class="mord">/</span><span class="mopen">(</span><span class="mord"><span class="mord mathrm">freq_of_first_element</span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1.06em;vertical-align:-0.31em;"></span><span class="mord"><span class="mord mathrm">freq_of_second_element</span></span><span class="mclose">)</span></span></span></span></span></p> <p>By dividing the frequency of the pair by the product of the frequencies of each of its parts, the algorithm prioritizes the merging of pairs where the individual parts are less frequent in the vocabulary. For instance, it won’t necessarily merge <code>("un", "##able")</code> even if that pair occurs very frequently in the vocabulary, because the two pairs <code>"un"</code> and <code>"##able"</code> will likely each appear in a lot of other words and have a high frequency. In contrast, a pair like <code>("hu", "##gging")</code> will probably be merged faster (assuming the word “hugging” appears often in the vocabulary) since <code>"hu"</code> and <code>"##gging"</code> are likely to be less frequent individually.</p> <p>Let’s look at the same vocabulary we used in the BPE training example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-string">"hug"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">10</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"pug"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"pun"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">12</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"bun"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"hugs"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>)</pre></div> <p>The splits here will be:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-string">"h"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#g</span>"</span>, <span class="hljs-number">10</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#g</span>"</span>, <span class="hljs-number">5</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#n</span>"</span>, <span class="hljs-number">12</span>), (<span class="hljs-string">"b"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#n</span>"</span>, <span class="hljs-number">4</span>), (<span class="hljs-string">"h"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#g</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#s</span>"</span>, <span class="hljs-number">5</span>)</pre></div> <p>so the initial vocabulary will be <code>["b", "h", "p", "##g", "##n", "##s", "##u"]</code> (if we forget about special tokens for now). The most frequent pair is <code>("##u", "##g")</code> (present 20 times), but the individual frequency of <code>"##u"</code> is very high, so its score is not the highest (it’s 1 / 36). All pairs with a <code>"##u"</code> actually have that same score (1 / 36), so the best score goes to the pair <code>("##g", "##s")</code> — the only one without a <code>"##u"</code> — at 1 / 20, and the first merge learned is <code>("##g", "##s") -&gt; ("##gs")</code>.</p> <p>Note that when we merge, we remove the <code>##</code> between the two tokens, so we add <code>"##gs"</code> to the vocabulary and apply the merge in the words of the corpus:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Vocabulary: [<span class="hljs-string">"b"</span>, <span class="hljs-string">"h"</span>, <span class="hljs-string">"p"</span>, <span class="hljs-string">"#<span class="hljs-subst">#g</span>"</span>, <span class="hljs-string">"#<span class="hljs-subst">#n</span>"</span>, <span class="hljs-string">"#<span class="hljs-subst">#s</span>"</span>, <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span>, <span class="hljs-string">"#<span class="hljs-subst">#gs</span>"</span>] Corpus: (<span class="hljs-string">"h"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#g</span>"</span>, <span class="hljs-number">10</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#g</span>"</span>, <span class="hljs-number">5</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#n</span>"</span>, <span class="hljs-number">12</span>), (<span class="hljs-string">"b"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#n</span>"</span>, <span class="hljs-number">4</span>), (<span class="hljs-string">"h"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#gs</span>"</span>, <span class="hljs-number">5</span>)</pre></div> <p>At this point, <code>"##u"</code> is in all the possible pairs, so they all end up with the same score. Let’s say that in this case, the first pair is merged, so <code>("h", "##u") -&gt; "hu"</code>. This takes us to:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Vocabulary: [<span class="hljs-string">"b"</span>, <span class="hljs-string">"h"</span>, <span class="hljs-string">"p"</span>, <span class="hljs-string">"#<span class="hljs-subst">#g</span>"</span>, <span class="hljs-string">"#<span class="hljs-subst">#n</span>"</span>, <span class="hljs-string">"#<span class="hljs-subst">#s</span>"</span>, <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span>, <span class="hljs-string">"#<span class="hljs-subst">#gs</span>"</span>, <span class="hljs-string">"hu"</span>] Corpus: (<span class="hljs-string">"hu"</span> <span class="hljs-string">"#<span class="hljs-subst">#g</span>"</span>, <span class="hljs-number">10</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#g</span>"</span>, <span class="hljs-number">5</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#n</span>"</span>, <span class="hljs-number">12</span>), (<span class="hljs-string">"b"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#n</span>"</span>, <span class="hljs-number">4</span>), (<span class="hljs-string">"hu"</span> <span class="hljs-string">"#<span class="hljs-subst">#gs</span>"</span>, <span class="hljs-number">5</span>)</pre></div> <p>Then the next best score is shared by <code>("hu", "##g")</code> and <code>("hu", "##gs")</code> (with 1/15, compared to 1/21 for all the other pairs), so the first pair with the biggest score is merged:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Vocabulary: [<span class="hljs-string">"b"</span>, <span class="hljs-string">"h"</span>, <span class="hljs-string">"p"</span>, <span class="hljs-string">"#<span class="hljs-subst">#g</span>"</span>, <span class="hljs-string">"#<span class="hljs-subst">#n</span>"</span>, <span class="hljs-string">"#<span class="hljs-subst">#s</span>"</span>, <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span>, <span class="hljs-string">"#<span class="hljs-subst">#gs</span>"</span>, <span class="hljs-string">"hu"</span>, <span class="hljs-string">"hug"</span>] Corpus: (<span class="hljs-string">"hug"</span>, <span class="hljs-number">10</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#g</span>"</span>, <span class="hljs-number">5</span>), (<span class="hljs-string">"p"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#n</span>"</span>, <span class="hljs-number">12</span>), (<span class="hljs-string">"b"</span> <span class="hljs-string">"#<span class="hljs-subst">#u</span>"</span> <span class="hljs-string">"#<span class="hljs-subst">#n</span>"</span>, <span class="hljs-number">4</span>), (<span class="hljs-string">"hu"</span> <span class="hljs-string">"#<span class="hljs-subst">#gs</span>"</span>, <span class="hljs-number">5</span>)</pre></div> <p>and we continue like this until we reach the desired vocabulary size.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Now your turn!</strong> What will the next merge rule be?</p></div> <h2 class="relative group"><a id="tokenization-algorithm" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tokenization-algorithm"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Tokenization algorithm</span></h2> <p>Tokenization differs in WordPiece and BPE in that WordPiece only saves the final vocabulary, not the merge rules learned. Starting from the word to tokenize, WordPiece finds the longest subword that is in the vocabulary, then splits on it. For instance, if we use the vocabulary learned in the example above, for the word <code>"hugs"</code> the longest subword starting from the beginning that is inside the vocabulary is <code>"hug"</code>, so we split there and get <code>["hug", "##s"]</code>. We then continue with <code>"##s"</code>, which is in the vocabulary, so the tokenization of <code>"hugs"</code> is <code>["hug", "##s"]</code>.</p> <p>With BPE, we would have applied the merges learned in order and tokenized this as <code>["hu", "##gs"]</code>, so the encoding is different.</p> <p>As another example, let’s see how the word <code>"bugs"</code> would be tokenized. <code>"b"</code> is the longest subword starting at the beginning of the word that is in the vocabulary, so we split there and get <code>["b", "##ugs"]</code>. Then <code>"##u"</code> is the longest subword starting at the beginning of <code>"##ugs"</code> that is in the vocabulary, so we split there and get <code>["b", "##u, "##gs"]</code>. Finally, <code>"##gs"</code> is in the vocabulary, so this last list is the tokenization of <code>"bugs"</code>.</p> <p>When the tokenization gets to a stage where it’s not possible to find a subword in the vocabulary, the whole word is tokenized as unknown — so, for instance, <code>"mug"</code> would be tokenized as <code>["[UNK]"]</code>, as would <code>"bum"</code> (even if we can begin with <code>"b"</code> and <code>"##u"</code>, <code>"##m"</code> is not the vocabulary, and the resulting tokenization will just be <code>["[UNK]"]</code>, not <code>["b", "##u", "[UNK]"]</code>). This is another difference from BPE, which would only classify the individual characters not in the vocabulary as unknown.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Now your turn!</strong> How will the word <code>"pugs"</code> be tokenized?</p></div> <h2 class="relative group"><a id="implementing-wordpiece" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#implementing-wordpiece"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Implementing WordPiece</span></h2> <p>Now let’s take a look at an implementation of the WordPiece algorithm. Like with BPE, this is just pedagogical, and you won’t able to use this on a big corpus.</p> <p>We will use the same corpus as in the BPE example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>corpus = [ <span class="hljs-string">"This is the Hugging Face Course."</span>, <span class="hljs-string">"This chapter is about tokenization."</span>, <span class="hljs-string">"This section shows several tokenizer algorithms."</span>, <span class="hljs-string">"Hopefully, you will be able to understand how they are trained and generate tokens."</span>, ]</pre></div> <p>First, we need to pre-tokenize the corpus into words. Since we are replicating a WordPiece tokenizer (like BERT), we will use the <code>bert-base-cased</code> tokenizer for the pre-tokenization:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>)</pre></div> <p>Then we compute the frequencies of each word in the corpus as we do the pre-tokenization:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> defaultdict word_freqs = defaultdict(<span class="hljs-built_in">int</span>) <span class="hljs-keyword">for</span> text <span class="hljs-keyword">in</span> corpus: words_with_offsets = tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(text) new_words = [word <span class="hljs-keyword">for</span> word, offset <span class="hljs-keyword">in</span> words_with_offsets] <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> new_words: word_freqs[word] += <span class="hljs-number">1</span> word_freqs</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>defaultdict( <span class="hljs-built_in">int</span>, {<span class="hljs-string">'This'</span>: <span class="hljs-number">3</span>, <span class="hljs-string">'is'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'the'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Hugging'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Face'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Course'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'.'</span>: <span class="hljs-number">4</span>, <span class="hljs-string">'chapter'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'about'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'tokenization'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'section'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'shows'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'several'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'tokenizer'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'algorithms'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'Hopefully'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">','</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'you'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'will'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'be'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'able'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'to'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'understand'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'how'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'they'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'are'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'trained'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'and'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'generate'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'tokens'</span>: <span class="hljs-number">1</span>})</pre></div> <p>As we saw before, the alphabet is the unique set composed of all the first letters of words, and all the other letters that appear in words prefixed by <code>##</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>alphabet = [] <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> word_freqs.keys(): <span class="hljs-keyword">if</span> word[<span class="hljs-number">0</span>] <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> alphabet: alphabet.append(word[<span class="hljs-number">0</span>]) <span class="hljs-keyword">for</span> letter <span class="hljs-keyword">in</span> word[<span class="hljs-number">1</span>:]: <span class="hljs-keyword">if</span> <span class="hljs-string">f"##<span class="hljs-subst">{letter}</span>"</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> alphabet: alphabet.append(<span class="hljs-string">f"##<span class="hljs-subst">{letter}</span>"</span>) alphabet.sort() alphabet <span class="hljs-built_in">print</span>(alphabet)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'##a'</span>, <span class="hljs-string">'##b'</span>, <span class="hljs-string">'##c'</span>, <span class="hljs-string">'##d'</span>, <span class="hljs-string">'##e'</span>, <span class="hljs-string">'##f'</span>, <span class="hljs-string">'##g'</span>, <span class="hljs-string">'##h'</span>, <span class="hljs-string">'##i'</span>, <span class="hljs-string">'##k'</span>, <span class="hljs-string">'##l'</span>, <span class="hljs-string">'##m'</span>, <span class="hljs-string">'##n'</span>, <span class="hljs-string">'##o'</span>, <span class="hljs-string">'##p'</span>, <span class="hljs-string">'##r'</span>, <span class="hljs-string">'##s'</span>, <span class="hljs-string">'##t'</span>, <span class="hljs-string">'##u'</span>, <span class="hljs-string">'##v'</span>, <span class="hljs-string">'##w'</span>, <span class="hljs-string">'##y'</span>, <span class="hljs-string">'##z'</span>, <span class="hljs-string">','</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'C'</span>, <span class="hljs-string">'F'</span>, <span class="hljs-string">'H'</span>, <span class="hljs-string">'T'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>, <span class="hljs-string">'g'</span>, <span class="hljs-string">'h'</span>, <span class="hljs-string">'i'</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'t'</span>, <span class="hljs-string">'u'</span>, <span class="hljs-string">'w'</span>, <span class="hljs-string">'y'</span>]</pre></div> <p>We also add the special tokens used by the model at the beginning of that vocabulary. In the case of BERT, it’s the list <code>["[PAD]", "[UNK]", "[CLS]", "[SEP]", "[MASK]"]</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>vocab = [<span class="hljs-string">"[PAD]"</span>, <span class="hljs-string">"[UNK]"</span>, <span class="hljs-string">"[CLS]"</span>, <span class="hljs-string">"[SEP]"</span>, <span class="hljs-string">"[MASK]"</span>] + alphabet.copy()</pre></div> <p>Next we need to split each word, with all the letters that are not the first prefixed by <code>##</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>splits = { word: [c <span class="hljs-keyword">if</span> i == <span class="hljs-number">0</span> <span class="hljs-keyword">else</span> <span class="hljs-string">f"##<span class="hljs-subst">{c}</span>"</span> <span class="hljs-keyword">for</span> i, c <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(word)] <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> word_freqs.keys() }</pre></div> <p>Now that we are ready for training, let’s write a function that computes the score of each pair. We’ll need to use this at each step of the training:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_pair_scores</span>(<span class="hljs-params">splits</span>): letter_freqs = defaultdict(<span class="hljs-built_in">int</span>) pair_freqs = defaultdict(<span class="hljs-built_in">int</span>) <span class="hljs-keyword">for</span> word, freq <span class="hljs-keyword">in</span> word_freqs.items(): split = splits[word] <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(split) == <span class="hljs-number">1</span>: letter_freqs[split[<span class="hljs-number">0</span>]] += freq <span class="hljs-keyword">continue</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(split) - <span class="hljs-number">1</span>): pair = (split[i], split[i + <span class="hljs-number">1</span>]) letter_freqs[split[i]] += freq pair_freqs[pair] += freq letter_freqs[split[-<span class="hljs-number">1</span>]] += freq scores = { pair: freq / (letter_freqs[pair[<span class="hljs-number">0</span>]] * letter_freqs[pair[<span class="hljs-number">1</span>]]) <span class="hljs-keyword">for</span> pair, freq <span class="hljs-keyword">in</span> pair_freqs.items() } <span class="hljs-keyword">return</span> scores</pre></div> <p>Let’s have a look at a part of this dictionary after the initial splits:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pair_scores = compute_pair_scores(splits) <span class="hljs-keyword">for</span> i, key <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(pair_scores.keys()): <span class="hljs-built_in">print</span>(<span class="hljs-string">f"<span class="hljs-subst">{key}</span>: <span class="hljs-subst">{pair_scores[key]}</span>"</span>) <span class="hljs-keyword">if</span> i &gt;= <span class="hljs-number">5</span>: <span class="hljs-keyword">break</span></pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-string">'T'</span>, <span class="hljs-string">'##h'</span>): <span class="hljs-number">0.125</span> (<span class="hljs-string">'##h'</span>, <span class="hljs-string">'##i'</span>): <span class="hljs-number">0.03409090909090909</span> (<span class="hljs-string">'##i'</span>, <span class="hljs-string">'##s'</span>): <span class="hljs-number">0.02727272727272727</span> (<span class="hljs-string">'i'</span>, <span class="hljs-string">'##s'</span>): <span class="hljs-number">0.1</span> (<span class="hljs-string">'t'</span>, <span class="hljs-string">'##h'</span>): <span class="hljs-number">0.03571428571428571</span> (<span class="hljs-string">'##h'</span>, <span class="hljs-string">'##e'</span>): <span class="hljs-number">0.011904761904761904</span></pre></div> <p>Now, finding the pair with the best score only takes a quick loop:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>best_pair = <span class="hljs-string">""</span> max_score = <span class="hljs-literal">None</span> <span class="hljs-keyword">for</span> pair, score <span class="hljs-keyword">in</span> pair_scores.items(): <span class="hljs-keyword">if</span> max_score <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">or</span> max_score &lt; score: best_pair = pair max_score = score <span class="hljs-built_in">print</span>(best_pair, max_score)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-string">'a'</span>, <span class="hljs-string">'##b'</span>) <span class="hljs-number">0.2</span></pre></div> <p>So the first merge to learn is <code>('a', '##b') -&gt; 'ab'</code>, and we add <code>'ab'</code> to the vocabulary:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>vocab.append(<span class="hljs-string">"ab"</span>)</pre></div> <p>To continue, we need to apply that merge in our <code>splits</code> dictionary. Let’s write another function for this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">merge_pair</span>(<span class="hljs-params">a, b, splits</span>): <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> word_freqs: split = splits[word] <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(split) == <span class="hljs-number">1</span>: <span class="hljs-keyword">continue</span> i = <span class="hljs-number">0</span> <span class="hljs-keyword">while</span> i &lt; <span class="hljs-built_in">len</span>(split) - <span class="hljs-number">1</span>: <span class="hljs-keyword">if</span> split[i] == a <span class="hljs-keyword">and</span> split[i + <span class="hljs-number">1</span>] == b: merge = a + b[<span class="hljs-number">2</span>:] <span class="hljs-keyword">if</span> b.startswith(<span class="hljs-string">"##"</span>) <span class="hljs-keyword">else</span> a + b split = split[:i] + [merge] + split[i + <span class="hljs-number">2</span> :] <span class="hljs-keyword">else</span>: i += <span class="hljs-number">1</span> splits[word] = split <span class="hljs-keyword">return</span> splits</pre></div> <p>And we can have a look at the result of the first merge:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>splits = merge_pair(<span class="hljs-string">"a"</span>, <span class="hljs-string">"##b"</span>, splits) splits[<span class="hljs-string">"about"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'ab'</span>, <span class="hljs-string">'##o'</span>, <span class="hljs-string">'##u'</span>, <span class="hljs-string">'##t'</span>]</pre></div> <p>Now we have everything we need to loop until we have learned all the merges we want. Let’s aim for a vocab size of 70:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>vocab_size = <span class="hljs-number">70</span> <span class="hljs-keyword">while</span> <span class="hljs-built_in">len</span>(vocab) &lt; vocab_size: scores = compute_pair_scores(splits) best_pair, max_score = <span class="hljs-string">""</span>, <span class="hljs-literal">None</span> <span class="hljs-keyword">for</span> pair, score <span class="hljs-keyword">in</span> scores.items(): <span class="hljs-keyword">if</span> max_score <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">or</span> max_score &lt; score: best_pair = pair max_score = score splits = merge_pair(*best_pair, splits) new_token = ( best_pair[<span class="hljs-number">0</span>] + best_pair[<span class="hljs-number">1</span>][<span class="hljs-number">2</span>:] <span class="hljs-keyword">if</span> best_pair[<span class="hljs-number">1</span>].startswith(<span class="hljs-string">"##"</span>) <span class="hljs-keyword">else</span> best_pair[<span class="hljs-number">0</span>] + best_pair[<span class="hljs-number">1</span>] ) vocab.append(new_token)</pre></div> <p>We can then look at the generated vocabulary:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(vocab)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'[PAD]'</span>, <span class="hljs-string">'[UNK]'</span>, <span class="hljs-string">'[CLS]'</span>, <span class="hljs-string">'[SEP]'</span>, <span class="hljs-string">'[MASK]'</span>, <span class="hljs-string">'##a'</span>, <span class="hljs-string">'##b'</span>, <span class="hljs-string">'##c'</span>, <span class="hljs-string">'##d'</span>, <span class="hljs-string">'##e'</span>, <span class="hljs-string">'##f'</span>, <span class="hljs-string">'##g'</span>, <span class="hljs-string">'##h'</span>, <span class="hljs-string">'##i'</span>, <span class="hljs-string">'##k'</span>, <span class="hljs-string">'##l'</span>, <span class="hljs-string">'##m'</span>, <span class="hljs-string">'##n'</span>, <span class="hljs-string">'##o'</span>, <span class="hljs-string">'##p'</span>, <span class="hljs-string">'##r'</span>, <span class="hljs-string">'##s'</span>, <span class="hljs-string">'##t'</span>, <span class="hljs-string">'##u'</span>, <span class="hljs-string">'##v'</span>, <span class="hljs-string">'##w'</span>, <span class="hljs-string">'##y'</span>, <span class="hljs-string">'##z'</span>, <span class="hljs-string">','</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'C'</span>, <span class="hljs-string">'F'</span>, <span class="hljs-string">'H'</span>, <span class="hljs-string">'T'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span>, <span class="hljs-string">'g'</span>, <span class="hljs-string">'h'</span>, <span class="hljs-string">'i'</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'t'</span>, <span class="hljs-string">'u'</span>, <span class="hljs-string">'w'</span>, <span class="hljs-string">'y'</span>, <span class="hljs-string">'ab'</span>, <span class="hljs-string">'##fu'</span>, <span class="hljs-string">'Fa'</span>, <span class="hljs-string">'Fac'</span>, <span class="hljs-string">'##ct'</span>, <span class="hljs-string">'##ful'</span>, <span class="hljs-string">'##full'</span>, <span class="hljs-string">'##fully'</span>, <span class="hljs-string">'Th'</span>, <span class="hljs-string">'ch'</span>, <span class="hljs-string">'##hm'</span>, <span class="hljs-string">'cha'</span>, <span class="hljs-string">'chap'</span>, <span class="hljs-string">'chapt'</span>, <span class="hljs-string">'##thm'</span>, <span class="hljs-string">'Hu'</span>, <span class="hljs-string">'Hug'</span>, <span class="hljs-string">'Hugg'</span>, <span class="hljs-string">'sh'</span>, <span class="hljs-string">'th'</span>, <span class="hljs-string">'is'</span>, <span class="hljs-string">'##thms'</span>, <span class="hljs-string">'##za'</span>, <span class="hljs-string">'##zat'</span>, <span class="hljs-string">'##ut'</span>]</pre></div> <p>As we can see, compared to BPE, this tokenizer learns parts of words as tokens a bit faster.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 Using <code>train_new_from_iterator()</code> on the same corpus won’t result in the exact same vocabulary. This is because the 🤗 Tokenizers library does not implement WordPiece for the training (since we are not completely sure of its internals), but uses BPE instead.</p></div> <p>To tokenize a new text, we pre-tokenize it, split it, then apply the tokenization algorithm on each word. That is, we look for the biggest subword starting at the beginning of the first word and split it, then we repeat the process on the second part, and so on for the rest of that word and the following words in the text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">encode_word</span>(<span class="hljs-params">word</span>): tokens = [] <span class="hljs-keyword">while</span> <span class="hljs-built_in">len</span>(word) &gt; <span class="hljs-number">0</span>: i = <span class="hljs-built_in">len</span>(word) <span class="hljs-keyword">while</span> i &gt; <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> word[:i] <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> vocab: i -= <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> i == <span class="hljs-number">0</span>: <span class="hljs-keyword">return</span> [<span class="hljs-string">"[UNK]"</span>] tokens.append(word[:i]) word = word[i:] <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(word) &gt; <span class="hljs-number">0</span>: word = <span class="hljs-string">f"##<span class="hljs-subst">{word}</span>"</span> <span class="hljs-keyword">return</span> tokens</pre></div> <p>Let’s test it on one word that’s in the vocabulary, and another that isn’t:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(encode_word(<span class="hljs-string">"Hugging"</span>)) <span class="hljs-built_in">print</span>(encode_word(<span class="hljs-string">"HOgging"</span>))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'Hugg'</span>, <span class="hljs-string">'##i'</span>, <span class="hljs-string">'##n'</span>, <span class="hljs-string">'##g'</span>] [<span class="hljs-string">'[UNK]'</span>]</pre></div> <p>Now, let’s write a function that tokenizes a text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize</span>(<span class="hljs-params">text</span>): pre_tokenize_result = tokenizer._tokenizer.pre_tokenizer.pre_tokenize_str(text) pre_tokenized_text = [word <span class="hljs-keyword">for</span> word, offset <span class="hljs-keyword">in</span> pre_tokenize_result] encoded_words = [encode_word(word) <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> pre_tokenized_text] <span class="hljs-keyword">return</span> <span class="hljs-built_in">sum</span>(encoded_words, [])</pre></div> <p>We can try it on any text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenize(<span class="hljs-string">"This is the Hugging Face course!"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'Th'</span>, <span class="hljs-string">'##i'</span>, <span class="hljs-string">'##s'</span>, <span class="hljs-string">'is'</span>, <span class="hljs-string">'th'</span>, <span class="hljs-string">'##e'</span>, <span class="hljs-string">'Hugg'</span>, <span class="hljs-string">'##i'</span>, <span class="hljs-string">'##n'</span>, <span class="hljs-string">'##g'</span>, <span class="hljs-string">'Fac'</span>, <span class="hljs-string">'##e'</span>, <span class="hljs-string">'c'</span>, <span class="hljs-string">'##o'</span>, <span class="hljs-string">'##u'</span>, <span class="hljs-string">'##r'</span>, <span class="hljs-string">'##s'</span>, <span class="hljs-string">'##e'</span>, <span class="hljs-string">'[UNK]'</span>]</pre></div> <p>That’s it for the WordPiece algorithm! Now let’s take a look at Unigram.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter6/5?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Byte-Pair Encoding tokenization</a> <a href="/learn/nlp-course/chapter6/7?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Unigram tokenization<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;wordpiece-tokenization&quot;,&quot;url&quot;:&quot;#wordpiece-tokenization&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Training algorithm&quot;,&quot;id&quot;:&quot;training-algorithm&quot;,&quot;url&quot;:&quot;#training-algorithm&quot;},{&quot;title&quot;:&quot;Tokenization algorithm&quot;,&quot;id&quot;:&quot;tokenization-algorithm&quot;,&quot;url&quot;:&quot;#tokenization-algorithm&quot;},{&quot;title&quot;:&quot;Implementing WordPiece&quot;,&quot;id&quot;:&quot;implementing-wordpiece&quot;,&quot;url&quot;:&quot;#implementing-wordpiece&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#wordpiece-tokenization" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-wordpiece-tokenization"><wbr>Word<wbr>Piece tokenization</a> <a href="#training-algorithm" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-algorithm"><wbr>Training algorithm</a> <a href="#tokenization-algorithm" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tokenization-algorithm"><wbr>Tokenization algorithm</a> <a href="#implementing-wordpiece" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-implementing-wordpiece"><wbr>Implementing <wbr>Word<wbr>Piece</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter6/6" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter6/6"); } </script> <iframe name="__privateStripeMetricsController9010" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter6%2F6%3Ffw%3Dpt&amp;title=WordPiece%20tokenization%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:23.644Z
Unigram tokenization - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter6/7?fw=pt
## [](#unigram-tokenization)Unigram tokenization [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-6-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section7.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section7.ipynb) The Unigram algorithm is often used in SentencePiece, which is the tokenization algorithm used by models like AlBERT, T5, mBART, Big Bird, and XLNet. 💡 This section covers Unigram in depth, going as far as showing a full implementation. You can skip to the end if you just want a general overview of the tokenization algorithm. ## [](#training-algorithm)Training algorithm Compared to BPE and WordPiece, Unigram works in the other direction: it starts from a big vocabulary and removes tokens from it until it reaches the desired vocabulary size. There are several options to use to build that base vocabulary: we can take the most common substrings in pre-tokenized words, for instance, or apply BPE on the initial corpus with a large vocabulary size. At each step of the training, the Unigram algorithm computes a loss over the corpus given the current vocabulary. Then, for each symbol in the vocabulary, the algorithm computes how much the overall loss would increase if the symbol was removed, and looks for the symbols that would increase it the least. Those symbols have a lower effect on the overall loss over the corpus, so in a sense they are “less needed” and are the best candidates for removal. This is all a very costly operation, so we don’t just remove the single symbol associated with the lowest loss increase, but the pp (pp being a hyperparameter you can control, usually 10 or 20) percent of the symbols associated with the lowest loss increase. This process is then repeated until the vocabulary has reached the desired size. Note that we never remove the base characters, to make sure any word can be tokenized. Now, this is still a bit vague: the main part of the algorithm is to compute a loss over the corpus and see how it changes when we remove some tokens from the vocabulary, but we haven’t explained how to do this yet. This step relies on the tokenization algorithm of a Unigram model, so we’ll dive into this next. We’ll reuse the corpus from the previous examples: ``` ("hug", 10), ("pug", 5), ("pun", 12), ("bun", 4), ("hugs", 5)``` and for this example, we will take all strict substrings for the initial vocabulary : ``` ["h", "u", "g", "hu", "ug", "p", "pu", "n", "un", "b", "bu", "s", "hug", "gs", "ugs"]``` ## [](#tokenization-algorithm)Tokenization algorithm A Unigram model is a type of language model that considers each token to be independent of the tokens before it. It’s the simplest language model, in the sense that the probability of token X given the previous context is just the probability of token X. So, if we used a Unigram language model to generate text, we would always predict the most common token. The probability of a given token is its frequency (the number of times we find it) in the original corpus, divided by the sum of all frequencies of all tokens in the vocabulary (to make sure the probabilities sum up to 1). For instance, `"ug"` is present in `"hug"`, `"pug"`, and `"hugs"`, so it has a frequency of 20 in our corpus. Here are the frequencies of all the possible subwords in the vocabulary: ``` ("h", 15) ("u", 36) ("g", 20) ("hu", 15) ("ug", 20) ("p", 17) ("pu", 17) ("n", 16) ("un", 16) ("b", 4) ("bu", 4) ("s", 5) ("hug", 15) ("gs", 5) ("ugs", 5)``` So, the sum of all frequencies is 210, and the probability of the subword `"ug"` is thus 20/210. ✏️ **Now your turn!** Write the code to compute the the frequencies above and double-check that the results shown are correct, as well as the total sum. Now, to tokenize a given word, we look at all the possible segmentations into tokens and compute the probability of each according to the Unigram model. Since all tokens are considered independent, this probability is just the product of the probability of each token. For instance, the tokenization `["p", "u", "g"]` of `"pug"` has the probability: P(\[‘‘p",‘‘u",‘‘g"\])\=P(‘‘p")×P(‘‘u")×P(‘‘g")\=5210×36210×20210\=0.000389P(\[\`\`p", \`\`u", \`\`g"\]) = P(\`\`p") \\times P(\`\`u") \\times P(\`\`g") = \\frac{5}{210} \\times \\frac{36}{210} \\times \\frac{20}{210} = 0.000389 Comparatively, the tokenization `["pu", "g"]` has the probability: P(\[‘‘pu",‘‘g"\])\=P(‘‘pu")×P(‘‘g")\=5210×20210\=0.0022676P(\[\`\`pu", \`\`g"\]) = P(\`\`pu") \\times P(\`\`g") = \\frac{5}{210} \\times \\frac{20}{210} = 0.0022676 so that one is way more likely. In general, tokenizations with the least tokens possible will have the highest probability (because of that division by 210 repeated for each token), which corresponds to what we want intuitively: to split a word into the least number of tokens possible. The tokenization of a word with the Unigram model is then the tokenization with the highest probability. In the example of `"pug"`, here are the probabilities we would get for each possible segmentation: ``` ["p", "u", "g"] : 0.000389 ["p", "ug"] : 0.0022676 ["pu", "g"] : 0.0022676``` So, `"pug"` would be tokenized as `["p", "ug"]` or `["pu", "g"]`, depending on which of those segmentations is encountered first (note that in a larger corpus, equality cases like this will be rare). In this case, it was easy to find all the possible segmentations and compute their probabilities, but in general it’s going to be a bit harder. There is a classic algorithm used for this, called the _Viterbi algorithm_. Essentially, we can build a graph to detect the possible segmentations of a given word by saying there is a branch from character _a_ to character _b_ if the subword from _a_ to _b_ is in the vocabulary, and attribute to that branch the probability of the subword. To find the path in that graph that is going to have the best score the Viterbi algorithm determines, for each position in the word, the segmentation with the best score that ends at that position. Since we go from the beginning to the end, that best score can be found by looping through all subwords ending at the current position and then using the best tokenization score from the position this subword begins at. Then, we just have to unroll the path taken to arrive at the end. Let’s take a look at an example using our vocabulary and the word `"unhug"`. For each position, the subwords with the best scores ending there are the following: ``` Character 0 (u): "u" (score 0.171429) Character 1 (n): "un" (score 0.076191) Character 2 (h): "un" "h" (score 0.005442) Character 3 (u): "un" "hu" (score 0.005442) Character 4 (g): "un" "hug" (score 0.005442)``` Thus `"unhug"` would be tokenized as `["un", "hug"]`. ✏️ **Now your turn!** Determine the tokenization of the word `"huggun"`, and its score. ## [](#back-to-training)Back to training Now that we have seen how the tokenization works, we can dive a little more deeply into the loss used during training. At any given stage, this loss is computed by tokenizing every word in the corpus, using the current vocabulary and the Unigram model determined by the frequencies of each token in the corpus (as seen before). Each word in the corpus has a score, and the loss is the negative log likelihood of those scores — that is, the sum for all the words in the corpus of all the `-log(P(word))`. Let’s go back to our example with the following corpus: ``` ("hug", 10), ("pug", 5), ("pun", 12), ("bun", 4), ("hugs", 5)``` The tokenization of each word with their respective scores is: ``` "hug": ["hug"] "pug": ["pu", "g"] "pun": ["pu", "n"] "bun": ["bu", "n"] "hugs": ["hug", "s"] ``` So the loss is: ``` 10 * (-log(0.071428)) + 5 * (-log(0.007710)) + 12 * (-log(0.006168)) + 4 * (-log(0.001451)) + 5 * (-log(0.001701)) = 169.8``` Now we need to compute how removing each token affects the loss. This is rather tedious, so we’ll just do it for two tokens here and save the whole process for when we have code to help us. In this (very) particular case, we had two equivalent tokenizations of all the words: as we saw earlier, for example, `"pug"` could be tokenized `["p", "ug"]` with the same score. Thus, removing the `"pu"` token from the vocabulary will give the exact same loss. On the other hand, removing `"hug"` will make the loss worse, because the tokenization of `"hug"` and `"hugs"` will become: ``` "hug": ["hu", "g"] "hugs": ["hu", "gs"] ``` These changes will cause the loss to rise by: ``` - 10 * (-log(0.071428)) + 10 * (-log(0.006802)) = 23.5``` Therefore, the token `"pu"` will probably be removed from the vocabulary, but not `"hug"`. ## [](#implementing-unigram)Implementing Unigram Now let’s implement everything we’ve seen so far in code. Like with BPE and WordPiece, this is not an efficient implementation of the Unigram algorithm (quite the opposite), but it should help you understand it a bit better. We will use the same corpus as before as an example: ``` corpus = [ "This is the Hugging Face Course.", "This chapter is about tokenization.", "This section shows several tokenizer algorithms.", "Hopefully, you will be able to understand how they are trained and generate tokens.", ]``` This time, we will use `xlnet-base-cased` as our model: ``` from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("xlnet-base-cased")``` Like for BPE and WordPiece, we begin by counting the number of occurrences of each word in the corpus: ``` from collections import defaultdict word_freqs = defaultdict(int) for text in corpus: words_with_offsets = tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(text) new_words = [word for word, offset in words_with_offsets] for word in new_words: word_freqs[word] += 1 word_freqs``` Then, we need to initialize our vocabulary to something larger than the vocab size we will want at the end. We have to include all the basic characters (otherwise we won’t be able to tokenize every word), but for the bigger substrings we’ll only keep the most common ones, so we sort them by frequency: ``` char_freqs = defaultdict(int) subwords_freqs = defaultdict(int) for word, freq in word_freqs.items(): for i in range(len(word)): char_freqs[word[i]] += freq for j in range(i + 2, len(word) + 1): subwords_freqs[word[i:j]] += freq sorted_subwords = sorted(subwords_freqs.items(), key=lambda x: x[1], reverse=True) sorted_subwords[:10]``` ``` [('▁t', 7), ('is', 5), ('er', 5), ('▁a', 5), ('▁to', 4), ('to', 4), ('en', 4), ('▁T', 3), ('▁Th', 3), ('▁Thi', 3)]``` We group the characters with the best subwords to arrive at an initial vocabulary of size 300: ``` token_freqs = list(char_freqs.items()) + sorted_subwords[: 300 - len(char_freqs)] token_freqs = {token: freq for token, freq in token_freqs}``` 💡 SentencePiece uses a more efficient algorithm called Enhanced Suffix Array (ESA) to create the initial vocabulary. Next, we compute the sum of all frequencies, to convert the frequencies into probabilities. For our model we will store the logarithms of the probabilities, because it’s more numerically stable to add logarithms than to multiply small numbers, and this will simplify the computation of the loss of the model: ``` from math import log total_sum = sum([freq for token, freq in token_freqs.items()]) model = {token: -log(freq / total_sum) for token, freq in token_freqs.items()}``` Now the main function is the one that tokenizes words using the Viterbi algorithm. As we saw before, that algorithm computes the best segmentation of each substring of the word, which we will store in a variable named `best_segmentations`. We will store one dictionary per position in the word (from 0 to its total length), with two keys: the index of the start of the last token in the best segmentation, and the score of the best segmentation. With the index of the start of the last token, we will be able to retrieve the full segmentation once the list is completely populated. Populating the list is done with just two loops: the main loop goes over each start position, and the second loop tries all substrings beginning at that start position. If the substring is in the vocabulary, we have a new segmentation of the word up until that end position, which we compare to what is in `best_segmentations`. Once the main loop is finished, we just start from the end and hop from one start position to the next, recording the tokens as we go, until we reach the start of the word: ``` def encode_word(word, model): best_segmentations = [{"start": 0, "score": 1}] + [ {"start": None, "score": None} for _ in range(len(word)) ] for start_idx in range(len(word)): best_score_at_start = best_segmentations[start_idx]["score"] for end_idx in range(start_idx + 1, len(word) + 1): token = word[start_idx:end_idx] if token in model and best_score_at_start is not None: score = model[token] + best_score_at_start if ( best_segmentations[end_idx]["score"] is None or best_segmentations[end_idx]["score"] > score ): best_segmentations[end_idx] = {"start": start_idx, "score": score} segmentation = best_segmentations[-1] if segmentation["score"] is None: return ["<unk>"], None score = segmentation["score"] start = segmentation["start"] end = len(word) tokens = [] while start != 0: tokens.insert(0, word[start:end]) next_start = best_segmentations[start]["start"] end = start start = next_start tokens.insert(0, word[start:end]) return tokens, score``` We can already try our initial model on some words: ``` print(encode_word("Hopefully", model)) print(encode_word("This", model))``` ``` (['H', 'o', 'p', 'e', 'f', 'u', 'll', 'y'], 41.5157494601402) (['This'], 6.288267030694535)``` Now it’s easy to compute the loss of the model on the corpus! ``` def compute_loss(model): loss = 0 for word, freq in word_freqs.items(): _, word_loss = encode_word(word, model) loss += freq * word_loss return loss``` We can check it works on the model we have: Computing the scores for each token is not very hard either; we just have to compute the loss for the models obtained by deleting each token: ``` import copy def compute_scores(model): scores = {} model_loss = compute_loss(model) for token, score in model.items(): if len(token) == 1: continue model_without_token = copy.deepcopy(model) _ = model_without_token.pop(token) scores[token] = compute_loss(model_without_token) - model_loss return scores``` We can try it on a given token: ``` scores = compute_scores(model) print(scores["ll"]) print(scores["his"])``` Since `"ll"` is used in the tokenization of `"Hopefully"`, and removing it will probably make us use the token `"l"` twice instead, we expect it will have a positive loss. `"his"` is only used inside the word `"This"`, which is tokenized as itself, so we expect it to have a zero loss. Here are the results: 💡 This approach is very inefficient, so SentencePiece uses an approximation of the loss of the model without token X: instead of starting from scratch, it just replaces token X by its segmentation in the vocabulary that is left. This way, all the scores can be computed at once at the same time as the model loss. With all of this in place, the last thing we need to do is add the special tokens used by the model to the vocabulary, then loop until we have pruned enough tokens from the vocabulary to reach our desired size: ``` percent_to_remove = 0.1 while len(model) > 100: scores = compute_scores(model) sorted_scores = sorted(scores.items(), key=lambda x: x[1]) for i in range(int(len(model) * percent_to_remove)): _ = token_freqs.pop(sorted_scores[i][0]) total_sum = sum([freq for token, freq in token_freqs.items()]) model = {token: -log(freq / total_sum) for token, freq in token_freqs.items()}``` Then, to tokenize some text, we just need to apply the pre-tokenization and then use our `encode_word()` function: ``` def tokenize(text, model): words_with_offsets = tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(text) pre_tokenized_text = [word for word, offset in words_with_offsets] encoded_words = [encode_word(word, model)[0] for word in pre_tokenized_text] return sum(encoded_words, []) tokenize("This is the Hugging Face course.", model)``` ``` ['▁This', '▁is', '▁the', '▁Hugging', '▁Face', '▁', 'c', 'ou', 'r', 's', 'e', '.']``` That’s it for Unigram! Hopefully by now you’re feeling like an expert in all things tokenizer. In the next section, we will delve into the building blocks of the 🤗 Tokenizers library, and show you how you can use them to build your own tokenizer.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Unigram tokenization - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter6/7"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Unigram tokenization - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;unigram-tokenization&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;training-algorithm&quot;,&quot;title&quot;:&quot;Training algorithm&quot;},{&quot;local&quot;:&quot;tokenization-algorithm&quot;,&quot;title&quot;:&quot;Tokenization algorithm&quot;},{&quot;local&quot;:&quot;back-to-training&quot;,&quot;title&quot;:&quot;Back to training&quot;},{&quot;local&quot;:&quot;implementing-unigram&quot;,&quot;title&quot;:&quot;Implementing Unigram&quot;}],&quot;title&quot;:&quot;Unigram tokenization&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter6/7&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Unigram tokenization&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"> <div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation </p> <div class="flex items-center"><p class="font-semibold">Unigram tokenization</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "> <button class=" " type="button"> <h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> </button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en" selected="">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"> <button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"> <svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> </a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->0. Setup<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->1. Transformer models<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->2. Using 🤗 Transformers<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->3. Fine-tuning a pretrained model<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->4. Sharing models and tokenizers<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->5. The 🤗 Datasets library<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->6. The 🤗 Tokenizers library<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/1?fw=pt"><!-- HTML_TAG_START -->Introduction<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/2?fw=pt"><!-- HTML_TAG_START -->Training a new tokenizer from an old one<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3?fw=pt"><!-- HTML_TAG_START -->Fast tokenizers' special powers<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3b?fw=pt"><!-- HTML_TAG_START -->Fast tokenizers in the QA pipeline<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/4?fw=pt"><!-- HTML_TAG_START -->Normalization and pre-tokenization<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/5?fw=pt"><!-- HTML_TAG_START -->Byte-Pair Encoding tokenization<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/6?fw=pt"><!-- HTML_TAG_START -->WordPiece tokenization<!-- HTML_TAG_END --> </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter6/7?fw=pt"><!-- HTML_TAG_START -->Unigram tokenization<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/8?fw=pt"><!-- HTML_TAG_START -->Building a tokenizer, block by block<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/9?fw=pt"><!-- HTML_TAG_START -->Tokenizers, check!<!-- HTML_TAG_END --> </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/10?fw=pt"><!-- HTML_TAG_START -->End-of-chapter quiz<!-- HTML_TAG_END --> </a> </div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->7. Main NLP tasks<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->8. How to ask for help<!-- HTML_TAG_END --></span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->9. Building and sharing demos<!-- HTML_TAG_END --></span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span> </span></span> </div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span><!-- HTML_TAG_START -->Course Events<!-- HTML_TAG_END --></span> </span></span> </div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="unigram-tokenization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#unigram-tokenization"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Unigram tokenization</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-6-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section7.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section7.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>The Unigram algorithm is often used in SentencePiece, which is the tokenization algorithm used by models like AlBERT, T5, mBART, Big Bird, and XLNet.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/TGZfZVuF9Yc" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 This section covers Unigram in depth, going as far as showing a full implementation. You can skip to the end if you just want a general overview of the tokenization algorithm.</p></div> <h2 class="relative group"><a id="training-algorithm" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-algorithm"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training algorithm</span></h2> <p>Compared to BPE and WordPiece, Unigram works in the other direction: it starts from a big vocabulary and removes tokens from it until it reaches the desired vocabulary size. There are several options to use to build that base vocabulary: we can take the most common substrings in pre-tokenized words, for instance, or apply BPE on the initial corpus with a large vocabulary size.</p> <p>At each step of the training, the Unigram algorithm computes a loss over the corpus given the current vocabulary. Then, for each symbol in the vocabulary, the algorithm computes how much the overall loss would increase if the symbol was removed, and looks for the symbols that would increase it the least. Those symbols have a lower effect on the overall loss over the corpus, so in a sense they are “less needed” and are the best candidates for removal.</p> <p>This is all a very costly operation, so we don’t just remove the single symbol associated with the lowest loss increase, but the <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi></mrow><annotation encoding="application/x-tex">p</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">p</span></span></span></span> (<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi></mrow><annotation encoding="application/x-tex">p</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">p</span></span></span></span> being a hyperparameter you can control, usually 10 or 20) percent of the symbols associated with the lowest loss increase. This process is then repeated until the vocabulary has reached the desired size.</p> <p>Note that we never remove the base characters, to make sure any word can be tokenized.</p> <p>Now, this is still a bit vague: the main part of the algorithm is to compute a loss over the corpus and see how it changes when we remove some tokens from the vocabulary, but we haven’t explained how to do this yet. This step relies on the tokenization algorithm of a Unigram model, so we’ll dive into this next.</p> <p>We’ll reuse the corpus from the previous examples:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-string">"hug"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">10</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"pug"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"pun"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">12</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"bun"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"hugs"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>)</pre></div> <p>and for this example, we will take all strict substrings for the initial vocabulary :</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-selector-attr">[<span class="hljs-string">"h"</span>, <span class="hljs-string">"u"</span>, <span class="hljs-string">"g"</span>, <span class="hljs-string">"hu"</span>, <span class="hljs-string">"ug"</span>, <span class="hljs-string">"p"</span>, <span class="hljs-string">"pu"</span>, <span class="hljs-string">"n"</span>, <span class="hljs-string">"un"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"bu"</span>, <span class="hljs-string">"s"</span>, <span class="hljs-string">"hug"</span>, <span class="hljs-string">"gs"</span>, <span class="hljs-string">"ugs"</span>]</span></pre></div> <h2 class="relative group"><a id="tokenization-algorithm" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tokenization-algorithm"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Tokenization algorithm</span></h2> <p>A Unigram model is a type of language model that considers each token to be independent of the tokens before it. It’s the simplest language model, in the sense that the probability of token X given the previous context is just the probability of token X. So, if we used a Unigram language model to generate text, we would always predict the most common token.</p> <p>The probability of a given token is its frequency (the number of times we find it) in the original corpus, divided by the sum of all frequencies of all tokens in the vocabulary (to make sure the probabilities sum up to 1). For instance, <code>"ug"</code> is present in <code>"hug"</code>, <code>"pug"</code>, and <code>"hugs"</code>, so it has a frequency of 20 in our corpus.</p> <p>Here are the frequencies of all the possible subwords in the vocabulary:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-string">"h"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">15</span>) (<span class="hljs-string">"u"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">36</span>) (<span class="hljs-string">"g"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">20</span>) (<span class="hljs-string">"hu"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">15</span>) (<span class="hljs-string">"ug"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">20</span>) (<span class="hljs-string">"p"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">17</span>) (<span class="hljs-string">"pu"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">17</span>) (<span class="hljs-string">"n"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">16</span>) (<span class="hljs-string">"un"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">16</span>) (<span class="hljs-string">"b"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4</span>) (<span class="hljs-string">"bu"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4</span>) (<span class="hljs-string">"s"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>) (<span class="hljs-string">"hug"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">15</span>) (<span class="hljs-string">"gs"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>) (<span class="hljs-string">"ugs"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>)</pre></div> <p>So, the sum of all frequencies is 210, and the probability of the subword <code>"ug"</code> is thus 20/210.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Now your turn!</strong> Write the code to compute the the frequencies above and double-check that the results shown are correct, as well as the total sum.</p></div> <p>Now, to tokenize a given word, we look at all the possible segmentations into tokens and compute the probability of each according to the Unigram model. Since all tokens are considered independent, this probability is just the product of the probability of each token. For instance, the tokenization <code>["p", "u", "g"]</code> of <code>"pug"</code> has the probability: <span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>P</mi><mo stretchy="false">(</mo><mo stretchy="false">[</mo><mi mathvariant="normal">‘</mi><mi mathvariant="normal">‘</mi><mi>p</mi><mi mathvariant="normal">"</mi><mo separator="true">,</mo><mi mathvariant="normal">‘</mi><mi mathvariant="normal">‘</mi><mi>u</mi><mi mathvariant="normal">"</mi><mo separator="true">,</mo><mi mathvariant="normal">‘</mi><mi mathvariant="normal">‘</mi><mi>g</mi><mi mathvariant="normal">"</mi><mo stretchy="false">]</mo><mo stretchy="false">)</mo><mo>=</mo><mi>P</mi><mo stretchy="false">(</mo><mi mathvariant="normal">‘</mi><mi mathvariant="normal">‘</mi><mi>p</mi><mi mathvariant="normal">"</mi><mo stretchy="false">)</mo><mo>×</mo><mi>P</mi><mo stretchy="false">(</mo><mi mathvariant="normal">‘</mi><mi mathvariant="normal">‘</mi><mi>u</mi><mi mathvariant="normal">"</mi><mo stretchy="false">)</mo><mo>×</mo><mi>P</mi><mo stretchy="false">(</mo><mi mathvariant="normal">‘</mi><mi mathvariant="normal">‘</mi><mi>g</mi><mi mathvariant="normal">"</mi><mo stretchy="false">)</mo><mo>=</mo><mfrac><mn>5</mn><mn>210</mn></mfrac><mo>×</mo><mfrac><mn>36</mn><mn>210</mn></mfrac><mo>×</mo><mfrac><mn>20</mn><mn>210</mn></mfrac><mo>=</mo><mn>0.000389</mn></mrow><annotation encoding="application/x-tex">P([``p", ``u", ``g"]) = P(``p") \times P(``u") \times P(``g") = \frac{5}{210} \times \frac{36}{210} \times \frac{20}{210} = 0.000389</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mopen">([</span><span class="mord">‘‘</span><span class="mord mathnormal">p</span><span class="mord">"</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">‘‘</span><span class="mord mathnormal">u</span><span class="mord">"</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">‘‘</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord">"</span><span class="mclose">])</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mopen">(</span><span class="mord">‘‘</span><span class="mord mathnormal">p</span><span class="mord">"</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mopen">(</span><span class="mord">‘‘</span><span class="mord mathnormal">u</span><span class="mord">"</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mopen">(</span><span class="mord">‘‘</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord">"</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:2.0074em;vertical-align:-0.686em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.3214em;"><span style="top:-2.314em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">210</span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">5</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.686em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:2.0074em;vertical-align:-0.686em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.3214em;"><span style="top:-2.314em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">210</span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">36</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.686em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:2.0074em;vertical-align:-0.686em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.3214em;"><span style="top:-2.314em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">210</span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">20</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.686em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0.000389</span></span></span></span></span></p> <p>Comparatively, the tokenization <code>["pu", "g"]</code> has the probability: <span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>P</mi><mo stretchy="false">(</mo><mo stretchy="false">[</mo><mi mathvariant="normal">‘</mi><mi mathvariant="normal">‘</mi><mi>p</mi><mi>u</mi><mi mathvariant="normal">"</mi><mo separator="true">,</mo><mi mathvariant="normal">‘</mi><mi mathvariant="normal">‘</mi><mi>g</mi><mi mathvariant="normal">"</mi><mo stretchy="false">]</mo><mo stretchy="false">)</mo><mo>=</mo><mi>P</mi><mo stretchy="false">(</mo><mi mathvariant="normal">‘</mi><mi mathvariant="normal">‘</mi><mi>p</mi><mi>u</mi><mi mathvariant="normal">"</mi><mo stretchy="false">)</mo><mo>×</mo><mi>P</mi><mo stretchy="false">(</mo><mi mathvariant="normal">‘</mi><mi mathvariant="normal">‘</mi><mi>g</mi><mi mathvariant="normal">"</mi><mo stretchy="false">)</mo><mo>=</mo><mfrac><mn>5</mn><mn>210</mn></mfrac><mo>×</mo><mfrac><mn>20</mn><mn>210</mn></mfrac><mo>=</mo><mn>0.0022676</mn></mrow><annotation encoding="application/x-tex">P([``pu", ``g"]) = P(``pu") \times P(``g") = \frac{5}{210} \times \frac{20}{210} = 0.0022676</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mopen">([</span><span class="mord">‘‘</span><span class="mord mathnormal">p</span><span class="mord mathnormal">u</span><span class="mord">"</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">‘‘</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord">"</span><span class="mclose">])</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mopen">(</span><span class="mord">‘‘</span><span class="mord mathnormal">p</span><span class="mord mathnormal">u</span><span class="mord">"</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mopen">(</span><span class="mord">‘‘</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord">"</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:2.0074em;vertical-align:-0.686em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.3214em;"><span style="top:-2.314em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">210</span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">5</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.686em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:2.0074em;vertical-align:-0.686em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.3214em;"><span style="top:-2.314em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">210</span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord">20</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.686em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">0.0022676</span></span></span></span></span></p> <p>so that one is way more likely. In general, tokenizations with the least tokens possible will have the highest probability (because of that division by 210 repeated for each token), which corresponds to what we want intuitively: to split a word into the least number of tokens possible.</p> <p>The tokenization of a word with the Unigram model is then the tokenization with the highest probability. In the example of <code>"pug"</code>, here are the probabilities we would get for each possible segmentation:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">"p"</span>, <span class="hljs-string">"u"</span>, <span class="hljs-string">"g"</span>] : 0.000389 [<span class="hljs-string">"p"</span>, <span class="hljs-string">"ug"</span>] : 0.0022676 [<span class="hljs-string">"pu"</span>, <span class="hljs-string">"g"</span>] : 0.0022676</pre></div> <p>So, <code>"pug"</code> would be tokenized as <code>["p", "ug"]</code> or <code>["pu", "g"]</code>, depending on which of those segmentations is encountered first (note that in a larger corpus, equality cases like this will be rare).</p> <p>In this case, it was easy to find all the possible segmentations and compute their probabilities, but in general it’s going to be a bit harder. There is a classic algorithm used for this, called the <em>Viterbi algorithm</em>. Essentially, we can build a graph to detect the possible segmentations of a given word by saying there is a branch from character <em>a</em> to character <em>b</em> if the subword from <em>a</em> to <em>b</em> is in the vocabulary, and attribute to that branch the probability of the subword.</p> <p>To find the path in that graph that is going to have the best score the Viterbi algorithm determines, for each position in the word, the segmentation with the best score that ends at that position. Since we go from the beginning to the end, that best score can be found by looping through all subwords ending at the current position and then using the best tokenization score from the position this subword begins at. Then, we just have to unroll the path taken to arrive at the end.</p> <p>Let’s take a look at an example using our vocabulary and the word <code>"unhug"</code>. For each position, the subwords with the best scores ending there are the following:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-attribute">Character</span> <span class="hljs-number">0</span> (u): <span class="hljs-string">"u"</span> (score <span class="hljs-number">0</span>.<span class="hljs-number">171429</span>) <span class="hljs-attribute">Character</span> <span class="hljs-number">1</span> (n): <span class="hljs-string">"un"</span> (score <span class="hljs-number">0</span>.<span class="hljs-number">076191</span>) <span class="hljs-attribute">Character</span> <span class="hljs-number">2</span> (h): <span class="hljs-string">"un"</span> <span class="hljs-string">"h"</span> (score <span class="hljs-number">0</span>.<span class="hljs-number">005442</span>) <span class="hljs-attribute">Character</span> <span class="hljs-number">3</span> (u): <span class="hljs-string">"un"</span> <span class="hljs-string">"hu"</span> (score <span class="hljs-number">0</span>.<span class="hljs-number">005442</span>) <span class="hljs-attribute">Character</span> <span class="hljs-number">4</span> (g): <span class="hljs-string">"un"</span> <span class="hljs-string">"hug"</span> (score <span class="hljs-number">0</span>.<span class="hljs-number">005442</span>)</pre></div> <p>Thus <code>"unhug"</code> would be tokenized as <code>["un", "hug"]</code>.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Now your turn!</strong> Determine the tokenization of the word <code>"huggun"</code>, and its score.</p></div> <h2 class="relative group"><a id="back-to-training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#back-to-training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Back to training</span></h2> <p>Now that we have seen how the tokenization works, we can dive a little more deeply into the loss used during training. At any given stage, this loss is computed by tokenizing every word in the corpus, using the current vocabulary and the Unigram model determined by the frequencies of each token in the corpus (as seen before).</p> <p>Each word in the corpus has a score, and the loss is the negative log likelihood of those scores — that is, the sum for all the words in the corpus of all the <code>-log(P(word))</code>.</p> <p>Let’s go back to our example with the following corpus:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-string">"hug"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">10</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"pug"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"pun"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">12</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"bun"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4</span>)<span class="hljs-punctuation">,</span> (<span class="hljs-string">"hugs"</span><span class="hljs-punctuation">,</span> <span class="hljs-number">5</span>)</pre></div> <p>The tokenization of each word with their respective scores is:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">"hug"</span>: [<span class="hljs-string">"hug"</span>] <span class="hljs-comment">(score 0.071428)</span> <span class="hljs-string">"pug"</span>: [<span class="hljs-string">"pu"</span>, <span class="hljs-string">"g"</span>] <span class="hljs-comment">(score 0.007710)</span> <span class="hljs-string">"pun"</span>: [<span class="hljs-string">"pu"</span>, <span class="hljs-string">"n"</span>] <span class="hljs-comment">(score 0.006168)</span> <span class="hljs-string">"bun"</span>: [<span class="hljs-string">"bu"</span>, <span class="hljs-string">"n"</span>] <span class="hljs-comment">(score 0.001451)</span> <span class="hljs-string">"hugs"</span>: [<span class="hljs-string">"hug"</span>, <span class="hljs-string">"s"</span>] <span class="hljs-comment">(score 0.001701)</span></pre></div> <p>So the loss is:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-attribute">10</span> * (-log(<span class="hljs-number">0</span>.<span class="hljs-number">071428</span>)) + <span class="hljs-number">5</span> * (-log(<span class="hljs-number">0</span>.<span class="hljs-number">007710</span>)) + <span class="hljs-number">12</span> * (-log(<span class="hljs-number">0</span>.<span class="hljs-number">006168</span>)) + <span class="hljs-number">4</span> * (-log(<span class="hljs-number">0</span>.<span class="hljs-number">001451</span>)) + <span class="hljs-number">5</span> * (-log(<span class="hljs-number">0</span>.<span class="hljs-number">001701</span>)) = <span class="hljs-number">169</span>.<span class="hljs-number">8</span></pre></div> <p>Now we need to compute how removing each token affects the loss. This is rather tedious, so we’ll just do it for two tokens here and save the whole process for when we have code to help us. In this (very) particular case, we had two equivalent tokenizations of all the words: as we saw earlier, for example, <code>"pug"</code> could be tokenized <code>["p", "ug"]</code> with the same score. Thus, removing the <code>"pu"</code> token from the vocabulary will give the exact same loss.</p> <p>On the other hand, removing <code>"hug"</code> will make the loss worse, because the tokenization of <code>"hug"</code> and <code>"hugs"</code> will become:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">"hug"</span>: [<span class="hljs-string">"hu"</span>, <span class="hljs-string">"g"</span>] <span class="hljs-comment">(score 0.006802)</span> <span class="hljs-string">"hugs"</span>: [<span class="hljs-string">"hu"</span>, <span class="hljs-string">"gs"</span>] <span class="hljs-comment">(score 0.001701)</span></pre></div> <p>These changes will cause the loss to rise by:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>- <span class="hljs-number">10</span> * (<span class="hljs-name">-log</span>(<span class="hljs-number">0.071428</span>)) + <span class="hljs-number">10</span> * (<span class="hljs-name">-log</span>(<span class="hljs-number">0.006802</span>)) = <span class="hljs-number">23.5</span></pre></div> <p>Therefore, the token <code>"pu"</code> will probably be removed from the vocabulary, but not <code>"hug"</code>.</p> <h2 class="relative group"><a id="implementing-unigram" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#implementing-unigram"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Implementing Unigram</span></h2> <p>Now let’s implement everything we’ve seen so far in code. Like with BPE and WordPiece, this is not an efficient implementation of the Unigram algorithm (quite the opposite), but it should help you understand it a bit better.</p> <p>We will use the same corpus as before as an example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>corpus = [ <span class="hljs-string">"This is the Hugging Face Course."</span>, <span class="hljs-string">"This chapter is about tokenization."</span>, <span class="hljs-string">"This section shows several tokenizer algorithms."</span>, <span class="hljs-string">"Hopefully, you will be able to understand how they are trained and generate tokens."</span>, ]</pre></div> <p>This time, we will use <code>xlnet-base-cased</code> as our model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"xlnet-base-cased"</span>)</pre></div> <p>Like for BPE and WordPiece, we begin by counting the number of occurrences of each word in the corpus:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> defaultdict word_freqs = defaultdict(<span class="hljs-built_in">int</span>) <span class="hljs-keyword">for</span> text <span class="hljs-keyword">in</span> corpus: words_with_offsets = tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(text) new_words = [word <span class="hljs-keyword">for</span> word, offset <span class="hljs-keyword">in</span> words_with_offsets] <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> new_words: word_freqs[word] += <span class="hljs-number">1</span> word_freqs</pre></div> <p>Then, we need to initialize our vocabulary to something larger than the vocab size we will want at the end. We have to include all the basic characters (otherwise we won’t be able to tokenize every word), but for the bigger substrings we’ll only keep the most common ones, so we sort them by frequency:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>char_freqs = defaultdict(<span class="hljs-built_in">int</span>) subwords_freqs = defaultdict(<span class="hljs-built_in">int</span>) <span class="hljs-keyword">for</span> word, freq <span class="hljs-keyword">in</span> word_freqs.items(): <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(word)): char_freqs[word[i]] += freq <span class="hljs-comment"># Loop through the subwords of length at least 2</span> <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(i + <span class="hljs-number">2</span>, <span class="hljs-built_in">len</span>(word) + <span class="hljs-number">1</span>): subwords_freqs[word[i:j]] += freq <span class="hljs-comment"># Sort subwords by frequency</span> sorted_subwords = <span class="hljs-built_in">sorted</span>(subwords_freqs.items(), key=<span class="hljs-keyword">lambda</span> x: x[<span class="hljs-number">1</span>], reverse=<span class="hljs-literal">True</span>) sorted_subwords[:<span class="hljs-number">10</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[(<span class="hljs-string">'▁t'</span>, <span class="hljs-number">7</span>), (<span class="hljs-string">'is'</span>, <span class="hljs-number">5</span>), (<span class="hljs-string">'er'</span>, <span class="hljs-number">5</span>), (<span class="hljs-string">'▁a'</span>, <span class="hljs-number">5</span>), (<span class="hljs-string">'▁to'</span>, <span class="hljs-number">4</span>), (<span class="hljs-string">'to'</span>, <span class="hljs-number">4</span>), (<span class="hljs-string">'en'</span>, <span class="hljs-number">4</span>), (<span class="hljs-string">'▁T'</span>, <span class="hljs-number">3</span>), (<span class="hljs-string">'▁Th'</span>, <span class="hljs-number">3</span>), (<span class="hljs-string">'▁Thi'</span>, <span class="hljs-number">3</span>)]</pre></div> <p>We group the characters with the best subwords to arrive at an initial vocabulary of size 300:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>token_freqs = <span class="hljs-built_in">list</span>(char_freqs.items()) + sorted_subwords[: <span class="hljs-number">300</span> - <span class="hljs-built_in">len</span>(char_freqs)] token_freqs = {token: freq <span class="hljs-keyword">for</span> token, freq <span class="hljs-keyword">in</span> token_freqs}</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 SentencePiece uses a more efficient algorithm called Enhanced Suffix Array (ESA) to create the initial vocabulary.</p></div> <p>Next, we compute the sum of all frequencies, to convert the frequencies into probabilities. For our model we will store the logarithms of the probabilities, because it’s more numerically stable to add logarithms than to multiply small numbers, and this will simplify the computation of the loss of the model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> log total_sum = <span class="hljs-built_in">sum</span>([freq <span class="hljs-keyword">for</span> token, freq <span class="hljs-keyword">in</span> token_freqs.items()]) model = {token: -log(freq / total_sum) <span class="hljs-keyword">for</span> token, freq <span class="hljs-keyword">in</span> token_freqs.items()}</pre></div> <p>Now the main function is the one that tokenizes words using the Viterbi algorithm. As we saw before, that algorithm computes the best segmentation of each substring of the word, which we will store in a variable named <code>best_segmentations</code>. We will store one dictionary per position in the word (from 0 to its total length), with two keys: the index of the start of the last token in the best segmentation, and the score of the best segmentation. With the index of the start of the last token, we will be able to retrieve the full segmentation once the list is completely populated.</p> <p>Populating the list is done with just two loops: the main loop goes over each start position, and the second loop tries all substrings beginning at that start position. If the substring is in the vocabulary, we have a new segmentation of the word up until that end position, which we compare to what is in <code>best_segmentations</code>.</p> <p>Once the main loop is finished, we just start from the end and hop from one start position to the next, recording the tokens as we go, until we reach the start of the word:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">encode_word</span>(<span class="hljs-params">word, model</span>): best_segmentations = [{<span class="hljs-string">"start"</span>: <span class="hljs-number">0</span>, <span class="hljs-string">"score"</span>: <span class="hljs-number">1</span>}] + [ {<span class="hljs-string">"start"</span>: <span class="hljs-literal">None</span>, <span class="hljs-string">"score"</span>: <span class="hljs-literal">None</span>} <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(word)) ] <span class="hljs-keyword">for</span> start_idx <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(word)): <span class="hljs-comment"># This should be properly filled by the previous steps of the loop</span> best_score_at_start = best_segmentations[start_idx][<span class="hljs-string">"score"</span>] <span class="hljs-keyword">for</span> end_idx <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(start_idx + <span class="hljs-number">1</span>, <span class="hljs-built_in">len</span>(word) + <span class="hljs-number">1</span>): token = word[start_idx:end_idx] <span class="hljs-keyword">if</span> token <span class="hljs-keyword">in</span> model <span class="hljs-keyword">and</span> best_score_at_start <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>: score = model[token] + best_score_at_start <span class="hljs-comment"># If we have found a better segmentation ending at end_idx, we update</span> <span class="hljs-keyword">if</span> ( best_segmentations[end_idx][<span class="hljs-string">"score"</span>] <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">or</span> best_segmentations[end_idx][<span class="hljs-string">"score"</span>] &gt; score ): best_segmentations[end_idx] = {<span class="hljs-string">"start"</span>: start_idx, <span class="hljs-string">"score"</span>: score} segmentation = best_segmentations[-<span class="hljs-number">1</span>] <span class="hljs-keyword">if</span> segmentation[<span class="hljs-string">"score"</span>] <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>: <span class="hljs-comment"># We did not find a tokenization of the word -&gt; unknown</span> <span class="hljs-keyword">return</span> [<span class="hljs-string">"&lt;unk&gt;"</span>], <span class="hljs-literal">None</span> score = segmentation[<span class="hljs-string">"score"</span>] start = segmentation[<span class="hljs-string">"start"</span>] end = <span class="hljs-built_in">len</span>(word) tokens = [] <span class="hljs-keyword">while</span> start != <span class="hljs-number">0</span>: tokens.insert(<span class="hljs-number">0</span>, word[start:end]) next_start = best_segmentations[start][<span class="hljs-string">"start"</span>] end = start start = next_start tokens.insert(<span class="hljs-number">0</span>, word[start:end]) <span class="hljs-keyword">return</span> tokens, score</pre></div> <p>We can already try our initial model on some words:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(encode_word(<span class="hljs-string">"Hopefully"</span>, model)) <span class="hljs-built_in">print</span>(encode_word(<span class="hljs-string">"This"</span>, model))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>([<span class="hljs-string">'H'</span>, <span class="hljs-string">'o'</span>, <span class="hljs-string">'p'</span>, <span class="hljs-string">'e'</span>, <span class="hljs-string">'f'</span>, <span class="hljs-string">'u'</span>, <span class="hljs-string">'ll'</span>, <span class="hljs-string">'y'</span>], <span class="hljs-number">41.5157494601402</span>) ([<span class="hljs-string">'This'</span>], <span class="hljs-number">6.288267030694535</span>)</pre></div> <p>Now it’s easy to compute the loss of the model on the corpus!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_loss</span>(<span class="hljs-params">model</span>): loss = <span class="hljs-number">0</span> <span class="hljs-keyword">for</span> word, freq <span class="hljs-keyword">in</span> word_freqs.items(): _, word_loss = encode_word(word, model) loss += freq * word_loss <span class="hljs-keyword">return</span> loss</pre></div> <p>We can check it works on the model we have:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>compute_loss(model)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">413.10377642940875</span></pre></div> <p>Computing the scores for each token is not very hard either; we just have to compute the loss for the models obtained by deleting each token:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> copy <span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_scores</span>(<span class="hljs-params">model</span>): scores = {} model_loss = compute_loss(model) <span class="hljs-keyword">for</span> token, score <span class="hljs-keyword">in</span> model.items(): <span class="hljs-comment"># We always keep tokens of length 1</span> <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(token) == <span class="hljs-number">1</span>: <span class="hljs-keyword">continue</span> model_without_token = copy.deepcopy(model) _ = model_without_token.pop(token) scores[token] = compute_loss(model_without_token) - model_loss <span class="hljs-keyword">return</span> scores</pre></div> <p>We can try it on a given token:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>scores = compute_scores(model) <span class="hljs-built_in">print</span>(scores[<span class="hljs-string">"ll"</span>]) <span class="hljs-built_in">print</span>(scores[<span class="hljs-string">"his"</span>])</pre></div> <p>Since <code>"ll"</code> is used in the tokenization of <code>"Hopefully"</code>, and removing it will probably make us use the token <code>"l"</code> twice instead, we expect it will have a positive loss. <code>"his"</code> is only used inside the word <code>"This"</code>, which is tokenized as itself, so we expect it to have a zero loss. Here are the results:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">6.376412403623874</span> <span class="hljs-number">0.0</span></pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 This approach is very inefficient, so SentencePiece uses an approximation of the loss of the model without token X: instead of starting from scratch, it just replaces token X by its segmentation in the vocabulary that is left. This way, all the scores can be computed at once at the same time as the model loss.</p></div> <p>With all of this in place, the last thing we need to do is add the special tokens used by the model to the vocabulary, then loop until we have pruned enough tokens from the vocabulary to reach our desired size:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>percent_to_remove = <span class="hljs-number">0.1</span> <span class="hljs-keyword">while</span> <span class="hljs-built_in">len</span>(model) &gt; <span class="hljs-number">100</span>: scores = compute_scores(model) sorted_scores = <span class="hljs-built_in">sorted</span>(scores.items(), key=<span class="hljs-keyword">lambda</span> x: x[<span class="hljs-number">1</span>]) <span class="hljs-comment"># Remove percent_to_remove tokens with the lowest scores.</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">int</span>(<span class="hljs-built_in">len</span>(model) * percent_to_remove)): _ = token_freqs.pop(sorted_scores[i][<span class="hljs-number">0</span>]) total_sum = <span class="hljs-built_in">sum</span>([freq <span class="hljs-keyword">for</span> token, freq <span class="hljs-keyword">in</span> token_freqs.items()]) model = {token: -log(freq / total_sum) <span class="hljs-keyword">for</span> token, freq <span class="hljs-keyword">in</span> token_freqs.items()}</pre></div> <p>Then, to tokenize some text, we just need to apply the pre-tokenization and then use our <code>encode_word()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize</span>(<span class="hljs-params">text, model</span>): words_with_offsets = tokenizer.backend_tokenizer.pre_tokenizer.pre_tokenize_str(text) pre_tokenized_text = [word <span class="hljs-keyword">for</span> word, offset <span class="hljs-keyword">in</span> words_with_offsets] encoded_words = [encode_word(word, model)[<span class="hljs-number">0</span>] <span class="hljs-keyword">for</span> word <span class="hljs-keyword">in</span> pre_tokenized_text] <span class="hljs-keyword">return</span> <span class="hljs-built_in">sum</span>(encoded_words, []) tokenize(<span class="hljs-string">"This is the Hugging Face course."</span>, model)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'▁This'</span>, <span class="hljs-string">'▁is'</span>, <span class="hljs-string">'▁the'</span>, <span class="hljs-string">'▁Hugging'</span>, <span class="hljs-string">'▁Face'</span>, <span class="hljs-string">'▁'</span>, <span class="hljs-string">'c'</span>, <span class="hljs-string">'ou'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'e'</span>, <span class="hljs-string">'.'</span>]</pre></div> <p>That’s it for Unigram! Hopefully by now you’re feeling like an expert in all things tokenizer. In the next section, we will delve into the building blocks of the 🤗 Tokenizers library, and show you how you can use them to build your own tokenizer.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter6/6?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>WordPiece tokenization</a> <a href="/learn/nlp-course/chapter6/8?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Building a tokenizer, block by block<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;unigram-tokenization&quot;,&quot;url&quot;:&quot;#unigram-tokenization&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Training algorithm&quot;,&quot;id&quot;:&quot;training-algorithm&quot;,&quot;url&quot;:&quot;#training-algorithm&quot;},{&quot;title&quot;:&quot;Tokenization algorithm&quot;,&quot;id&quot;:&quot;tokenization-algorithm&quot;,&quot;url&quot;:&quot;#tokenization-algorithm&quot;},{&quot;title&quot;:&quot;Back to training&quot;,&quot;id&quot;:&quot;back-to-training&quot;,&quot;url&quot;:&quot;#back-to-training&quot;},{&quot;title&quot;:&quot;Implementing Unigram&quot;,&quot;id&quot;:&quot;implementing-unigram&quot;,&quot;url&quot;:&quot;#implementing-unigram&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#unigram-tokenization" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-unigram-tokenization"><wbr>Unigram tokenization</a> <a href="#training-algorithm" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-algorithm"><wbr>Training algorithm</a> <a href="#tokenization-algorithm" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tokenization-algorithm"><wbr>Tokenization algorithm</a> <a href="#back-to-training" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-back-to-training"><wbr>Back to training</a> <a href="#implementing-unigram" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-implementing-unigram"><wbr>Implementing <wbr>Unigram</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter6/7" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter6/7"); } </script> <iframe name="__privateStripeMetricsController6490" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter6%2F7%3Ffw%3Dpt&amp;title=Unigram%20tokenization%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:23.912Z
Building a tokenizer, block by block - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter6/8?fw=pt
## [](#building-a-tokenizer-block-by-block)Building a tokenizer, block by block [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-6-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section8.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section8.ipynb) As we’ve seen in the previous sections, tokenization comprises several steps: - Normalization (any cleanup of the text that is deemed necessary, such as removing spaces or accents, Unicode normalization, etc.) - Pre-tokenization (splitting the input into words) - Running the input through the model (using the pre-tokenized words to produce a sequence of tokens) - Post-processing (adding the special tokens of the tokenizer, generating the attention mask and token type IDs) As a reminder, here’s another look at the overall process: ![The tokenization pipeline.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/tokenization_pipeline.svg) ![The tokenization pipeline.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/tokenization_pipeline-dark.svg) The 🤗 Tokenizers library has been built to provide several options for each of those steps, which you can mix and match together. In this section we’ll see how we can build a tokenizer from scratch, as opposed to training a new tokenizer from an old one as we did in [section 2](/course/chapter6/2). You’ll then be able to build any kind of tokenizer you can think of! More precisely, the library is built around a central `Tokenizer` class with the building blocks regrouped in submodules: - `normalizers` contains all the possible types of `Normalizer` you can use (complete list [here](https://huggingface.co/docs/tokenizers/python/latest/api/reference.html#module-tokenizers.normalizers)). - `pre_tokenizers` contains all the possible types of `PreTokenizer` you can use (complete list [here](https://huggingface.co/docs/tokenizers/python/latest/api/reference.html#module-tokenizers.pre_tokenizers)). - `models` contains the various types of `Model` you can use, like `BPE`, `WordPiece`, and `Unigram` (complete list [here](https://huggingface.co/docs/tokenizers/python/latest/api/reference.html#module-tokenizers.models)). - `trainers` contains all the different types of `Trainer` you can use to train your model on a corpus (one per type of model; complete list [here](https://huggingface.co/docs/tokenizers/python/latest/api/reference.html#module-tokenizers.trainers)). - `post_processors` contains the various types of `PostProcessor` you can use (complete list [here](https://huggingface.co/docs/tokenizers/python/latest/api/reference.html#module-tokenizers.processors)). - `decoders` contains the various types of `Decoder` you can use to decode the outputs of tokenization (complete list [here](https://huggingface.co/docs/tokenizers/python/latest/components.html#decoders)). You can find the whole list of building blocks [here](https://huggingface.co/docs/tokenizers/python/latest/components.html). ## [](#acquiring-a-corpus)Acquiring a corpus To train our new tokenizer, we will use a small corpus of text (so the examples run fast). The steps for acquiring the corpus are similar to the ones we took at the [beginning of this chapter](/course/chapter6/2), but this time we’ll use the [WikiText-2](https://huggingface.co/datasets/wikitext) dataset: ``` from datasets import load_dataset dataset = load_dataset("wikitext", name="wikitext-2-raw-v1", split="train") def get_training_corpus(): for i in range(0, len(dataset), 1000): yield dataset[i : i + 1000]["text"]``` The function `get_training_corpus()` is a generator that will yield batches of 1,000 texts, which we will use to train the tokenizer. 🤗 Tokenizers can also be trained on text files directly. Here’s how we can generate a text file containing all the texts/inputs from WikiText-2 that we can use locally: ``` with open("wikitext-2.txt", "w", encoding="utf-8") as f: for i in range(len(dataset)): f.write(dataset[i]["text"] + "\n")``` Next we’ll show you how to build your own BERT, GPT-2, and XLNet tokenizers, block by block. That will give us an example of each of the three main tokenization algorithms: WordPiece, BPE, and Unigram. Let’s start with BERT! ## [](#building-a-wordpiece-tokenizer-from-scratch)Building a WordPiece tokenizer from scratch To build a tokenizer with the 🤗 Tokenizers library, we start by instantiating a `Tokenizer` object with a `model`, then set its `normalizer`, `pre_tokenizer`, `post_processor`, and `decoder` attributes to the values we want. For this example, we’ll create a `Tokenizer` with a WordPiece model: ``` from tokenizers import ( decoders, models, normalizers, pre_tokenizers, processors, trainers, Tokenizer, ) tokenizer = Tokenizer(models.WordPiece(unk_token="[UNK]"))``` We have to specify the `unk_token` so the model knows what to return when it encounters characters it hasn’t seen before. Other arguments we can set here include the `vocab` of our model (we’re going to train the model, so we don’t need to set this) and `max_input_chars_per_word`, which specifies a maximum length for each word (words longer than the value passed will be split). The first step of tokenization is normalization, so let’s begin with that. Since BERT is widely used, there is a `BertNormalizer` with the classic options we can set for BERT: `lowercase` and `strip_accents`, which are self-explanatory; `clean_text` to remove all control characters and replace repeating spaces with a single one; and `handle_chinese_chars`, which places spaces around Chinese characters. To replicate the `bert-base-uncased` tokenizer, we can just set this normalizer: ``` tokenizer.normalizer = normalizers.BertNormalizer(lowercase=True)``` Generally speaking, however, when building a new tokenizer you won’t have access to such a handy normalizer already implemented in the 🤗 Tokenizers library — so let’s see how to create the BERT normalizer by hand. The library provides a `Lowercase` normalizer and a `StripAccents` normalizer, and you can compose several normalizers using a `Sequence`: ``` tokenizer.normalizer = normalizers.Sequence( [normalizers.NFD(), normalizers.Lowercase(), normalizers.StripAccents()] )``` We’re also using an `NFD` Unicode normalizer, as otherwise the `StripAccents` normalizer won’t properly recognize the accented characters and thus won’t strip them out. As we’ve seen before, we can use the `normalize_str()` method of the `normalizer` to check out the effects it has on a given text: ``` print(tokenizer.normalizer.normalize_str("Héllò hôw are ü?"))``` **To go further** If you test the two versions of the previous normalizers on a string containing the unicode character `u"\u0085"` you will surely notice that these two normalizers are not exactly equivalent. To not over-complicate the version with `normalizers.Sequence` too much , we haven’t included the Regex replacements that the `BertNormalizer` requires when the `clean_text` argument is set to `True` - which is the default behavior. But don’t worry: it is possible to get exactly the same normalization without using the handy `BertNormalizer` by adding two `normalizers.Replace`’s to the normalizers sequence. Next is the pre-tokenization step. Again, there is a prebuilt `BertPreTokenizer` that we can use: ``` tokenizer.pre_tokenizer = pre_tokenizers.BertPreTokenizer()``` Or we can build it from scratch: ``` tokenizer.pre_tokenizer = pre_tokenizers.Whitespace()``` Note that the `Whitespace` pre-tokenizer splits on whitespace and all characters that are not letters, digits, or the underscore character, so it technically splits on whitespace and punctuation: ``` tokenizer.pre_tokenizer.pre_tokenize_str("Let's test my pre-tokenizer.")``` ``` [('Let', (0, 3)), ("'", (3, 4)), ('s', (4, 5)), ('test', (6, 10)), ('my', (11, 13)), ('pre', (14, 17)), ('-', (17, 18)), ('tokenizer', (18, 27)), ('.', (27, 28))]``` If you only want to split on whitespace, you should use the `WhitespaceSplit` pre-tokenizer instead: ``` pre_tokenizer = pre_tokenizers.WhitespaceSplit() pre_tokenizer.pre_tokenize_str("Let's test my pre-tokenizer.")``` ``` [("Let's", (0, 5)), ('test', (6, 10)), ('my', (11, 13)), ('pre-tokenizer.', (14, 28))]``` Like with normalizers, you can use a `Sequence` to compose several pre-tokenizers: ``` pre_tokenizer = pre_tokenizers.Sequence( [pre_tokenizers.WhitespaceSplit(), pre_tokenizers.Punctuation()] ) pre_tokenizer.pre_tokenize_str("Let's test my pre-tokenizer.")``` ``` [('Let', (0, 3)), ("'", (3, 4)), ('s', (4, 5)), ('test', (6, 10)), ('my', (11, 13)), ('pre', (14, 17)), ('-', (17, 18)), ('tokenizer', (18, 27)), ('.', (27, 28))]``` The next step in the tokenization pipeline is running the inputs through the model. We already specified our model in the initialization, but we still need to train it, which will require a `WordPieceTrainer`. The main thing to remember when instantiating a trainer in 🤗 Tokenizers is that you need to pass it all the special tokens you intend to use — otherwise it won’t add them to the vocabulary, since they are not in the training corpus: ``` special_tokens = ["[UNK]", "[PAD]", "[CLS]", "[SEP]", "[MASK]"] trainer = trainers.WordPieceTrainer(vocab_size=25000, special_tokens=special_tokens)``` As well as specifying the `vocab_size` and `special_tokens`, we can set the `min_frequency` (the number of times a token must appear to be included in the vocabulary) or change the `continuing_subword_prefix` (if we want to use something different from `##`). To train our model using the iterator we defined earlier, we just have to execute this command: ``` tokenizer.train_from_iterator(get_training_corpus(), trainer=trainer)``` We can also use text files to train our tokenizer, which would look like this (we reinitialize the model with an empty `WordPiece` beforehand): ``` tokenizer.model = models.WordPiece(unk_token="[UNK]") tokenizer.train(["wikitext-2.txt"], trainer=trainer)``` In both cases, we can then test the tokenizer on a text by calling the `encode()` method: ``` encoding = tokenizer.encode("Let's test this tokenizer.") print(encoding.tokens)``` ``` ['let', "'", 's', 'test', 'this', 'tok', '##eni', '##zer', '.']``` The `encoding` obtained is an `Encoding`, which contains all the necessary outputs of the tokenizer in its various attributes: `ids`, `type_ids`, `tokens`, `offsets`, `attention_mask`, `special_tokens_mask`, and `overflowing`. The last step in the tokenization pipeline is post-processing. We need to add the `[CLS]` token at the beginning and the `[SEP]` token at the end (or after each sentence, if we have a pair of sentences). We will use a `TemplateProcessor` for this, but first we need to know the IDs of the `[CLS]` and `[SEP]` tokens in the vocabulary: ``` cls_token_id = tokenizer.token_to_id("[CLS]") sep_token_id = tokenizer.token_to_id("[SEP]") print(cls_token_id, sep_token_id)``` To write the template for the `TemplateProcessor`, we have to specify how to treat a single sentence and a pair of sentences. For both, we write the special tokens we want to use; the first (or single) sentence is represented by `$A`, while the second sentence (if encoding a pair) is represented by `$B`. For each of these (special tokens and sentences), we also specify the corresponding token type ID after a colon. The classic BERT template is thus defined as follows: ``` tokenizer.post_processor = processors.TemplateProcessing( single=f"[CLS]:0 $A:0 [SEP]:0", pair=f"[CLS]:0 $A:0 [SEP]:0 $B:1 [SEP]:1", special_tokens=[("[CLS]", cls_token_id), ("[SEP]", sep_token_id)], )``` Note that we need to pass along the IDs of the special tokens, so the tokenizer can properly convert them to their IDs. Once this is added, going back to our previous example will give: ``` encoding = tokenizer.encode("Let's test this tokenizer.") print(encoding.tokens)``` ``` ['[CLS]', 'let', "'", 's', 'test', 'this', 'tok', '##eni', '##zer', '.', '[SEP]']``` And on a pair of sentences, we get the proper result: ``` encoding = tokenizer.encode("Let's test this tokenizer...", "on a pair of sentences.") print(encoding.tokens) print(encoding.type_ids)``` ``` ['[CLS]', 'let', "'", 's', 'test', 'this', 'tok', '##eni', '##zer', '...', '[SEP]', 'on', 'a', 'pair', 'of', 'sentences', '.', '[SEP]'] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]``` We’ve almost finished building this tokenizer from scratch — the last step is to include a decoder: ``` tokenizer.decoder = decoders.WordPiece(prefix="##")``` Let’s test it on our previous `encoding`: ``` tokenizer.decode(encoding.ids)``` ``` "let's test this tokenizer... on a pair of sentences."``` Great! We can save our tokenizer in a single JSON file like this: ``` tokenizer.save("tokenizer.json")``` We can then reload that file in a `Tokenizer` object with the `from_file()` method: ``` new_tokenizer = Tokenizer.from_file("tokenizer.json")``` To use this tokenizer in 🤗 Transformers, we have to wrap it in a `PreTrainedTokenizerFast`. We can either use the generic class or, if our tokenizer corresponds to an existing model, use that class (here, `BertTokenizerFast`). If you apply this lesson to build a brand new tokenizer, you will have to use the first option. To wrap the tokenizer in a `PreTrainedTokenizerFast`, we can either pass the tokenizer we built as a `tokenizer_object` or pass the tokenizer file we saved as `tokenizer_file`. The key thing to remember is that we have to manually set all the special tokens, since that class can’t infer from the `tokenizer` object which token is the mask token, the `[CLS]` token, etc.: ``` from transformers import PreTrainedTokenizerFast wrapped_tokenizer = PreTrainedTokenizerFast( tokenizer_object=tokenizer, unk_token="[UNK]", pad_token="[PAD]", cls_token="[CLS]", sep_token="[SEP]", mask_token="[MASK]", )``` If you are using a specific tokenizer class (like `BertTokenizerFast`), you will only need to specify the special tokens that are different from the default ones (here, none): ``` from transformers import BertTokenizerFast wrapped_tokenizer = BertTokenizerFast(tokenizer_object=tokenizer)``` You can then use this tokenizer like any other 🤗 Transformers tokenizer. You can save it with the `save_pretrained()` method, or upload it to the Hub with the `push_to_hub()` method. Now that we’ve seen how to build a WordPiece tokenizer, let’s do the same for a BPE tokenizer. We’ll go a bit faster since you know all the steps, and only highlight the differences. ## [](#building-a-bpe-tokenizer-from-scratch)Building a BPE tokenizer from scratch Let’s now build a GPT-2 tokenizer. Like for the BERT tokenizer, we start by initializing a `Tokenizer` with a BPE model: ``` tokenizer = Tokenizer(models.BPE())``` Also like for BERT, we could initialize this model with a vocabulary if we had one (we would need to pass the `vocab` and `merges` in this case), but since we will train from scratch, we don’t need to do that. We also don’t need to specify an `unk_token` because GPT-2 uses byte-level BPE, which doesn’t require it. GPT-2 does not use a normalizer, so we skip that step and go directly to the pre-tokenization: ``` tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=False)``` The option we added to `ByteLevel` here is to not add a space at the beginning of a sentence (which is the default otherwise). We can have a look at the pre-tokenization of an example text like before: ``` tokenizer.pre_tokenizer.pre_tokenize_str("Let's test pre-tokenization!")``` ``` [('Let', (0, 3)), ("'s", (3, 5)), ('Ġtest', (5, 10)), ('Ġpre', (10, 14)), ('-', (14, 15)), ('tokenization', (15, 27)), ('!', (27, 28))]``` Next is the model, which needs training. For GPT-2, the only special token is the end-of-text token: ``` trainer = trainers.BpeTrainer(vocab_size=25000, special_tokens=["<|endoftext|>"]) tokenizer.train_from_iterator(get_training_corpus(), trainer=trainer)``` Like with the `WordPieceTrainer`, as well as the `vocab_size` and `special_tokens`, we can specify the `min_frequency` if we want to, or if we have an end-of-word suffix (like `</w>`), we can set it with `end_of_word_suffix`. This tokenizer can also be trained on text files: ``` tokenizer.model = models.BPE() tokenizer.train(["wikitext-2.txt"], trainer=trainer)``` Let’s have a look at the tokenization of a sample text: ``` encoding = tokenizer.encode("Let's test this tokenizer.") print(encoding.tokens)``` ``` ['L', 'et', "'", 's', 'Ġtest', 'Ġthis', 'Ġto', 'ken', 'izer', '.']``` We apply the byte-level post-processing for the GPT-2 tokenizer as follows: ``` tokenizer.post_processor = processors.ByteLevel(trim_offsets=False)``` The `trim_offsets = False` option indicates to the post-processor that we should leave the offsets of tokens that begin with ‘Ġ’ as they are: this way the start of the offsets will point to the space before the word, not the first character of the word (since the space is technically part of the token). Let’s have a look at the result with the text we just encoded, where `'Ġtest'` is the token at index 4: ``` sentence = "Let's test this tokenizer." encoding = tokenizer.encode(sentence) start, end = encoding.offsets[4] sentence[start:end]``` Finally, we add a byte-level decoder: ``` tokenizer.decoder = decoders.ByteLevel()``` and we can double-check it works properly: ``` tokenizer.decode(encoding.ids)``` ``` "Let's test this tokenizer."``` Great! Now that we’re done, we can save the tokenizer like before, and wrap it in a `PreTrainedTokenizerFast` or `GPT2TokenizerFast` if we want to use it in 🤗 Transformers: ``` from transformers import PreTrainedTokenizerFast wrapped_tokenizer = PreTrainedTokenizerFast( tokenizer_object=tokenizer, bos_token="<|endoftext|>", eos_token="<|endoftext|>", )``` or: ``` from transformers import GPT2TokenizerFast wrapped_tokenizer = GPT2TokenizerFast(tokenizer_object=tokenizer)``` As the last example, we’ll show you how to build a Unigram tokenizer from scratch. ## [](#building-a-unigram-tokenizer-from-scratch)Building a Unigram tokenizer from scratch Let’s now build an XLNet tokenizer. Like for the previous tokenizers, we start by initializing a `Tokenizer` with a Unigram model: ``` tokenizer = Tokenizer(models.Unigram())``` Again, we could initialize this model with a vocabulary if we had one. For the normalization, XLNet uses a few replacements (which come from SentencePiece): ``` from tokenizers import Regex tokenizer.normalizer = normalizers.Sequence( [ normalizers.Replace("``", '"'), normalizers.Replace("''", '"'), normalizers.NFKD(), normalizers.StripAccents(), normalizers.Replace(Regex(" {2,}"), " "), ] )``` This replaces `“` and `”` with `”` and any sequence of two or more spaces with a single space, as well as removing the accents in the texts to tokenize. The pre-tokenizer to use for any SentencePiece tokenizer is `Metaspace`: ``` tokenizer.pre_tokenizer = pre_tokenizers.Metaspace()``` We can have a look at the pre-tokenization of an example text like before: ``` tokenizer.pre_tokenizer.pre_tokenize_str("Let's test the pre-tokenizer!")``` ``` [("▁Let's", (0, 5)), ('▁test', (5, 10)), ('▁the', (10, 14)), ('▁pre-tokenizer!', (14, 29))]``` Next is the model, which needs training. XLNet has quite a few special tokens: ``` special_tokens = ["<cls>", "<sep>", "<unk>", "<pad>", "<mask>", "<s>", "</s>"] trainer = trainers.UnigramTrainer( vocab_size=25000, special_tokens=special_tokens, unk_token="<unk>" ) tokenizer.train_from_iterator(get_training_corpus(), trainer=trainer)``` A very important argument not to forget for the `UnigramTrainer` is the `unk_token`. We can also pass along other arguments specific to the Unigram algorithm, such as the `shrinking_factor` for each step where we remove tokens (defaults to 0.75) or the `max_piece_length` to specify the maximum length of a given token (defaults to 16). This tokenizer can also be trained on text files: ``` tokenizer.model = models.Unigram() tokenizer.train(["wikitext-2.txt"], trainer=trainer)``` Let’s have a look at the tokenization of a sample text: ``` encoding = tokenizer.encode("Let's test this tokenizer.") print(encoding.tokens)``` ``` ['▁Let', "'", 's', '▁test', '▁this', '▁to', 'ken', 'izer', '.']``` A peculiarity of XLNet is that it puts the `<cls>` token at the end of the sentence, with a type ID of 2 (to distinguish it from the other tokens). It’s padding on the left, as a result. We can deal with all the special tokens and token type IDs with a template, like for BERT, but first we have to get the IDs of the `<cls>` and `<sep>` tokens: ``` cls_token_id = tokenizer.token_to_id("<cls>") sep_token_id = tokenizer.token_to_id("<sep>") print(cls_token_id, sep_token_id)``` The template looks like this: ``` tokenizer.post_processor = processors.TemplateProcessing( single="$A:0 <sep>:0 <cls>:2", pair="$A:0 <sep>:0 $B:1 <sep>:1 <cls>:2", special_tokens=[("<sep>", sep_token_id), ("<cls>", cls_token_id)], )``` And we can test it works by encoding a pair of sentences: ``` encoding = tokenizer.encode("Let's test this tokenizer...", "on a pair of sentences!") print(encoding.tokens) print(encoding.type_ids)``` ``` ['▁Let', "'", 's', '▁test', '▁this', '▁to', 'ken', 'izer', '.', '.', '.', '<sep>', '▁', 'on', '▁', 'a', '▁pair', '▁of', '▁sentence', 's', '!', '<sep>', '<cls>'] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2]``` Finally, we add a `Metaspace` decoder: ``` tokenizer.decoder = decoders.Metaspace()``` and we’re done with this tokenizer! We can save the tokenizer like before, and wrap it in a `PreTrainedTokenizerFast` or `XLNetTokenizerFast` if we want to use it in 🤗 Transformers. One thing to note when using `PreTrainedTokenizerFast` is that on top of the special tokens, we need to tell the 🤗 Transformers library to pad on the left: ``` from transformers import PreTrainedTokenizerFast wrapped_tokenizer = PreTrainedTokenizerFast( tokenizer_object=tokenizer, bos_token="<s>", eos_token="</s>", unk_token="<unk>", pad_token="<pad>", cls_token="<cls>", sep_token="<sep>", mask_token="<mask>", padding_side="left", )``` Or alternatively: ``` from transformers import XLNetTokenizerFast wrapped_tokenizer = XLNetTokenizerFast(tokenizer_object=tokenizer)``` Now that you have seen how the various building blocks are used to build existing tokenizers, you should be able to write any tokenizer you want with the 🤗 Tokenizers library and be able to use it in 🤗 Transformers.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Building a tokenizer, block by block - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter6/8"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Building a tokenizer, block by block - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;building-a-tokenizer-block-by-block&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;acquiring-a-corpus&quot;,&quot;title&quot;:&quot;Acquiring a corpus&quot;},{&quot;local&quot;:&quot;building-a-wordpiece-tokenizer-from-scratch&quot;,&quot;title&quot;:&quot;Building a WordPiece tokenizer from scratch&quot;},{&quot;local&quot;:&quot;building-a-bpe-tokenizer-from-scratch&quot;,&quot;title&quot;:&quot;Building a BPE tokenizer from scratch&quot;},{&quot;local&quot;:&quot;building-a-unigram-tokenizer-from-scratch&quot;,&quot;title&quot;:&quot;Building a Unigram tokenizer from scratch&quot;}],&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter6/8&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Building a tokenizer, block by block</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/2?fw=pt">Training a new tokenizer from an old one </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3?fw=pt">Fast tokenizers' special powers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3b?fw=pt">Fast tokenizers in the QA pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/4?fw=pt">Normalization and pre-tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/5?fw=pt">Byte-Pair Encoding tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/6?fw=pt">WordPiece tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/7?fw=pt">Unigram tokenization </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter6/8?fw=pt">Building a tokenizer, block by block </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/9?fw=pt">Tokenizers, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="building-a-tokenizer-block-by-block" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#building-a-tokenizer-block-by-block"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Building a tokenizer, block by block</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-6-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter6/section8.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter6/section8.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>As we’ve seen in the previous sections, tokenization comprises several steps:</p> <ul><li>Normalization (any cleanup of the text that is deemed necessary, such as removing spaces or accents, Unicode normalization, etc.)</li> <li>Pre-tokenization (splitting the input into words)</li> <li>Running the input through the model (using the pre-tokenized words to produce a sequence of tokens)</li> <li>Post-processing (adding the special tokens of the tokenizer, generating the attention mask and token type IDs)</li></ul> <p>As a reminder, here’s another look at the overall process:</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/tokenization_pipeline.svg" alt="The tokenization pipeline."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter6/tokenization_pipeline-dark.svg" alt="The tokenization pipeline."></div> <p>The 🤗 Tokenizers library has been built to provide several options for each of those steps, which you can mix and match together. In this section we’ll see how we can build a tokenizer from scratch, as opposed to training a new tokenizer from an old one as we did in <a href="/course/chapter6/2">section 2</a>. You’ll then be able to build any kind of tokenizer you can think of!</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/MR8tZm5ViWU" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>More precisely, the library is built around a central <code>Tokenizer</code> class with the building blocks regrouped in submodules:</p> <ul><li><code>normalizers</code> contains all the possible types of <code>Normalizer</code> you can use (complete list <a href="https://huggingface.co/docs/tokenizers/python/latest/api/reference.html#module-tokenizers.normalizers" rel="nofollow">here</a>).</li> <li><code>pre_tokenizers</code> contains all the possible types of <code>PreTokenizer</code> you can use (complete list <a href="https://huggingface.co/docs/tokenizers/python/latest/api/reference.html#module-tokenizers.pre_tokenizers" rel="nofollow">here</a>).</li> <li><code>models</code> contains the various types of <code>Model</code> you can use, like <code>BPE</code>, <code>WordPiece</code>, and <code>Unigram</code> (complete list <a href="https://huggingface.co/docs/tokenizers/python/latest/api/reference.html#module-tokenizers.models" rel="nofollow">here</a>).</li> <li><code>trainers</code> contains all the different types of <code>Trainer</code> you can use to train your model on a corpus (one per type of model; complete list <a href="https://huggingface.co/docs/tokenizers/python/latest/api/reference.html#module-tokenizers.trainers" rel="nofollow">here</a>).</li> <li><code>post_processors</code> contains the various types of <code>PostProcessor</code> you can use (complete list <a href="https://huggingface.co/docs/tokenizers/python/latest/api/reference.html#module-tokenizers.processors" rel="nofollow">here</a>).</li> <li><code>decoders</code> contains the various types of <code>Decoder</code> you can use to decode the outputs of tokenization (complete list <a href="https://huggingface.co/docs/tokenizers/python/latest/components.html#decoders" rel="nofollow">here</a>).</li></ul> <p>You can find the whole list of building blocks <a href="https://huggingface.co/docs/tokenizers/python/latest/components.html" rel="nofollow">here</a>.</p> <h2 class="relative group"><a id="acquiring-a-corpus" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#acquiring-a-corpus"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Acquiring a corpus</span></h2> <p>To train our new tokenizer, we will use a small corpus of text (so the examples run fast). The steps for acquiring the corpus are similar to the ones we took at the <a href="/course/chapter6/2">beginning of this chapter</a>, but this time we’ll use the <a href="https://huggingface.co/datasets/wikitext" rel="nofollow">WikiText-2</a> dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset dataset = load_dataset(<span class="hljs-string">"wikitext"</span>, name=<span class="hljs-string">"wikitext-2-raw-v1"</span>, split=<span class="hljs-string">"train"</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">get_training_corpus</span>(): <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, <span class="hljs-built_in">len</span>(dataset), <span class="hljs-number">1000</span>): <span class="hljs-keyword">yield</span> dataset[i : i + <span class="hljs-number">1000</span>][<span class="hljs-string">"text"</span>]</pre></div> <p>The function <code>get_training_corpus()</code> is a generator that will yield batches of 1,000 texts, which we will use to train the tokenizer.</p> <p>🤗 Tokenizers can also be trained on text files directly. Here’s how we can generate a text file containing all the texts/inputs from WikiText-2 that we can use locally:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(<span class="hljs-string">"wikitext-2.txt"</span>, <span class="hljs-string">"w"</span>, encoding=<span class="hljs-string">"utf-8"</span>) <span class="hljs-keyword">as</span> f: <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(dataset)): f.write(dataset[i][<span class="hljs-string">"text"</span>] + <span class="hljs-string">"\n"</span>)</pre></div> <p>Next we’ll show you how to build your own BERT, GPT-2, and XLNet tokenizers, block by block. That will give us an example of each of the three main tokenization algorithms: WordPiece, BPE, and Unigram. Let’s start with BERT!</p> <h2 class="relative group"><a id="building-a-wordpiece-tokenizer-from-scratch" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#building-a-wordpiece-tokenizer-from-scratch"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Building a WordPiece tokenizer from scratch</span></h2> <p>To build a tokenizer with the 🤗 Tokenizers library, we start by instantiating a <code>Tokenizer</code> object with a <code>model</code>, then set its <code>normalizer</code>, <code>pre_tokenizer</code>, <code>post_processor</code>, and <code>decoder</code> attributes to the values we want.</p> <p>For this example, we’ll create a <code>Tokenizer</code> with a WordPiece model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> tokenizers <span class="hljs-keyword">import</span> ( decoders, models, normalizers, pre_tokenizers, processors, trainers, Tokenizer, ) tokenizer = Tokenizer(models.WordPiece(unk_token=<span class="hljs-string">"[UNK]"</span>))</pre></div> <p>We have to specify the <code>unk_token</code> so the model knows what to return when it encounters characters it hasn’t seen before. Other arguments we can set here include the <code>vocab</code> of our model (we’re going to train the model, so we don’t need to set this) and <code>max_input_chars_per_word</code>, which specifies a maximum length for each word (words longer than the value passed will be split).</p> <p>The first step of tokenization is normalization, so let’s begin with that. Since BERT is widely used, there is a <code>BertNormalizer</code> with the classic options we can set for BERT: <code>lowercase</code> and <code>strip_accents</code>, which are self-explanatory; <code>clean_text</code> to remove all control characters and replace repeating spaces with a single one; and <code>handle_chinese_chars</code>, which places spaces around Chinese characters. To replicate the <code>bert-base-uncased</code> tokenizer, we can just set this normalizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.normalizer = normalizers.BertNormalizer(lowercase=<span class="hljs-literal">True</span>)</pre></div> <p>Generally speaking, however, when building a new tokenizer you won’t have access to such a handy normalizer already implemented in the 🤗 Tokenizers library — so let’s see how to create the BERT normalizer by hand. The library provides a <code>Lowercase</code> normalizer and a <code>StripAccents</code> normalizer, and you can compose several normalizers using a <code>Sequence</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.normalizer = normalizers.<span class="hljs-type">Sequence</span>( [normalizers.NFD(), normalizers.Lowercase(), normalizers.StripAccents()] )</pre></div> <p>We’re also using an <code>NFD</code> Unicode normalizer, as otherwise the <code>StripAccents</code> normalizer won’t properly recognize the accented characters and thus won’t strip them out.</p> <p>As we’ve seen before, we can use the <code>normalize_str()</code> method of the <code>normalizer</code> to check out the effects it has on a given text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(tokenizer.normalizer.normalize_str(<span class="hljs-string">"Héllò hôw are ü?"</span>))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>hello how are u?</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p><strong>To go further</strong> If you test the two versions of the previous normalizers on a string containing the unicode character <code>u"\u0085"</code> you will surely notice that these two normalizers are not exactly equivalent. To not over-complicate the version with <code>normalizers.Sequence</code> too much , we haven’t included the Regex replacements that the <code>BertNormalizer</code> requires when the <code>clean_text</code> argument is set to <code>True</code> - which is the default behavior. But don’t worry: it is possible to get exactly the same normalization without using the handy <code>BertNormalizer</code> by adding two <code>normalizers.Replace</code>’s to the normalizers sequence.</p></div> <p>Next is the pre-tokenization step. Again, there is a prebuilt <code>BertPreTokenizer</code> that we can use:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.pre_tokenizer = pre_tokenizers.BertPreTokenizer()</pre></div> <p>Or we can build it from scratch:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.pre_tokenizer = pre_tokenizers.Whitespace()</pre></div> <p>Note that the <code>Whitespace</code> pre-tokenizer splits on whitespace and all characters that are not letters, digits, or the underscore character, so it technically splits on whitespace and punctuation:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.pre_tokenizer.pre_tokenize_str(<span class="hljs-string">"Let's test my pre-tokenizer."</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[(<span class="hljs-string">'Let'</span>, (<span class="hljs-number">0</span>, <span class="hljs-number">3</span>)), (<span class="hljs-string">"'"</span>, (<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)), (<span class="hljs-string">'s'</span>, (<span class="hljs-number">4</span>, <span class="hljs-number">5</span>)), (<span class="hljs-string">'test'</span>, (<span class="hljs-number">6</span>, <span class="hljs-number">10</span>)), (<span class="hljs-string">'my'</span>, (<span class="hljs-number">11</span>, <span class="hljs-number">13</span>)), (<span class="hljs-string">'pre'</span>, (<span class="hljs-number">14</span>, <span class="hljs-number">17</span>)), (<span class="hljs-string">'-'</span>, (<span class="hljs-number">17</span>, <span class="hljs-number">18</span>)), (<span class="hljs-string">'tokenizer'</span>, (<span class="hljs-number">18</span>, <span class="hljs-number">27</span>)), (<span class="hljs-string">'.'</span>, (<span class="hljs-number">27</span>, <span class="hljs-number">28</span>))]</pre></div> <p>If you only want to split on whitespace, you should use the <code>WhitespaceSplit</code> pre-tokenizer instead:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pre_tokenizer = pre_tokenizers.WhitespaceSplit() pre_tokenizer.pre_tokenize_str(<span class="hljs-string">"Let's test my pre-tokenizer."</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[(<span class="hljs-string">"Let's"</span>, (<span class="hljs-number">0</span>, <span class="hljs-number">5</span>)), (<span class="hljs-string">'test'</span>, (<span class="hljs-number">6</span>, <span class="hljs-number">10</span>)), (<span class="hljs-string">'my'</span>, (<span class="hljs-number">11</span>, <span class="hljs-number">13</span>)), (<span class="hljs-string">'pre-tokenizer.'</span>, (<span class="hljs-number">14</span>, <span class="hljs-number">28</span>))]</pre></div> <p>Like with normalizers, you can use a <code>Sequence</code> to compose several pre-tokenizers:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pre_tokenizer = pre_tokenizers.<span class="hljs-type">Sequence</span>( [pre_tokenizers.WhitespaceSplit(), pre_tokenizers.Punctuation()] ) pre_tokenizer.pre_tokenize_str(<span class="hljs-string">"Let's test my pre-tokenizer."</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[(<span class="hljs-string">'Let'</span>, (<span class="hljs-number">0</span>, <span class="hljs-number">3</span>)), (<span class="hljs-string">"'"</span>, (<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)), (<span class="hljs-string">'s'</span>, (<span class="hljs-number">4</span>, <span class="hljs-number">5</span>)), (<span class="hljs-string">'test'</span>, (<span class="hljs-number">6</span>, <span class="hljs-number">10</span>)), (<span class="hljs-string">'my'</span>, (<span class="hljs-number">11</span>, <span class="hljs-number">13</span>)), (<span class="hljs-string">'pre'</span>, (<span class="hljs-number">14</span>, <span class="hljs-number">17</span>)), (<span class="hljs-string">'-'</span>, (<span class="hljs-number">17</span>, <span class="hljs-number">18</span>)), (<span class="hljs-string">'tokenizer'</span>, (<span class="hljs-number">18</span>, <span class="hljs-number">27</span>)), (<span class="hljs-string">'.'</span>, (<span class="hljs-number">27</span>, <span class="hljs-number">28</span>))]</pre></div> <p>The next step in the tokenization pipeline is running the inputs through the model. We already specified our model in the initialization, but we still need to train it, which will require a <code>WordPieceTrainer</code>. The main thing to remember when instantiating a trainer in 🤗 Tokenizers is that you need to pass it all the special tokens you intend to use — otherwise it won’t add them to the vocabulary, since they are not in the training corpus:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>special_tokens = [<span class="hljs-string">"[UNK]"</span>, <span class="hljs-string">"[PAD]"</span>, <span class="hljs-string">"[CLS]"</span>, <span class="hljs-string">"[SEP]"</span>, <span class="hljs-string">"[MASK]"</span>] trainer = trainers.WordPieceTrainer(vocab_size=<span class="hljs-number">25000</span>, special_tokens=special_tokens)</pre></div> <p>As well as specifying the <code>vocab_size</code> and <code>special_tokens</code>, we can set the <code>min_frequency</code> (the number of times a token must appear to be included in the vocabulary) or change the <code>continuing_subword_prefix</code> (if we want to use something different from <code>##</code>).</p> <p>To train our model using the iterator we defined earlier, we just have to execute this command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.train_from_iterator(get_training_corpus(), trainer=trainer)</pre></div> <p>We can also use text files to train our tokenizer, which would look like this (we reinitialize the model with an empty <code>WordPiece</code> beforehand):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.model = models.WordPiece(unk_token=<span class="hljs-string">"[UNK]"</span>) tokenizer.train([<span class="hljs-string">"wikitext-2.txt"</span>], trainer=trainer)</pre></div> <p>In both cases, we can then test the tokenizer on a text by calling the <code>encode()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>encoding = tokenizer.encode(<span class="hljs-string">"Let's test this tokenizer."</span>) <span class="hljs-built_in">print</span>(encoding.tokens)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'let'</span>, <span class="hljs-string">"'"</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'test'</span>, <span class="hljs-string">'this'</span>, <span class="hljs-string">'tok'</span>, <span class="hljs-string">'##eni'</span>, <span class="hljs-string">'##zer'</span>, <span class="hljs-string">'.'</span>]</pre></div> <p>The <code>encoding</code> obtained is an <code>Encoding</code>, which contains all the necessary outputs of the tokenizer in its various attributes: <code>ids</code>, <code>type_ids</code>, <code>tokens</code>, <code>offsets</code>, <code>attention_mask</code>, <code>special_tokens_mask</code>, and <code>overflowing</code>.</p> <p>The last step in the tokenization pipeline is post-processing. We need to add the <code>[CLS]</code> token at the beginning and the <code>[SEP]</code> token at the end (or after each sentence, if we have a pair of sentences). We will use a <code>TemplateProcessor</code> for this, but first we need to know the IDs of the <code>[CLS]</code> and <code>[SEP]</code> tokens in the vocabulary:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>cls_token_id = tokenizer.token_to_id(<span class="hljs-string">"[CLS]"</span>) sep_token_id = tokenizer.token_to_id(<span class="hljs-string">"[SEP]"</span>) <span class="hljs-built_in">print</span>(cls_token_id, sep_token_id)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>)</pre></div> <p>To write the template for the <code>TemplateProcessor</code>, we have to specify how to treat a single sentence and a pair of sentences. For both, we write the special tokens we want to use; the first (or single) sentence is represented by <code>$A</code>, while the second sentence (if encoding a pair) is represented by <code>$B</code>. For each of these (special tokens and sentences), we also specify the corresponding token type ID after a colon.</p> <p>The classic BERT template is thus defined as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.post_processor = processors.TemplateProcessing( single=<span class="hljs-string">f"[CLS]:0 $A:0 [SEP]:0"</span>, pair=<span class="hljs-string">f"[CLS]:0 $A:0 [SEP]:0 $B:1 [SEP]:1"</span>, special_tokens=[(<span class="hljs-string">"[CLS]"</span>, cls_token_id), (<span class="hljs-string">"[SEP]"</span>, sep_token_id)], )</pre></div> <p>Note that we need to pass along the IDs of the special tokens, so the tokenizer can properly convert them to their IDs.</p> <p>Once this is added, going back to our previous example will give:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>encoding = tokenizer.encode(<span class="hljs-string">"Let's test this tokenizer."</span>) <span class="hljs-built_in">print</span>(encoding.tokens)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'[CLS]'</span>, <span class="hljs-string">'let'</span>, <span class="hljs-string">"'"</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'test'</span>, <span class="hljs-string">'this'</span>, <span class="hljs-string">'tok'</span>, <span class="hljs-string">'##eni'</span>, <span class="hljs-string">'##zer'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'[SEP]'</span>]</pre></div> <p>And on a pair of sentences, we get the proper result:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>encoding = tokenizer.encode(<span class="hljs-string">"Let's test this tokenizer..."</span>, <span class="hljs-string">"on a pair of sentences."</span>) <span class="hljs-built_in">print</span>(encoding.tokens) <span class="hljs-built_in">print</span>(encoding.type_ids)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'[CLS]'</span>, <span class="hljs-string">'let'</span>, <span class="hljs-string">"'"</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'test'</span>, <span class="hljs-string">'this'</span>, <span class="hljs-string">'tok'</span>, <span class="hljs-string">'##eni'</span>, <span class="hljs-string">'##zer'</span>, <span class="hljs-string">'...'</span>, <span class="hljs-string">'[SEP]'</span>, <span class="hljs-string">'on'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'pair'</span>, <span class="hljs-string">'of'</span>, <span class="hljs-string">'sentences'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'[SEP]'</span>] [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]</pre></div> <p>We’ve almost finished building this tokenizer from scratch — the last step is to include a decoder:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.decoder = decoders.WordPiece(prefix=<span class="hljs-string">"##"</span>)</pre></div> <p>Let’s test it on our previous <code>encoding</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.decode(encoding.ids)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">"let's test this tokenizer... on a pair of sentences."</span></pre></div> <p>Great! We can save our tokenizer in a single JSON file like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.save(<span class="hljs-string">"tokenizer.json"</span>)</pre></div> <p>We can then reload that file in a <code>Tokenizer</code> object with the <code>from_file()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>new_tokenizer = Tokenizer.from_file(<span class="hljs-string">"tokenizer.json"</span>)</pre></div> <p>To use this tokenizer in 🤗 Transformers, we have to wrap it in a <code>PreTrainedTokenizerFast</code>. We can either use the generic class or, if our tokenizer corresponds to an existing model, use that class (here, <code>BertTokenizerFast</code>). If you apply this lesson to build a brand new tokenizer, you will have to use the first option.</p> <p>To wrap the tokenizer in a <code>PreTrainedTokenizerFast</code>, we can either pass the tokenizer we built as a <code>tokenizer_object</code> or pass the tokenizer file we saved as <code>tokenizer_file</code>. The key thing to remember is that we have to manually set all the special tokens, since that class can’t infer from the <code>tokenizer</code> object which token is the mask token, the <code>[CLS]</code> token, etc.:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PreTrainedTokenizerFast wrapped_tokenizer = PreTrainedTokenizerFast( tokenizer_object=tokenizer, <span class="hljs-comment"># tokenizer_file="tokenizer.json", # You can load from the tokenizer file, alternatively</span> unk_token=<span class="hljs-string">"[UNK]"</span>, pad_token=<span class="hljs-string">"[PAD]"</span>, cls_token=<span class="hljs-string">"[CLS]"</span>, sep_token=<span class="hljs-string">"[SEP]"</span>, mask_token=<span class="hljs-string">"[MASK]"</span>, )</pre></div> <p>If you are using a specific tokenizer class (like <code>BertTokenizerFast</code>), you will only need to specify the special tokens that are different from the default ones (here, none):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> BertTokenizerFast wrapped_tokenizer = BertTokenizerFast(tokenizer_object=tokenizer)</pre></div> <p>You can then use this tokenizer like any other 🤗 Transformers tokenizer. You can save it with the <code>save_pretrained()</code> method, or upload it to the Hub with the <code>push_to_hub()</code> method.</p> <p>Now that we’ve seen how to build a WordPiece tokenizer, let’s do the same for a BPE tokenizer. We’ll go a bit faster since you know all the steps, and only highlight the differences.</p> <h2 class="relative group"><a id="building-a-bpe-tokenizer-from-scratch" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#building-a-bpe-tokenizer-from-scratch"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Building a BPE tokenizer from scratch</span></h2> <p>Let’s now build a GPT-2 tokenizer. Like for the BERT tokenizer, we start by initializing a <code>Tokenizer</code> with a BPE model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer = Tokenizer(models.BPE())</pre></div> <p>Also like for BERT, we could initialize this model with a vocabulary if we had one (we would need to pass the <code>vocab</code> and <code>merges</code> in this case), but since we will train from scratch, we don’t need to do that. We also don’t need to specify an <code>unk_token</code> because GPT-2 uses byte-level BPE, which doesn’t require it.</p> <p>GPT-2 does not use a normalizer, so we skip that step and go directly to the pre-tokenization:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=<span class="hljs-literal">False</span>)</pre></div> <p>The option we added to <code>ByteLevel</code> here is to not add a space at the beginning of a sentence (which is the default otherwise). We can have a look at the pre-tokenization of an example text like before:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.pre_tokenizer.pre_tokenize_str(<span class="hljs-string">"Let's test pre-tokenization!"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[(<span class="hljs-string">'Let'</span>, (<span class="hljs-number">0</span>, <span class="hljs-number">3</span>)), (<span class="hljs-string">"'s"</span>, (<span class="hljs-number">3</span>, <span class="hljs-number">5</span>)), (<span class="hljs-string">'Ġtest'</span>, (<span class="hljs-number">5</span>, <span class="hljs-number">10</span>)), (<span class="hljs-string">'Ġpre'</span>, (<span class="hljs-number">10</span>, <span class="hljs-number">14</span>)), (<span class="hljs-string">'-'</span>, (<span class="hljs-number">14</span>, <span class="hljs-number">15</span>)), (<span class="hljs-string">'tokenization'</span>, (<span class="hljs-number">15</span>, <span class="hljs-number">27</span>)), (<span class="hljs-string">'!'</span>, (<span class="hljs-number">27</span>, <span class="hljs-number">28</span>))]</pre></div> <p>Next is the model, which needs training. For GPT-2, the only special token is the end-of-text token:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer = trainers.BpeTrainer(vocab_size=<span class="hljs-number">25000</span>, special_tokens=[<span class="hljs-string">"&lt;|endoftext|&gt;"</span>]) tokenizer.train_from_iterator(get_training_corpus(), trainer=trainer)</pre></div> <p>Like with the <code>WordPieceTrainer</code>, as well as the <code>vocab_size</code> and <code>special_tokens</code>, we can specify the <code>min_frequency</code> if we want to, or if we have an end-of-word suffix (like <code>&lt;/w&gt;</code>), we can set it with <code>end_of_word_suffix</code>.</p> <p>This tokenizer can also be trained on text files:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.model = models.BPE() tokenizer.train([<span class="hljs-string">"wikitext-2.txt"</span>], trainer=trainer)</pre></div> <p>Let’s have a look at the tokenization of a sample text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>encoding = tokenizer.encode(<span class="hljs-string">"Let's test this tokenizer."</span>) <span class="hljs-built_in">print</span>(encoding.tokens)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'L'</span>, <span class="hljs-string">'et'</span>, <span class="hljs-string">"'"</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'Ġtest'</span>, <span class="hljs-string">'Ġthis'</span>, <span class="hljs-string">'Ġto'</span>, <span class="hljs-string">'ken'</span>, <span class="hljs-string">'izer'</span>, <span class="hljs-string">'.'</span>]</pre></div> <p>We apply the byte-level post-processing for the GPT-2 tokenizer as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.post_processor = processors.ByteLevel(trim_offsets=<span class="hljs-literal">False</span>)</pre></div> <p>The <code>trim_offsets = False</code> option indicates to the post-processor that we should leave the offsets of tokens that begin with ‘Ġ’ as they are: this way the start of the offsets will point to the space before the word, not the first character of the word (since the space is technically part of the token). Let’s have a look at the result with the text we just encoded, where <code>'Ġtest'</code> is the token at index 4:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sentence = <span class="hljs-string">"Let's test this tokenizer."</span> encoding = tokenizer.encode(sentence) start, end = encoding.offsets[<span class="hljs-number">4</span>] sentence[start:end]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">' test'</span></pre></div> <p>Finally, we add a byte-level decoder:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.decoder = decoders.ByteLevel()</pre></div> <p>and we can double-check it works properly:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.decode(encoding.ids)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">"Let's test this tokenizer."</span></pre></div> <p>Great! Now that we’re done, we can save the tokenizer like before, and wrap it in a <code>PreTrainedTokenizerFast</code> or <code>GPT2TokenizerFast</code> if we want to use it in 🤗 Transformers:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PreTrainedTokenizerFast wrapped_tokenizer = PreTrainedTokenizerFast( tokenizer_object=tokenizer, bos_token=<span class="hljs-string">"&lt;|endoftext|&gt;"</span>, eos_token=<span class="hljs-string">"&lt;|endoftext|&gt;"</span>, )</pre></div> <p>or:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> GPT2TokenizerFast wrapped_tokenizer = GPT2TokenizerFast(tokenizer_object=tokenizer)</pre></div> <p>As the last example, we’ll show you how to build a Unigram tokenizer from scratch.</p> <h2 class="relative group"><a id="building-a-unigram-tokenizer-from-scratch" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#building-a-unigram-tokenizer-from-scratch"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Building a Unigram tokenizer from scratch</span></h2> <p>Let’s now build an XLNet tokenizer. Like for the previous tokenizers, we start by initializing a <code>Tokenizer</code> with a Unigram model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer = Tokenizer(models.Unigram())</pre></div> <p>Again, we could initialize this model with a vocabulary if we had one.</p> <p>For the normalization, XLNet uses a few replacements (which come from SentencePiece):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> tokenizers <span class="hljs-keyword">import</span> Regex tokenizer.normalizer = normalizers.<span class="hljs-type">Sequence</span>( [ normalizers.Replace(<span class="hljs-string">"``"</span>, <span class="hljs-string">'"'</span>), normalizers.Replace(<span class="hljs-string">"''"</span>, <span class="hljs-string">'"'</span>), normalizers.NFKD(), normalizers.StripAccents(), normalizers.Replace(Regex(<span class="hljs-string">" {2,}"</span>), <span class="hljs-string">" "</span>), ] )</pre></div> <p>This replaces <code>“</code> and <code>”</code> with <code>”</code> and any sequence of two or more spaces with a single space, as well as removing the accents in the texts to tokenize.</p> <p>The pre-tokenizer to use for any SentencePiece tokenizer is <code>Metaspace</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.pre_tokenizer = pre_tokenizers.Metaspace()</pre></div> <p>We can have a look at the pre-tokenization of an example text like before:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.pre_tokenizer.pre_tokenize_str(<span class="hljs-string">"Let's test the pre-tokenizer!"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[(<span class="hljs-string">"▁Let's"</span>, (<span class="hljs-number">0</span>, <span class="hljs-number">5</span>)), (<span class="hljs-string">'▁test'</span>, (<span class="hljs-number">5</span>, <span class="hljs-number">10</span>)), (<span class="hljs-string">'▁the'</span>, (<span class="hljs-number">10</span>, <span class="hljs-number">14</span>)), (<span class="hljs-string">'▁pre-tokenizer!'</span>, (<span class="hljs-number">14</span>, <span class="hljs-number">29</span>))]</pre></div> <p>Next is the model, which needs training. XLNet has quite a few special tokens:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>special_tokens = [<span class="hljs-string">"&lt;cls&gt;"</span>, <span class="hljs-string">"&lt;sep&gt;"</span>, <span class="hljs-string">"&lt;unk&gt;"</span>, <span class="hljs-string">"&lt;pad&gt;"</span>, <span class="hljs-string">"&lt;mask&gt;"</span>, <span class="hljs-string">"&lt;s&gt;"</span>, <span class="hljs-string">"&lt;/s&gt;"</span>] trainer = trainers.UnigramTrainer( vocab_size=<span class="hljs-number">25000</span>, special_tokens=special_tokens, unk_token=<span class="hljs-string">"&lt;unk&gt;"</span> ) tokenizer.train_from_iterator(get_training_corpus(), trainer=trainer)</pre></div> <p>A very important argument not to forget for the <code>UnigramTrainer</code> is the <code>unk_token</code>. We can also pass along other arguments specific to the Unigram algorithm, such as the <code>shrinking_factor</code> for each step where we remove tokens (defaults to 0.75) or the <code>max_piece_length</code> to specify the maximum length of a given token (defaults to 16).</p> <p>This tokenizer can also be trained on text files:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.model = models.Unigram() tokenizer.train([<span class="hljs-string">"wikitext-2.txt"</span>], trainer=trainer)</pre></div> <p>Let’s have a look at the tokenization of a sample text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>encoding = tokenizer.encode(<span class="hljs-string">"Let's test this tokenizer."</span>) <span class="hljs-built_in">print</span>(encoding.tokens)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'▁Let'</span>, <span class="hljs-string">"'"</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'▁test'</span>, <span class="hljs-string">'▁this'</span>, <span class="hljs-string">'▁to'</span>, <span class="hljs-string">'ken'</span>, <span class="hljs-string">'izer'</span>, <span class="hljs-string">'.'</span>]</pre></div> <p>A peculiarity of XLNet is that it puts the <code>&lt;cls&gt;</code> token at the end of the sentence, with a type ID of 2 (to distinguish it from the other tokens). It’s padding on the left, as a result. We can deal with all the special tokens and token type IDs with a template, like for BERT, but first we have to get the IDs of the <code>&lt;cls&gt;</code> and <code>&lt;sep&gt;</code> tokens:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>cls_token_id = tokenizer.token_to_id(<span class="hljs-string">"&lt;cls&gt;"</span>) sep_token_id = tokenizer.token_to_id(<span class="hljs-string">"&lt;sep&gt;"</span>) <span class="hljs-built_in">print</span>(cls_token_id, sep_token_id)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">0</span> <span class="hljs-number">1</span></pre></div> <p>The template looks like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.post_processor = processors.TemplateProcessing( single=<span class="hljs-string">"$A:0 &lt;sep&gt;:0 &lt;cls&gt;:2"</span>, pair=<span class="hljs-string">"$A:0 &lt;sep&gt;:0 $B:1 &lt;sep&gt;:1 &lt;cls&gt;:2"</span>, special_tokens=[(<span class="hljs-string">"&lt;sep&gt;"</span>, sep_token_id), (<span class="hljs-string">"&lt;cls&gt;"</span>, cls_token_id)], )</pre></div> <p>And we can test it works by encoding a pair of sentences:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>encoding = tokenizer.encode(<span class="hljs-string">"Let's test this tokenizer..."</span>, <span class="hljs-string">"on a pair of sentences!"</span>) <span class="hljs-built_in">print</span>(encoding.tokens) <span class="hljs-built_in">print</span>(encoding.type_ids)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'▁Let'</span>, <span class="hljs-string">"'"</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'▁test'</span>, <span class="hljs-string">'▁this'</span>, <span class="hljs-string">'▁to'</span>, <span class="hljs-string">'ken'</span>, <span class="hljs-string">'izer'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'&lt;sep&gt;'</span>, <span class="hljs-string">'▁'</span>, <span class="hljs-string">'on'</span>, <span class="hljs-string">'▁'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'▁pair'</span>, <span class="hljs-string">'▁of'</span>, <span class="hljs-string">'▁sentence'</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'!'</span>, <span class="hljs-string">'&lt;sep&gt;'</span>, <span class="hljs-string">'&lt;cls&gt;'</span>] [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>]</pre></div> <p>Finally, we add a <code>Metaspace</code> decoder:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.decoder = decoders.Metaspace()</pre></div> <p>and we’re done with this tokenizer! We can save the tokenizer like before, and wrap it in a <code>PreTrainedTokenizerFast</code> or <code>XLNetTokenizerFast</code> if we want to use it in 🤗 Transformers. One thing to note when using <code>PreTrainedTokenizerFast</code> is that on top of the special tokens, we need to tell the 🤗 Transformers library to pad on the left:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PreTrainedTokenizerFast wrapped_tokenizer = PreTrainedTokenizerFast( tokenizer_object=tokenizer, bos_token=<span class="hljs-string">"&lt;s&gt;"</span>, eos_token=<span class="hljs-string">"&lt;/s&gt;"</span>, unk_token=<span class="hljs-string">"&lt;unk&gt;"</span>, pad_token=<span class="hljs-string">"&lt;pad&gt;"</span>, cls_token=<span class="hljs-string">"&lt;cls&gt;"</span>, sep_token=<span class="hljs-string">"&lt;sep&gt;"</span>, mask_token=<span class="hljs-string">"&lt;mask&gt;"</span>, padding_side=<span class="hljs-string">"left"</span>, )</pre></div> <p>Or alternatively:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> XLNetTokenizerFast wrapped_tokenizer = XLNetTokenizerFast(tokenizer_object=tokenizer)</pre></div> <p>Now that you have seen how the various building blocks are used to build existing tokenizers, you should be able to write any tokenizer you want with the 🤗 Tokenizers library and be able to use it in 🤗 Transformers.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter6/7?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Unigram tokenization</a> <a href="/learn/nlp-course/chapter6/9?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Tokenizers, check!<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;building-a-tokenizer-block-by-block&quot;,&quot;url&quot;:&quot;#building-a-tokenizer-block-by-block&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Acquiring a corpus&quot;,&quot;id&quot;:&quot;acquiring-a-corpus&quot;,&quot;url&quot;:&quot;#acquiring-a-corpus&quot;},{&quot;title&quot;:&quot;Building a WordPiece tokenizer from scratch&quot;,&quot;id&quot;:&quot;building-a-wordpiece-tokenizer-from-scratch&quot;,&quot;url&quot;:&quot;#building-a-wordpiece-tokenizer-from-scratch&quot;},{&quot;title&quot;:&quot;Building a BPE tokenizer from scratch&quot;,&quot;id&quot;:&quot;building-a-bpe-tokenizer-from-scratch&quot;,&quot;url&quot;:&quot;#building-a-bpe-tokenizer-from-scratch&quot;},{&quot;title&quot;:&quot;Building a Unigram tokenizer from scratch&quot;,&quot;id&quot;:&quot;building-a-unigram-tokenizer-from-scratch&quot;,&quot;url&quot;:&quot;#building-a-unigram-tokenizer-from-scratch&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#building-a-tokenizer-block-by-block" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-building-a-tokenizer-block-by-block"><wbr>Building a tokenizer, block by block</a> <a href="#acquiring-a-corpus" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-acquiring-a-corpus"><wbr>Acquiring a corpus</a> <a href="#building-a-wordpiece-tokenizer-from-scratch" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-building-a-wordpiece-tokenizer-from-scratch"><wbr>Building a <wbr>Word<wbr>Piece tokenizer from scratch</a> <a href="#building-a-bpe-tokenizer-from-scratch" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-building-a-bpe-tokenizer-from-scratch"><wbr>Building a BP<wbr>E tokenizer from scratch</a> <a href="#building-a-unigram-tokenizer-from-scratch" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-building-a-unigram-tokenizer-from-scratch"><wbr>Building a <wbr>Unigram tokenizer from scratch</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter6/8" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter6/8"); } </script> <iframe name="__privateStripeMetricsController6710" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter6%2F8%3Ffw%3Dpt&amp;title=Building%20a%20tokenizer%2C%20block%20by%20block%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:24.535Z
Tokenizers, check! - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter6/9?fw=pt
![Hugging Face's logo](/front/assets/huggingface_logo-noborder.svg) 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 ## [](#tokenizers-check)Tokenizers, check! [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-6-questions) Great job finishing this chapter! After this deep dive into tokenizers, you should: - Be able to train a new tokenizer using an old one as a template - Understand how to use offsets to map tokens’ positions to their original span of text - Know the differences between BPE, WordPiece, and Unigram - Be able to mix and match the blocks provided by the 🤗 Tokenizers library to build your own tokenizer - Be able to use that tokenizer inside the 🤗 Transformers library
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Tokenizers, check! - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter6/9"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Tokenizers, check! - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;tokenizers-check&quot;,&quot;title&quot;:&quot;Tokenizers, check!&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter6/9&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Tokenizers, check!&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Tokenizers, check!</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/2?fw=pt">Training a new tokenizer from an old one </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3?fw=pt">Fast tokenizers' special powers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3b?fw=pt">Fast tokenizers in the QA pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/4?fw=pt">Normalization and pre-tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/5?fw=pt">Byte-Pair Encoding tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/6?fw=pt">WordPiece tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/7?fw=pt">Unigram tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/8?fw=pt">Building a tokenizer, block by block </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter6/9?fw=pt">Tokenizers, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="tokenizers-check" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tokenizers-check"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Tokenizers, check!</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-6-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>Great job finishing this chapter!</p> <p>After this deep dive into tokenizers, you should:</p> <ul><li>Be able to train a new tokenizer using an old one as a template</li> <li>Understand how to use offsets to map tokens’ positions to their original span of text</li> <li>Know the differences between BPE, WordPiece, and Unigram</li> <li>Be able to mix and match the blocks provided by the 🤗 Tokenizers library to build your own tokenizer</li> <li>Be able to use that tokenizer inside the 🤗 Transformers library</li></ul> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter6/8?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Building a tokenizer, block by block</a> <a href="/learn/nlp-course/chapter6/10?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">End-of-chapter quiz<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;tokenizers-check&quot;,&quot;url&quot;:&quot;#tokenizers-check&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#tokenizers-check" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tokenizers-check"><wbr>Tokenizers, check!</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter6/9" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter6/9"); } </script> <iframe name="__privateStripeMetricsController7990" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter6%2F9%3Ffw%3Dpt&amp;title=Tokenizers%2C%20check!%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:24.799Z
End-of-chapter quiz - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter6/10?fw=pt
## [](#end-of-chapter-quiz)End-of-chapter quiz [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-6-questions) Let’s test what you learned in this chapter! ### [](#1.-when-should-you-train-a-new-tokenizer?)1\. When should you train a new tokenizer? ### [](#2.-what-is-the-advantage-of-using-a-generator-of-lists-of-texts-compared-to-a-list-of-lists-of-texts-when-using-<code>train_new_from_iterator()</code>?)2\. What is the advantage of using a generator of lists of texts compared to a list of lists of texts when using `train_new_from_iterator()`? ### [](#3.-what-are-the-advantages-of-using-a-“fast”-tokenizer?)3\. What are the advantages of using a “fast” tokenizer? ### [](#4.-how-does-the-<code>token-classification</code>-pipeline-handle-entities-that-span-over-several-tokens?)4\. How does the `token-classification` pipeline handle entities that span over several tokens? ### [](#5.-how-does-the-<code>question-answering</code>-pipeline-handle-long-contexts?)5\. How does the `question-answering` pipeline handle long contexts? ### [](#6.-what-is-normalization?)6\. What is normalization? ### [](#7.-what-is-pre-tokenization-for-a-subword-tokenizer?)7\. What is pre-tokenization for a subword tokenizer? ### [](#8.-select-the-sentences-that-apply-to-the-bpe-model-of-tokenization.)8\. Select the sentences that apply to the BPE model of tokenization. ### [](#9.-select-the-sentences-that-apply-to-the-wordpiece-model-of-tokenization.)9\. Select the sentences that apply to the WordPiece model of tokenization. ### [](#10.-select-the-sentences-that-apply-to-the-unigram-model-of-tokenization.)10\. Select the sentences that apply to the Unigram model of tokenization.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="End-of-chapter quiz - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter6/10"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>End-of-chapter quiz - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;end-of-chapter-quiz&quot;,&quot;title&quot;:&quot;End-of-chapter quiz&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter6/10&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;End-of-chapter quiz&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">End-of-chapter quiz</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/2?fw=pt">Training a new tokenizer from an old one </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3?fw=pt">Fast tokenizers' special powers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/3b?fw=pt">Fast tokenizers in the QA pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/4?fw=pt">Normalization and pre-tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/5?fw=pt">Byte-Pair Encoding tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/6?fw=pt">WordPiece tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/7?fw=pt">Unigram tokenization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/8?fw=pt">Building a tokenizer, block by block </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter6/9?fw=pt">Tokenizers, check! </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter6/10?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="end-of-chapter-quiz" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#end-of-chapter-quiz"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>End-of-chapter quiz</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-6-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>Let’s test what you learned in this chapter!</p> <h3 class="relative group"><a id="1.-when-should-you-train-a-new-tokenizer?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#1.-when-should-you-train-a-new-tokenizer?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>1. When should you train a new tokenizer?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> When your dataset is similar to that used by an existing pretrained model, and you want to pretrain a new model</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> When your dataset is similar to that used by an existing pretrained model, and you want to fine-tune a new model using this pretrained model</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> When your dataset is different from the one used by an existing pretrained model, and you want to pretrain a new model</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> When your dataset is different from the one used by an existing pretrained model, but you want to fine-tune a new model using this pretrained model</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="2.-what-is-the-advantage-of-using-a-generator-of-lists-of-texts-compared-to-a-list-of-lists-of-texts-when-using-<code>train_new_from_iterator()</code>?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#2.-what-is-the-advantage-of-using-a-generator-of-lists-of-texts-compared-to-a-list-of-lists-of-texts-when-using-<code>train_new_from_iterator()</code>?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>2. What is the advantage of using a generator of lists of texts compared to a list of lists of texts when using <code>train_new_from_iterator()</code>?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> That's the only type the method <code>train_new_from_iterator()</code> accepts.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> You will avoid loading the whole dataset into memory at once.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> This will allow the 🤗 Tokenizers library to use multiprocessing.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> The tokenizer you train will generate better texts.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="3.-what-are-the-advantages-of-using-a-“fast”-tokenizer?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#3.-what-are-the-advantages-of-using-a-“fast”-tokenizer?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>3. What are the advantages of using a “fast” tokenizer?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It can process inputs faster than a slow tokenizer when you batch lots of inputs together.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Fast tokenizers always tokenize faster than their slow counterparts.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It can apply padding and truncation.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> It has some additional features allowing you to map tokens to the span of text that created them.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="4.-how-does-the-<code>token-classification</code>-pipeline-handle-entities-that-span-over-several-tokens?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#4.-how-does-the-<code>token-classification</code>-pipeline-handle-entities-that-span-over-several-tokens?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>4. How does the <code>token-classification</code> pipeline handle entities that span over several tokens?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> The entities with the same label are merged into one entity.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> There is a label for the beginning of an entity and a label for the continuation of an entity.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> In a given word, as long as the first token has the label of the entity, the whole word is considered labeled with that entity.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> When a token has the label of a given entity, any other following token with the same label is considered part of the same entity, unless it's labeled as the start of a new entity.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="5.-how-does-the-<code>question-answering</code>-pipeline-handle-long-contexts?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#5.-how-does-the-<code>question-answering</code>-pipeline-handle-long-contexts?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>5. How does the <code>question-answering</code> pipeline handle long contexts?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It doesn't really, as it truncates the long context at the maximum length accepted by the model.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It splits the context into several parts and averages the results obtained.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It splits the context into several parts (with overlap) and finds the maximum score for an answer in each part.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> It splits the context into several parts (without overlap, for efficiency) and finds the maximum score for an answer in each part.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="6.-what-is-normalization?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#6.-what-is-normalization?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>6. What is normalization?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It's any cleanup the tokenizer performs on the texts in the initial stages.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It's a data augmentation technique that involves making the text more normal by removing rare words.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It's the final post-processing step where the tokenizer adds the special tokens.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> It's when the embeddings are made with mean 0 and standard deviation 1, by subtracting the mean and dividing by the std.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="7.-what-is-pre-tokenization-for-a-subword-tokenizer?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#7.-what-is-pre-tokenization-for-a-subword-tokenizer?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>7. What is pre-tokenization for a subword tokenizer?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It's the step before the tokenization, where data augmentation (like random masking) is applied.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It's the step before the tokenization, where the desired cleanup operations are applied to the text.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It's the step before the tokenizer model is applied, to split the input into words.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> It's the step before the tokenizer model is applied, to split the input into tokens.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="8.-select-the-sentences-that-apply-to-the-bpe-model-of-tokenization." class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#8.-select-the-sentences-that-apply-to-the-bpe-model-of-tokenization."><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>8. Select the sentences that apply to the BPE model of tokenization.</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> BPE is a subword tokenization algorithm that starts with a small vocabulary and learns merge rules.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> BPE is a subword tokenization algorithm that starts with a big vocabulary and progressively removes tokens from it.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> BPE tokenizers learn merge rules by merging the pair of tokens that is the most frequent.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> A BPE tokenizer learns a merge rule by merging the pair of tokens that maximizes a score that privileges frequent pairs with less frequent individual parts.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="4"> BPE tokenizes words into subwords by splitting them into characters and then applying the merge rules.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="5"> BPE tokenizes words into subwords by finding the longest subword starting from the beginning that is in the vocabulary, then repeating the process for the rest of the text.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="9.-select-the-sentences-that-apply-to-the-wordpiece-model-of-tokenization." class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#9.-select-the-sentences-that-apply-to-the-wordpiece-model-of-tokenization."><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>9. Select the sentences that apply to the WordPiece model of tokenization.</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> WordPiece is a subword tokenization algorithm that starts with a small vocabulary and learns merge rules.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> WordPiece is a subword tokenization algorithm that starts with a big vocabulary and progressively removes tokens from it.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> WordPiece tokenizers learn merge rules by merging the pair of tokens that is the most frequent.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> A WordPiece tokenizer learns a merge rule by merging the pair of tokens that maximizes a score that privileges frequent pairs with less frequent individual parts.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="4"> WordPiece tokenizes words into subwords by finding the most likely segmentation into tokens, according to the model.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="5"> WordPiece tokenizes words into subwords by finding the longest subword starting from the beginning that is in the vocabulary, then repeating the process for the rest of the text.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="10.-select-the-sentences-that-apply-to-the-unigram-model-of-tokenization." class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#10.-select-the-sentences-that-apply-to-the-unigram-model-of-tokenization."><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>10. Select the sentences that apply to the Unigram model of tokenization.</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Unigram is a subword tokenization algorithm that starts with a small vocabulary and learns merge rules.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Unigram is a subword tokenization algorithm that starts with a big vocabulary and progressively removes tokens from it.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Unigram adapts its vocabulary by minimizing a loss computed over the whole corpus.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Unigram adapts its vocabulary by keeping the most frequent subwords.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="4"> Unigram tokenizes words into subwords by finding the most likely segmentation into tokens, according to the model.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="5"> Unigram tokenizes words into subwords by splitting them into characters, then applying the merge rules.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter6/9?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Tokenizers, check!</a> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isLoggedIn&quot;:false,&quot;nextChapterUrl&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;,&quot;quizChapter&quot;:6}" data-target="CourseCompleteButton"><div class="relative ml-auto"> <button class="btn btn-lg" disabled=""><svg class="mr-2" width="1em" height="1em" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0 16C0 7.16344 7.16344 0 16 0V0C24.8366 0 32 7.16344 32 16V16C32 24.8366 24.8366 32 16 32V32C7.16344 32 0 24.8366 0 16V16Z" fill="#D9FFD2"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M23.5607 10.9393C24.1464 11.5251 24.1464 12.4749 23.5607 13.0607L14.5 22.1213L8.93934 16.5607C8.35355 15.9749 8.35355 15.0251 8.93934 14.4393C9.52513 13.8536 10.4749 13.8536 11.0607 14.4393L14.5 17.8787L21.4393 10.9393C22.0251 10.3536 22.9749 10.3536 23.5607 10.9393Z" fill="#188B3F"></path></svg> Complete Chapter</button></div></div> <a href="/learn/nlp-course/chapter7/1?fw=pt" class="hidden">Next chapter</a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;id&quot;:&quot;end-of-chapter-quiz&quot;,&quot;url&quot;:&quot;#end-of-chapter-quiz&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#end-of-chapter-quiz" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-end-of-chapter-quiz"><wbr>End-of-chapter quiz</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter6/10" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter6/10"); } </script> <iframe name="__privateStripeMetricsController3290" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter6%2F10%3Ffw%3Dpt&amp;title=End-of-chapter%20quiz%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=17d75daa-df84-469e-af79-64a34f98225af5f002&amp;sid=e5acaebc-72ef-4c45-996c-50c92c80ca8865613b&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:24.947Z
Introduction - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter7/1?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#introduction)Introduction [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-7-questions) In [Chapter 3](/course/chapter3), you saw how to fine-tune a model for text classification. In this chapter, we will tackle the following common NLP tasks: - Token classification - Masked language modeling (like BERT) - Summarization - Translation - Causal language modeling pretraining (like GPT-2) - Question answering To do this, you’ll need to leverage everything you learned about the `Trainer` API and the 🤗 Accelerate library in [Chapter 3](/course/chapter3), the 🤗 Datasets library in [Chapter 5](/course/chapter5), and the 🤗 Tokenizers library in [Chapter 6](/course/chapter6). We’ll also upload our results to the Model Hub, like we did in [Chapter 4](/course/chapter4), so this is really the chapter where everything comes together! Each section can be read independently and will show you how to train a model with the `Trainer` API or with your own training loop, using 🤗 Accelerate. Feel free to skip either part and focus on the one that interests you the most: the `Trainer` API is great for fine-tuning or training your model without worrying about what’s going on behind the scenes, while the training loop with `Accelerate` will let you customize any part you want more easily. If you read the sections in sequence, you will notice that they have quite a bit of code and prose in common. The repetition is intentional, to allow you to dip in (or come back later) to any task that interests you and find a complete working example.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Introduction - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter7/1"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Introduction - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;introduction&quot;,&quot;title&quot;:&quot;Introduction&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter7/1&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Introduction&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Introduction</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter7/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/2?fw=pt">Token classification </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/3?fw=pt">Fine-tuning a masked language model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/4?fw=pt">Translation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/5?fw=pt">Summarization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/6?fw=pt">Training a causal language model from scratch </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/7?fw=pt">Question answering </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/8?fw=pt">Mastering NLP </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="introduction" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#introduction"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Introduction</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-7-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>In <a href="/course/chapter3">Chapter 3</a>, you saw how to fine-tune a model for text classification. In this chapter, we will tackle the following common NLP tasks:</p> <ul><li>Token classification</li> <li>Masked language modeling (like BERT)</li> <li>Summarization</li> <li>Translation</li> <li>Causal language modeling pretraining (like GPT-2)</li> <li>Question answering</li></ul> <p>To do this, you’ll need to leverage everything you learned about the <code>Trainer</code> API and the 🤗 Accelerate library in <a href="/course/chapter3">Chapter 3</a>, the 🤗 Datasets library in <a href="/course/chapter5">Chapter 5</a>, and the 🤗 Tokenizers library in <a href="/course/chapter6">Chapter 6</a>. We’ll also upload our results to the Model Hub, like we did in <a href="/course/chapter4">Chapter 4</a>, so this is really the chapter where everything comes together!</p> <p>Each section can be read independently and will show you how to train a model with the <code>Trainer</code> API or with your own training loop, using 🤗 Accelerate. Feel free to skip either part and focus on the one that interests you the most: the <code>Trainer</code> API is great for fine-tuning or training your model without worrying about what’s going on behind the scenes, while the training loop with <code>Accelerate</code> will let you customize any part you want more easily.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>If you read the sections in sequence, you will notice that they have quite a bit of code and prose in common. The repetition is intentional, to allow you to dip in (or come back later) to any task that interests you and find a complete working example.</p></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter6/10?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>End-of-chapter quiz</a> <a href="/learn/nlp-course/chapter7/2?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Token classification<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;introduction&quot;,&quot;url&quot;:&quot;#introduction&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#introduction" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-introduction"><wbr>Introduction</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter7/1" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter7/1"); } </script> <iframe name="__privateStripeMetricsController2330" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter7%2F1%3Ffw%3Dpt&amp;title=Introduction%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:26.397Z
Token classification - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter7/2?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#token-classification)Token classification [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-7-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section2_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section2_pt.ipynb) The first application we’ll explore is token classification. This generic task encompasses any problem that can be formulated as “attributing a label to each token in a sentence,” such as: - **Named entity recognition (NER)**: Find the entities (such as persons, locations, or organizations) in a sentence. This can be formulated as attributing a label to each token by having one class per entity and one class for “no entity.” - **Part-of-speech tagging (POS)**: Mark each word in a sentence as corresponding to a particular part of speech (such as noun, verb, adjective, etc.). - **Chunking**: Find the tokens that belong to the same entity. This task (which can be combined with POS or NER) can be formulated as attributing one label (usually `B-`) to any tokens that are at the beginning of a chunk, another label (usually `I-`) to tokens that are inside a chunk, and a third label (usually `O`) to tokens that don’t belong to any chunk. Of course, there are many other types of token classification problem; those are just a few representative examples. In this section, we will fine-tune a model (BERT) on a NER task, which will then be able to compute predictions like this one: [![One-hot encoded labels for question answering.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/model-eval-bert-finetuned-ner.png) ![One-hot encoded labels for question answering.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/model-eval-bert-finetuned-ner-dark.png)](/huggingface-course/bert-finetuned-ner) You can find the model we’ll train and upload to the Hub and double-check its predictions [here](https://huggingface.co/huggingface-course/bert-finetuned-ner?text=My+name+is+Sylvain+and+I+work+at+Hugging+Face+in+Brooklyn). ## [](#preparing-the-data)Preparing the data First things first, we need a dataset suitable for token classification. In this section we will use the [CoNLL-2003 dataset](https://huggingface.co/datasets/conll2003), which contains news stories from Reuters. 💡 As long as your dataset consists of texts split into words with their corresponding labels, you will be able to adapt the data processing procedures described here to your own dataset. Refer back to [Chapter 5](/course/chapter5) if you need a refresher on how to load your own custom data in a `Dataset`. ### [](#the-conll-2003-dataset)The CoNLL-2003 dataset To load the CoNLL-2003 dataset, we use the `load_dataset()` method from the 🤗 Datasets library: ``` from datasets import load_dataset raw_datasets = load_dataset("conll2003")``` This will download and cache the dataset, like we saw in [Chapter 3](/course/chapter3) for the GLUE MRPC dataset. Inspecting this object shows us the columns present and the split between the training, validation, and test sets: ``` DatasetDict({ train: Dataset({ features: ['chunk_tags', 'id', 'ner_tags', 'pos_tags', 'tokens'], num_rows: 14041 }) validation: Dataset({ features: ['chunk_tags', 'id', 'ner_tags', 'pos_tags', 'tokens'], num_rows: 3250 }) test: Dataset({ features: ['chunk_tags', 'id', 'ner_tags', 'pos_tags', 'tokens'], num_rows: 3453 }) })``` In particular, we can see the dataset contains labels for the three tasks we mentioned earlier: NER, POS, and chunking. A big difference from other datasets is that the input texts are not presented as sentences or documents, but lists of words (the last column is called `tokens`, but it contains words in the sense that these are pre-tokenized inputs that still need to go through the tokenizer for subword tokenization). Let’s have a look at the first element of the training set: ``` raw_datasets["train"][0]["tokens"]``` ``` ['EU', 'rejects', 'German', 'call', 'to', 'boycott', 'British', 'lamb', '.']``` Since we want to perform named entity recognition, we will look at the NER tags: ``` raw_datasets["train"][0]["ner_tags"]``` ``` [3, 0, 7, 0, 0, 0, 7, 0, 0]``` Those are the labels as integers ready for training, but they’re not necessarily useful when we want to inspect the data. Like for text classification, we can access the correspondence between those integers and the label names by looking at the `features` attribute of our dataset: ``` ner_feature = raw_datasets["train"].features["ner_tags"] ner_feature``` ``` Sequence(feature=ClassLabel(num_classes=9, names=['O', 'B-PER', 'I-PER', 'B-ORG', 'I-ORG', 'B-LOC', 'I-LOC', 'B-MISC', 'I-MISC'], names_file=None, id=None), length=-1, id=None)``` So this column contains elements that are sequences of `ClassLabel`s. The type of the elements of the sequence is in the `feature` attribute of this `ner_feature`, and we can access the list of names by looking at the `names` attribute of that `feature`: ``` label_names = ner_feature.feature.names label_names``` ``` ['O', 'B-PER', 'I-PER', 'B-ORG', 'I-ORG', 'B-LOC', 'I-LOC', 'B-MISC', 'I-MISC']``` We already saw these labels when digging into the `token-classification` pipeline in [Chapter 6](/course/chapter6/3), but for a quick refresher: - `O` means the word doesn’t correspond to any entity. - `B-PER`/`I-PER` means the word corresponds to the beginning of/is inside a _person_ entity. - `B-ORG`/`I-ORG` means the word corresponds to the beginning of/is inside an _organization_ entity. - `B-LOC`/`I-LOC` means the word corresponds to the beginning of/is inside a _location_ entity. - `B-MISC`/`I-MISC` means the word corresponds to the beginning of/is inside a _miscellaneous_ entity. Now decoding the labels we saw earlier gives us this: ``` words = raw_datasets["train"][0]["tokens"] labels = raw_datasets["train"][0]["ner_tags"] line1 = "" line2 = "" for word, label in zip(words, labels): full_label = label_names[label] max_length = max(len(word), len(full_label)) line1 += word + " " * (max_length - len(word) + 1) line2 += full_label + " " * (max_length - len(full_label) + 1) print(line1) print(line2)``` ``` 'EU rejects German call to boycott British lamb .' 'B-ORG O B-MISC O O O B-MISC O O'``` And for an example mixing `B-` and `I-` labels, here’s what the same code gives us on the element of the training set at index 4: ``` 'Germany \'s representative to the European Union \'s veterinary committee Werner Zwingmann said on Wednesday consumers should buy sheepmeat from countries other than Britain until the scientific advice was clearer .' 'B-LOC O O O O B-ORG I-ORG O O O B-PER I-PER O O O O O O O O O O O B-LOC O O O O O O O'``` As we can see, entities spanning two words, like “European Union” and “Werner Zwingmann,” are attributed a `B-` label for the first word and an `I-` label for the second. ✏️ **Your turn!** Print the same two sentences with their POS or chunking labels. ### [](#processing-the-data)Processing the data As usual, our texts need to be converted to token IDs before the model can make sense of them. As we saw in [Chapter 6](/course/chapter6/), a big difference in the case of token classification tasks is that we have pre-tokenized inputs. Fortunately, the tokenizer API can deal with that pretty easily; we just need to warn the `tokenizer` with a special flag. To begin, let’s create our `tokenizer` object. As we said before, we will be using a BERT pretrained model, so we’ll start by downloading and caching the associated tokenizer: ``` from transformers import AutoTokenizer model_checkpoint = "bert-base-cased" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)``` You can replace the `model_checkpoint` with any other model you prefer from the [Hub](https://huggingface.co/models), or with a local folder in which you’ve saved a pretrained model and a tokenizer. The only constraint is that the tokenizer needs to be backed by the 🤗 Tokenizers library, so there’s a “fast” version available. You can see all the architectures that come with a fast version in [this big table](https://huggingface.co/transformers/#supported-frameworks), and to check that the `tokenizer` object you’re using is indeed backed by 🤗 Tokenizers you can look at its `is_fast` attribute: To tokenize a pre-tokenized input, we can use our `tokenizer` as usual and just add `is_split_into_words=True`: ``` inputs = tokenizer(raw_datasets["train"][0]["tokens"], is_split_into_words=True) inputs.tokens()``` ``` ['[CLS]', 'EU', 'rejects', 'German', 'call', 'to', 'boycott', 'British', 'la', '##mb', '.', '[SEP]']``` As we can see, the tokenizer added the special tokens used by the model (`[CLS]` at the beginning and `[SEP]` at the end) and left most of the words untouched. The word `lamb`, however, was tokenized into two subwords, `la` and `##mb`. This introduces a mismatch between our inputs and the labels: the list of labels has only 9 elements, whereas our input now has 12 tokens. Accounting for the special tokens is easy (we know they are at the beginning and the end), but we also need to make sure we align all the labels with the proper words. Fortunately, because we’re using a fast tokenizer we have access to the 🤗 Tokenizers superpowers, which means we can easily map each token to its corresponding word (as seen in [Chapter 6](/course/chapter6/3)): ``` [None, 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, None]``` With a tiny bit of work, we can then expand our label list to match the tokens. The first rule we’ll apply is that special tokens get a label of `-100`. This is because by default `-100` is an index that is ignored in the loss function we will use (cross entropy). Then, each token gets the same label as the token that started the word it’s inside, since they are part of the same entity. For tokens inside a word but not at the beginning, we replace the `B-` with `I-` (since the token does not begin the entity): ``` def align_labels_with_tokens(labels, word_ids): new_labels = [] current_word = None for word_id in word_ids: if word_id != current_word: current_word = word_id label = -100 if word_id is None else labels[word_id] new_labels.append(label) elif word_id is None: new_labels.append(-100) else: label = labels[word_id] if label % 2 == 1: label += 1 new_labels.append(label) return new_labels``` Let’s try it out on our first sentence: ``` labels = raw_datasets["train"][0]["ner_tags"] word_ids = inputs.word_ids() print(labels) print(align_labels_with_tokens(labels, word_ids))``` ``` [3, 0, 7, 0, 0, 0, 7, 0, 0] [-100, 3, 0, 7, 0, 0, 0, 7, 0, 0, 0, -100]``` As we can see, our function added the `-100` for the two special tokens at the beginning and the end, and a new `0` for our word that was split into two tokens. ✏️ **Your turn!** Some researchers prefer to attribute only one label per word, and assign `-100` to the other subtokens in a given word. This is to avoid long words that split into lots of subtokens contributing heavily to the loss. Change the previous function to align labels with input IDs by following this rule. To preprocess our whole dataset, we need to tokenize all the inputs and apply `align_labels_with_tokens()` on all the labels. To take advantage of the speed of our fast tokenizer, it’s best to tokenize lots of texts at the same time, so we’ll write a function that processes a list of examples and use the `Dataset.map()` method with the option `batched=True`. The only thing that is different from our previous example is that the `word_ids()` function needs to get the index of the example we want the word IDs of when the inputs to the tokenizer are lists of texts (or in our case, list of lists of words), so we add that too: ``` def tokenize_and_align_labels(examples): tokenized_inputs = tokenizer( examples["tokens"], truncation=True, is_split_into_words=True ) all_labels = examples["ner_tags"] new_labels = [] for i, labels in enumerate(all_labels): word_ids = tokenized_inputs.word_ids(i) new_labels.append(align_labels_with_tokens(labels, word_ids)) tokenized_inputs["labels"] = new_labels return tokenized_inputs``` Note that we haven’t padded our inputs yet; we’ll do that later, when creating the batches with a data collator. We can now apply all that preprocessing in one go on the other splits of our dataset: ``` tokenized_datasets = raw_datasets.map( tokenize_and_align_labels, batched=True, remove_columns=raw_datasets["train"].column_names, )``` We’ve done the hardest part! Now that the data has been preprocessed, the actual training will look a lot like what we did in [Chapter 3](/course/chapter3). ## [](#fine-tuning-the-model-with-the-trainer-api)Fine-tuning the model with the `Trainer` API The actual code using the `Trainer` will be the same as before; the only changes are the way the data is collated into a batch and the metric computation function. ### [](#data-collation)Data collation We can’t just use a `DataCollatorWithPadding` like in [Chapter 3](/course/chapter3) because that only pads the inputs (input IDs, attention mask, and token type IDs). Here our labels should be padded the exact same way as the inputs so that they stay the same size, using `-100` as a value so that the corresponding predictions are ignored in the loss computation. This is all done by a [`DataCollatorForTokenClassification`](https://huggingface.co/transformers/main_classes/data_collator.html#datacollatorfortokenclassification). Like the `DataCollatorWithPadding`, it takes the `tokenizer` used to preprocess the inputs: ``` from transformers import DataCollatorForTokenClassification data_collator = DataCollatorForTokenClassification(tokenizer=tokenizer)``` To test this on a few samples, we can just call it on a list of examples from our tokenized training set: ``` batch = data_collator([tokenized_datasets["train"][i] for i in range(2)]) batch["labels"]``` ``` tensor([[-100, 3, 0, 7, 0, 0, 0, 7, 0, 0, 0, -100], [-100, 1, 2, -100, -100, -100, -100, -100, -100, -100, -100, -100]])``` Let’s compare this to the labels for the first and second elements in our dataset: ``` for i in range(2): print(tokenized_datasets["train"][i]["labels"])``` ``` [-100, 3, 0, 7, 0, 0, 0, 7, 0, 0, 0, -100] [-100, 1, 2, -100]``` As we can see, the second set of labels has been padded to the length of the first one using `-100`s. ### [](#metrics)Metrics To have the `Trainer` compute a metric every epoch, we will need to define a `compute_metrics()` function that takes the arrays of predictions and labels, and returns a dictionary with the metric names and values. The traditional framework used to evaluate token classification prediction is [_seqeval_](https://github.com/chakki-works/seqeval). To use this metric, we first need to install the _seqeval_ library: We can then load it via the `evaluate.load()` function like we did in [Chapter 3](/course/chapter3): ``` import evaluate metric = evaluate.load("seqeval")``` This metric does not behave like the standard accuracy: it will actually take the lists of labels as strings, not integers, so we will need to fully decode the predictions and labels before passing them to the metric. Let’s see how it works. First, we’ll get the labels for our first training example: ``` labels = raw_datasets["train"][0]["ner_tags"] labels = [label_names[i] for i in labels] labels``` ``` ['B-ORG', 'O', 'B-MISC', 'O', 'O', 'O', 'B-MISC', 'O', 'O']``` We can then create fake predictions for those by just changing the value at index 2: ``` predictions = labels.copy() predictions[2] = "O" metric.compute(predictions=[predictions], references=[labels])``` Note that the metric takes a list of predictions (not just one) and a list of labels. Here’s the output: ``` {'MISC': {'precision': 1.0, 'recall': 0.5, 'f1': 0.67, 'number': 2}, 'ORG': {'precision': 1.0, 'recall': 1.0, 'f1': 1.0, 'number': 1}, 'overall_precision': 1.0, 'overall_recall': 0.67, 'overall_f1': 0.8, 'overall_accuracy': 0.89}``` This is sending back a lot of information! We get the precision, recall, and F1 score for each separate entity, as well as overall. For our metric computation we will only keep the overall score, but feel free to tweak the `compute_metrics()` function to return all the metrics you would like reported. This `compute_metrics()` function first takes the argmax of the logits to convert them to predictions (as usual, the logits and the probabilities are in the same order, so we don’t need to apply the softmax). Then we have to convert both labels and predictions from integers to strings. We remove all the values where the label is `-100`, then pass the results to the `metric.compute()` method: ``` import numpy as np def compute_metrics(eval_preds): logits, labels = eval_preds predictions = np.argmax(logits, axis=-1) true_labels = [[label_names[l] for l in label if l != -100] for label in labels] true_predictions = [ [label_names[p] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] all_metrics = metric.compute(predictions=true_predictions, references=true_labels) return { "precision": all_metrics["overall_precision"], "recall": all_metrics["overall_recall"], "f1": all_metrics["overall_f1"], "accuracy": all_metrics["overall_accuracy"], }``` Now that this is done, we are almost ready to define our `Trainer`. We just need a `model` to fine-tune! ### [](#defining-the-model)Defining the model Since we are working on a token classification problem, we will use the `AutoModelForTokenClassification` class. The main thing to remember when defining this model is to pass along some information on the number of labels we have. The easiest way to do this is to pass that number with the `num_labels` argument, but if we want a nice inference widget working like the one we saw at the beginning of this section, it’s better to set the correct label correspondences instead. They should be set by two dictionaries, `id2label` and `label2id`, which contain the mappings from ID to label and vice versa: ``` id2label = {i: label for i, label in enumerate(label_names)} label2id = {v: k for k, v in id2label.items()}``` Now we can just pass them to the `AutoModelForTokenClassification.from_pretrained()` method, and they will be set in the model’s configuration and then properly saved and uploaded to the Hub: ``` from transformers import AutoModelForTokenClassification model = AutoModelForTokenClassification.from_pretrained( model_checkpoint, id2label=id2label, label2id=label2id, )``` Like when we defined our `AutoModelForSequenceClassification` in [Chapter 3](/course/chapter3), creating the model issues a warning that some weights were not used (the ones from the pretraining head) and some other weights are randomly initialized (the ones from the new token classification head), and that this model should be trained. We will do that in a minute, but first let’s double-check that our model has the right number of labels: ⚠️ If you have a model with the wrong number of labels, you will get an obscure error when calling the `Trainer.train()` method later on (something like “CUDA error: device-side assert triggered”). This is the number one cause of bugs reported by users for such errors, so make sure you do this check to confirm that you have the expected number of labels. ### [](#fine-tuning-the-model)Fine-tuning the model We are now ready to train our model! We just need to do two last things before we define our `Trainer`: log in to Hugging Face and define our training arguments. If you’re working in a notebook, there’s a convenience function to help you with this: ``` from huggingface_hub import notebook_login notebook_login()``` This will display a widget where you can enter your Hugging Face login credentials. If you aren’t working in a notebook, just type the following line in your terminal: Once this is done, we can define our `TrainingArguments`: ``` from transformers import TrainingArguments args = TrainingArguments( "bert-finetuned-ner", evaluation_strategy="epoch", save_strategy="epoch", learning_rate=2e-5, num_train_epochs=3, weight_decay=0.01, push_to_hub=True, )``` You’ve seen most of those before: we set some hyperparameters (like the learning rate, the number of epochs to train for, and the weight decay), and we specify `push_to_hub=True` to indicate that we want to save the model and evaluate it at the end of every epoch, and that we want to upload our results to the Model Hub. Note that you can specify the name of the repository you want to push to with the `hub_model_id` argument (in particular, you will have to use this argument to push to an organization). For instance, when we pushed the model to the [`huggingface-course` organization](https://huggingface.co/huggingface-course), we added `hub_model_id="huggingface-course/bert-finetuned-ner"` to `TrainingArguments`. By default, the repository used will be in your namespace and named after the output directory you set, so in our case it will be `"sgugger/bert-finetuned-ner"`. 💡 If the output directory you are using already exists, it needs to be a local clone of the repository you want to push to. If it isn’t, you’ll get an error when defining your `Trainer` and will need to set a new name. Finally, we just pass everything to the `Trainer` and launch the training: ``` from transformers import Trainer trainer = Trainer( model=model, args=args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=data_collator, compute_metrics=compute_metrics, tokenizer=tokenizer, ) trainer.train()``` Note that while the training happens, each time the model is saved (here, every epoch) it is uploaded to the Hub in the background. This way, you will be able to to resume your training on another machine if necessary. Once the training is complete, we use the `push_to_hub()` method to make sure we upload the most recent version of the model: ``` trainer.push_to_hub(commit_message="Training complete")``` This command returns the URL of the commit it just did, if you want to inspect it: ``` 'https://huggingface.co/sgugger/bert-finetuned-ner/commit/26ab21e5b1568f9afeccdaed2d8715f571d786ed'``` The `Trainer` also drafts a model card with all the evaluation results and uploads it. At this stage, you can use the inference widget on the Model Hub to test your model and share it with your friends. You have successfully fine-tuned a model on a token classification task — congratulations! If you want to dive a bit more deeply into the training loop, we will now show you how to do the same thing using 🤗 Accelerate. ## [](#a-custom-training-loop)A custom training loop Let’s now take a look at the full training loop, so you can easily customize the parts you need. It will look a lot like what we did in [Chapter 3](/course/chapter3/4), with a few changes for the evaluation. ### [](#preparing-everything-for-training)Preparing everything for training First we need to build the `DataLoader`s from our datasets. We’ll reuse our `data_collator` as a `collate_fn` and shuffle the training set, but not the validation set: ``` from torch.utils.data import DataLoader train_dataloader = DataLoader( tokenized_datasets["train"], shuffle=True, collate_fn=data_collator, batch_size=8, ) eval_dataloader = DataLoader( tokenized_datasets["validation"], collate_fn=data_collator, batch_size=8 )``` Next we reinstantiate our model, to make sure we’re not continuing the fine-tuning from before but starting from the BERT pretrained model again: ``` model = AutoModelForTokenClassification.from_pretrained( model_checkpoint, id2label=id2label, label2id=label2id, )``` Then we will need an optimizer. We’ll use the classic `AdamW`, which is like `Adam`, but with a fix in the way weight decay is applied: ``` from torch.optim import AdamW optimizer = AdamW(model.parameters(), lr=2e-5)``` Once we have all those objects, we can send them to the `accelerator.prepare()` method: ``` from accelerate import Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )``` 🚨 If you’re training on a TPU, you’ll need to move all the code starting from the cell above into a dedicated training function. See [Chapter 3](/course/chapter3) for more details. Now that we have sent our `train_dataloader` to `accelerator.prepare()`, we can use its length to compute the number of training steps. Remember that we should always do this after preparing the dataloader, as that method will change its length. We use a classic linear schedule from the learning rate to 0: ``` from transformers import get_scheduler num_train_epochs = 3 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, )``` Lastly, to push our model to the Hub, we will need to create a `Repository` object in a working folder. First log in to Hugging Face, if you’re not logged in already. We’ll determine the repository name from the model ID we want to give our model (feel free to replace the `repo_name` with your own choice; it just needs to contain your username, which is what the function `get_full_repo_name()` does): ``` from huggingface_hub import Repository, get_full_repo_name model_name = "bert-finetuned-ner-accelerate" repo_name = get_full_repo_name(model_name) repo_name``` ``` 'sgugger/bert-finetuned-ner-accelerate'``` Then we can clone that repository in a local folder. If it already exists, this local folder should be an existing clone of the repository we are working with: ``` output_dir = "bert-finetuned-ner-accelerate" repo = Repository(output_dir, clone_from=repo_name)``` We can now upload anything we save in `output_dir` by calling the `repo.push_to_hub()` method. This will help us upload the intermediate models at the end of each epoch. ### [](#training-loop)Training loop We are now ready to write the full training loop. To simplify its evaluation part, we define this `postprocess()` function that takes predictions and labels and converts them to lists of strings, like our `metric` object expects: ``` def postprocess(predictions, labels): predictions = predictions.detach().cpu().clone().numpy() labels = labels.detach().cpu().clone().numpy() true_labels = [[label_names[l] for l in label if l != -100] for label in labels] true_predictions = [ [label_names[p] for (p, l) in zip(prediction, label) if l != -100] for prediction, label in zip(predictions, labels) ] return true_labels, true_predictions``` Then we can write the training loop. After defining a progress bar to follow how training goes, the loop has three parts: - The training in itself, which is the classic iteration over the `train_dataloader`, forward pass through the model, then backward pass and optimizer step. - The evaluation, in which there is a novelty after getting the outputs of our model on a batch: since two processes may have padded the inputs and labels to different shapes, we need to use `accelerator.pad_across_processes()` to make the predictions and labels the same shape before calling the `gather()` method. If we don’t do this, the evaluation will either error out or hang forever. Then we send the results to `metric.add_batch()` and call `metric.compute()` once the evaluation loop is over. - Saving and uploading, where we first save the model and the tokenizer, then call `repo.push_to_hub()`. Notice that we use the argument `blocking=False` to tell the 🤗 Hub library to push in an asynchronous process. This way, training continues normally and this (long) instruction is executed in the background. Here’s the complete code for the training loop: ``` from tqdm.auto import tqdm import torch progress_bar = tqdm(range(num_training_steps)) for epoch in range(num_train_epochs): model.train() for batch in train_dataloader: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) model.eval() for batch in eval_dataloader: with torch.no_grad(): outputs = model(**batch) predictions = outputs.logits.argmax(dim=-1) labels = batch["labels"] predictions = accelerator.pad_across_processes(predictions, dim=1, pad_index=-100) labels = accelerator.pad_across_processes(labels, dim=1, pad_index=-100) predictions_gathered = accelerator.gather(predictions) labels_gathered = accelerator.gather(labels) true_predictions, true_labels = postprocess(predictions_gathered, labels_gathered) metric.add_batch(predictions=true_predictions, references=true_labels) results = metric.compute() print( f"epoch {epoch}:", { key: results[f"overall_{key}"] for key in ["precision", "recall", "f1", "accuracy"] }, ) accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=f"Training in progress epoch {epoch}", blocking=False )``` In case this is the first time you’re seeing a model saved with 🤗 Accelerate, let’s take a moment to inspect the three lines of code that go with it: ``` accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save)``` The first line is self-explanatory: it tells all the processes to wait until everyone is at that stage before continuing. This is to make sure we have the same model in every process before saving. Then we grab the `unwrapped_model`, which is the base model we defined. The `accelerator.prepare()` method changes the model to work in distributed training, so it won’t have the `save_pretrained()` method anymore; the `accelerator.unwrap_model()` method undoes that step. Lastly, we call `save_pretrained()` but tell that method to use `accelerator.save()` instead of `torch.save()`. Once this is done, you should have a model that produces results pretty similar to the one trained with the `Trainer`. You can check the model we trained using this code at [_huggingface-course/bert-finetuned-ner-accelerate_](https://huggingface.co/huggingface-course/bert-finetuned-ner-accelerate). And if you want to test out any tweaks to the training loop, you can directly implement them by editing the code shown above! ## [](#using-the-fine-tuned-model)Using the fine-tuned model We’ve already shown you how you can use the model we fine-tuned on the Model Hub with the inference widget. To use it locally in a `pipeline`, you just have to specify the proper model identifier: ``` from transformers import pipeline model_checkpoint = "huggingface-course/bert-finetuned-ner" token_classifier = pipeline( "token-classification", model=model_checkpoint, aggregation_strategy="simple" ) token_classifier("My name is Sylvain and I work at Hugging Face in Brooklyn.")``` ``` [{'entity_group': 'PER', 'score': 0.9988506, 'word': 'Sylvain', 'start': 11, 'end': 18}, {'entity_group': 'ORG', 'score': 0.9647625, 'word': 'Hugging Face', 'start': 33, 'end': 45}, {'entity_group': 'LOC', 'score': 0.9986118, 'word': 'Brooklyn', 'start': 49, 'end': 57}]``` Great! Our model is working as well as the default one for this pipeline!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Token classification - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter7/2"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Token classification - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;token-classification&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;preparing-the-data&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;the-conll-2003-dataset&quot;,&quot;title&quot;:&quot;The CoNLL-2003 dataset&quot;},{&quot;local&quot;:&quot;processing-the-data&quot;,&quot;title&quot;:&quot;Processing the data&quot;}],&quot;title&quot;:&quot;Preparing the data&quot;},{&quot;local&quot;:&quot;fine-tuning-the-model-with-the-trainer-api&quot;,&quot;title&quot;:&quot;Fine-tuning the model with the `Trainer` API&quot;},{&quot;local&quot;:&quot;fine-tuning-the-model-with-keras&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;data-collation&quot;,&quot;title&quot;:&quot;Data collation&quot;},{&quot;local&quot;:&quot;defining-the-model&quot;,&quot;title&quot;:&quot;Defining the model&quot;},{&quot;local&quot;:&quot;fine-tuning-the-model&quot;,&quot;title&quot;:&quot;Fine-tuning the model&quot;},{&quot;local&quot;:&quot;metrics&quot;,&quot;title&quot;:&quot;Metrics&quot;},{&quot;local&quot;:&quot;defining-the-model&quot;,&quot;title&quot;:&quot;Defining the model&quot;},{&quot;local&quot;:&quot;fine-tuning-the-model&quot;,&quot;title&quot;:&quot;Fine-tuning the model&quot;}],&quot;title&quot;:&quot;Fine-tuning the model with Keras&quot;},{&quot;local&quot;:&quot;a-custom-training-loop&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;preparing-everything-for-training&quot;,&quot;title&quot;:&quot;Preparing everything for training&quot;},{&quot;local&quot;:&quot;training-loop&quot;,&quot;title&quot;:&quot;Training loop&quot;}],&quot;title&quot;:&quot;A custom training loop&quot;},{&quot;local&quot;:&quot;using-the-fine-tuned-model&quot;,&quot;title&quot;:&quot;Using the fine-tuned model&quot;}],&quot;title&quot;:&quot;Token classification&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter7/2&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Token classification&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Token classification</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/1?fw=pt">Introduction </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter7/2?fw=pt">Token classification </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/3?fw=pt">Fine-tuning a masked language model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/4?fw=pt">Translation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/5?fw=pt">Summarization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/6?fw=pt">Training a causal language model from scratch </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/7?fw=pt">Question answering </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/8?fw=pt">Mastering NLP </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="token-classification" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#token-classification"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Token classification</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-7-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section2_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section2_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>The first application we’ll explore is token classification. This generic task encompasses any problem that can be formulated as “attributing a label to each token in a sentence,” such as:</p> <ul><li><strong>Named entity recognition (NER)</strong>: Find the entities (such as persons, locations, or organizations) in a sentence. This can be formulated as attributing a label to each token by having one class per entity and one class for “no entity.”</li> <li><strong>Part-of-speech tagging (POS)</strong>: Mark each word in a sentence as corresponding to a particular part of speech (such as noun, verb, adjective, etc.).</li> <li><strong>Chunking</strong>: Find the tokens that belong to the same entity. This task (which can be combined with POS or NER) can be formulated as attributing one label (usually <code>B-</code>) to any tokens that are at the beginning of a chunk, another label (usually <code>I-</code>) to tokens that are inside a chunk, and a third label (usually <code>O</code>) to tokens that don’t belong to any chunk.</li></ul> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/wVHdVlPScxA" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Of course, there are many other types of token classification problem; those are just a few representative examples. In this section, we will fine-tune a model (BERT) on a NER task, which will then be able to compute predictions like this one:</p> <iframe src="https://course-demos-bert-finetuned-ner.hf.space" frameborder="0" height="350" title="Gradio app" class="block dark:hidden container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <a class="flex justify-center" href="/huggingface-course/bert-finetuned-ner"><img class="block dark:hidden lg:w-3/5" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/model-eval-bert-finetuned-ner.png" alt="One-hot encoded labels for question answering."> <img class="hidden dark:block lg:w-3/5" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/model-eval-bert-finetuned-ner-dark.png" alt="One-hot encoded labels for question answering."></a> <p>You can find the model we’ll train and upload to the Hub and double-check its predictions <a href="https://huggingface.co/huggingface-course/bert-finetuned-ner?text=My+name+is+Sylvain+and+I+work+at+Hugging+Face+in+Brooklyn" rel="nofollow">here</a>.</p> <h2 class="relative group"><a id="preparing-the-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preparing-the-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preparing the data</span></h2> <p>First things first, we need a dataset suitable for token classification. In this section we will use the <a href="https://huggingface.co/datasets/conll2003" rel="nofollow">CoNLL-2003 dataset</a>, which contains news stories from Reuters.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 As long as your dataset consists of texts split into words with their corresponding labels, you will be able to adapt the data processing procedures described here to your own dataset. Refer back to <a href="/course/chapter5">Chapter 5</a> if you need a refresher on how to load your own custom data in a <code>Dataset</code>.</p></div> <h3 class="relative group"><a id="the-conll-2003-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-conll-2003-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The CoNLL-2003 dataset</span></h3> <p>To load the CoNLL-2003 dataset, we use the <code>load_dataset()</code> method from the 🤗 Datasets library:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset raw_datasets = load_dataset(<span class="hljs-string">"conll2003"</span>)</pre></div> <p>This will download and cache the dataset, like we saw in <a href="/course/chapter3">Chapter 3</a> for the GLUE MRPC dataset. Inspecting this object shows us the columns present and the split between the training, validation, and test sets:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>raw_datasets</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'chunk_tags'</span>, <span class="hljs-string">'id'</span>, <span class="hljs-string">'ner_tags'</span>, <span class="hljs-string">'pos_tags'</span>, <span class="hljs-string">'tokens'</span>], num_rows: <span class="hljs-number">14041</span> }) validation: Dataset({ features: [<span class="hljs-string">'chunk_tags'</span>, <span class="hljs-string">'id'</span>, <span class="hljs-string">'ner_tags'</span>, <span class="hljs-string">'pos_tags'</span>, <span class="hljs-string">'tokens'</span>], num_rows: <span class="hljs-number">3250</span> }) test: Dataset({ features: [<span class="hljs-string">'chunk_tags'</span>, <span class="hljs-string">'id'</span>, <span class="hljs-string">'ner_tags'</span>, <span class="hljs-string">'pos_tags'</span>, <span class="hljs-string">'tokens'</span>], num_rows: <span class="hljs-number">3453</span> }) })</pre></div> <p>In particular, we can see the dataset contains labels for the three tasks we mentioned earlier: NER, POS, and chunking. A big difference from other datasets is that the input texts are not presented as sentences or documents, but lists of words (the last column is called <code>tokens</code>, but it contains words in the sense that these are pre-tokenized inputs that still need to go through the tokenizer for subword tokenization).</p> <p>Let’s have a look at the first element of the training set:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"tokens"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'EU'</span>, <span class="hljs-string">'rejects'</span>, <span class="hljs-string">'German'</span>, <span class="hljs-string">'call'</span>, <span class="hljs-string">'to'</span>, <span class="hljs-string">'boycott'</span>, <span class="hljs-string">'British'</span>, <span class="hljs-string">'lamb'</span>, <span class="hljs-string">'.'</span>]</pre></div> <p>Since we want to perform named entity recognition, we will look at the NER tags:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"ner_tags"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">3</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]</pre></div> <p>Those are the labels as integers ready for training, but they’re not necessarily useful when we want to inspect the data. Like for text classification, we can access the correspondence between those integers and the label names by looking at the <code>features</code> attribute of our dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>ner_feature = raw_datasets[<span class="hljs-string">"train"</span>].features[<span class="hljs-string">"ner_tags"</span>] ner_feature</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-type">Sequence</span>(feature=ClassLabel(num_classes=<span class="hljs-number">9</span>, names=[<span class="hljs-string">'O'</span>, <span class="hljs-string">'B-PER'</span>, <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'B-ORG'</span>, <span class="hljs-string">'I-ORG'</span>, <span class="hljs-string">'B-LOC'</span>, <span class="hljs-string">'I-LOC'</span>, <span class="hljs-string">'B-MISC'</span>, <span class="hljs-string">'I-MISC'</span>], names_file=<span class="hljs-literal">None</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>), length=-<span class="hljs-number">1</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>)</pre></div> <p>So this column contains elements that are sequences of <code>ClassLabel</code>s. The type of the elements of the sequence is in the <code>feature</code> attribute of this <code>ner_feature</code>, and we can access the list of names by looking at the <code>names</code> attribute of that <code>feature</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>label_names = ner_feature.feature.names label_names</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'O'</span>, <span class="hljs-string">'B-PER'</span>, <span class="hljs-string">'I-PER'</span>, <span class="hljs-string">'B-ORG'</span>, <span class="hljs-string">'I-ORG'</span>, <span class="hljs-string">'B-LOC'</span>, <span class="hljs-string">'I-LOC'</span>, <span class="hljs-string">'B-MISC'</span>, <span class="hljs-string">'I-MISC'</span>]</pre></div> <p>We already saw these labels when digging into the <code>token-classification</code> pipeline in <a href="/course/chapter6/3">Chapter 6</a>, but for a quick refresher:</p> <ul><li><code>O</code> means the word doesn’t correspond to any entity.</li> <li><code>B-PER</code>/<code>I-PER</code> means the word corresponds to the beginning of/is inside a <em>person</em> entity.</li> <li><code>B-ORG</code>/<code>I-ORG</code> means the word corresponds to the beginning of/is inside an <em>organization</em> entity.</li> <li><code>B-LOC</code>/<code>I-LOC</code> means the word corresponds to the beginning of/is inside a <em>location</em> entity.</li> <li><code>B-MISC</code>/<code>I-MISC</code> means the word corresponds to the beginning of/is inside a <em>miscellaneous</em> entity.</li></ul> <p>Now decoding the labels we saw earlier gives us this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>words = raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"tokens"</span>] labels = raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"ner_tags"</span>] line1 = <span class="hljs-string">""</span> line2 = <span class="hljs-string">""</span> <span class="hljs-keyword">for</span> word, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(words, labels): full_label = label_names[label] max_length = <span class="hljs-built_in">max</span>(<span class="hljs-built_in">len</span>(word), <span class="hljs-built_in">len</span>(full_label)) line1 += word + <span class="hljs-string">" "</span> * (max_length - <span class="hljs-built_in">len</span>(word) + <span class="hljs-number">1</span>) line2 += full_label + <span class="hljs-string">" "</span> * (max_length - <span class="hljs-built_in">len</span>(full_label) + <span class="hljs-number">1</span>) <span class="hljs-built_in">print</span>(line1) <span class="hljs-built_in">print</span>(line2)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'EU rejects German call to boycott British lamb .'</span> <span class="hljs-string">'B-ORG O B-MISC O O O B-MISC O O'</span></pre></div> <p>And for an example mixing <code>B-</code> and <code>I-</code> labels, here’s what the same code gives us on the element of the training set at index 4:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'Germany \'s representative to the European Union \'s veterinary committee Werner Zwingmann said on Wednesday consumers should buy sheepmeat from countries other than Britain until the scientific advice was clearer .'</span> <span class="hljs-string">'B-LOC O O O O B-ORG I-ORG O O O B-PER I-PER O O O O O O O O O O O B-LOC O O O O O O O'</span></pre></div> <p>As we can see, entities spanning two words, like “European Union” and “Werner Zwingmann,” are attributed a <code>B-</code> label for the first word and an <code>I-</code> label for the second.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Your turn!</strong> Print the same two sentences with their POS or chunking labels.</p></div> <h3 class="relative group"><a id="processing-the-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#processing-the-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Processing the data</span></h3> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/iY2AZYdZAr0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>As usual, our texts need to be converted to token IDs before the model can make sense of them. As we saw in <a href="/course/chapter6/">Chapter 6</a>, a big difference in the case of token classification tasks is that we have pre-tokenized inputs. Fortunately, the tokenizer API can deal with that pretty easily; we just need to warn the <code>tokenizer</code> with a special flag.</p> <p>To begin, let’s create our <code>tokenizer</code> object. As we said before, we will be using a BERT pretrained model, so we’ll start by downloading and caching the associated tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer model_checkpoint = <span class="hljs-string">"bert-base-cased"</span> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)</pre></div> <p>You can replace the <code>model_checkpoint</code> with any other model you prefer from the <a href="https://huggingface.co/models" rel="nofollow">Hub</a>, or with a local folder in which you’ve saved a pretrained model and a tokenizer. The only constraint is that the tokenizer needs to be backed by the 🤗 Tokenizers library, so there’s a “fast” version available. You can see all the architectures that come with a fast version in <a href="https://huggingface.co/transformers/#supported-frameworks" rel="nofollow">this big table</a>, and to check that the <code>tokenizer</code> object you’re using is indeed backed by 🤗 Tokenizers you can look at its <code>is_fast</code> attribute:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.is_fast</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-literal">True</span></pre></div> <p>To tokenize a pre-tokenized input, we can use our <code>tokenizer</code> as usual and just add <code>is_split_into_words=True</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs = tokenizer(raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"tokens"</span>], is_split_into_words=<span class="hljs-literal">True</span>) inputs.tokens()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'[CLS]'</span>, <span class="hljs-string">'EU'</span>, <span class="hljs-string">'rejects'</span>, <span class="hljs-string">'German'</span>, <span class="hljs-string">'call'</span>, <span class="hljs-string">'to'</span>, <span class="hljs-string">'boycott'</span>, <span class="hljs-string">'British'</span>, <span class="hljs-string">'la'</span>, <span class="hljs-string">'##mb'</span>, <span class="hljs-string">'.'</span>, <span class="hljs-string">'[SEP]'</span>]</pre></div> <p>As we can see, the tokenizer added the special tokens used by the model (<code>[CLS]</code> at the beginning and <code>[SEP]</code> at the end) and left most of the words untouched. The word <code>lamb</code>, however, was tokenized into two subwords, <code>la</code> and <code>##mb</code>. This introduces a mismatch between our inputs and the labels: the list of labels has only 9 elements, whereas our input now has 12 tokens. Accounting for the special tokens is easy (we know they are at the beginning and the end), but we also need to make sure we align all the labels with the proper words.</p> <p>Fortunately, because we’re using a fast tokenizer we have access to the 🤗 Tokenizers superpowers, which means we can easily map each token to its corresponding word (as seen in <a href="/course/chapter6/3">Chapter 6</a>):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs.word_ids()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-literal">None</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-literal">None</span>]</pre></div> <p>With a tiny bit of work, we can then expand our label list to match the tokens. The first rule we’ll apply is that special tokens get a label of <code>-100</code>. This is because by default <code>-100</code> is an index that is ignored in the loss function we will use (cross entropy). Then, each token gets the same label as the token that started the word it’s inside, since they are part of the same entity. For tokens inside a word but not at the beginning, we replace the <code>B-</code> with <code>I-</code> (since the token does not begin the entity):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">align_labels_with_tokens</span>(<span class="hljs-params">labels, word_ids</span>): new_labels = [] current_word = <span class="hljs-literal">None</span> <span class="hljs-keyword">for</span> word_id <span class="hljs-keyword">in</span> word_ids: <span class="hljs-keyword">if</span> word_id != current_word: <span class="hljs-comment"># Start of a new word!</span> current_word = word_id label = -<span class="hljs-number">100</span> <span class="hljs-keyword">if</span> word_id <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">else</span> labels[word_id] new_labels.append(label) <span class="hljs-keyword">elif</span> word_id <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>: <span class="hljs-comment"># Special token</span> new_labels.append(-<span class="hljs-number">100</span>) <span class="hljs-keyword">else</span>: <span class="hljs-comment"># Same word as previous token</span> label = labels[word_id] <span class="hljs-comment"># If the label is B-XXX we change it to I-XXX</span> <span class="hljs-keyword">if</span> label % <span class="hljs-number">2</span> == <span class="hljs-number">1</span>: label += <span class="hljs-number">1</span> new_labels.append(label) <span class="hljs-keyword">return</span> new_labels</pre></div> <p>Let’s try it out on our first sentence:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>labels = raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"ner_tags"</span>] word_ids = inputs.word_ids() <span class="hljs-built_in">print</span>(labels) <span class="hljs-built_in">print</span>(align_labels_with_tokens(labels, word_ids))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">3</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>] [-<span class="hljs-number">100</span>, <span class="hljs-number">3</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, -<span class="hljs-number">100</span>]</pre></div> <p>As we can see, our function added the <code>-100</code> for the two special tokens at the beginning and the end, and a new <code>0</code> for our word that was split into two tokens.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Your turn!</strong> Some researchers prefer to attribute only one label per word, and assign <code>-100</code> to the other subtokens in a given word. This is to avoid long words that split into lots of subtokens contributing heavily to the loss. Change the previous function to align labels with input IDs by following this rule.</p></div> <p>To preprocess our whole dataset, we need to tokenize all the inputs and apply <code>align_labels_with_tokens()</code> on all the labels. To take advantage of the speed of our fast tokenizer, it’s best to tokenize lots of texts at the same time, so we’ll write a function that processes a list of examples and use the <code>Dataset.map()</code> method with the option <code>batched=True</code>. The only thing that is different from our previous example is that the <code>word_ids()</code> function needs to get the index of the example we want the word IDs of when the inputs to the tokenizer are lists of texts (or in our case, list of lists of words), so we add that too:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_and_align_labels</span>(<span class="hljs-params">examples</span>): tokenized_inputs = tokenizer( examples[<span class="hljs-string">"tokens"</span>], truncation=<span class="hljs-literal">True</span>, is_split_into_words=<span class="hljs-literal">True</span> ) all_labels = examples[<span class="hljs-string">"ner_tags"</span>] new_labels = [] <span class="hljs-keyword">for</span> i, labels <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(all_labels): word_ids = tokenized_inputs.word_ids(i) new_labels.append(align_labels_with_tokens(labels, word_ids)) tokenized_inputs[<span class="hljs-string">"labels"</span>] = new_labels <span class="hljs-keyword">return</span> tokenized_inputs</pre></div> <p>Note that we haven’t padded our inputs yet; we’ll do that later, when creating the batches with a data collator.</p> <p>We can now apply all that preprocessing in one go on the other splits of our dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>( tokenize_and_align_labels, batched=<span class="hljs-literal">True</span>, remove_columns=raw_datasets[<span class="hljs-string">"train"</span>].column_names, )</pre></div> <p>We’ve done the hardest part! Now that the data has been preprocessed, the actual training will look a lot like what we did in <a href="/course/chapter3">Chapter 3</a>.</p> <h2 class="relative group"><a id="fine-tuning-the-model-with-the-trainer-api" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-the-model-with-the-trainer-api"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning the model with the <code>Trainer</code> API</span></h2> <p>The actual code using the <code>Trainer</code> will be the same as before; the only changes are the way the data is collated into a batch and the metric computation function.</p> <h3 class="relative group"><a id="data-collation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#data-collation"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Data collation</span></h3> <p>We can’t just use a <code>DataCollatorWithPadding</code> like in <a href="/course/chapter3">Chapter 3</a> because that only pads the inputs (input IDs, attention mask, and token type IDs). Here our labels should be padded the exact same way as the inputs so that they stay the same size, using <code>-100</code> as a value so that the corresponding predictions are ignored in the loss computation.</p> <p>This is all done by a <a href="https://huggingface.co/transformers/main_classes/data_collator.html#datacollatorfortokenclassification" rel="nofollow"><code>DataCollatorForTokenClassification</code></a>. Like the <code>DataCollatorWithPadding</code>, it takes the <code>tokenizer</code> used to preprocess the inputs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForTokenClassification data_collator = DataCollatorForTokenClassification(tokenizer=tokenizer)</pre></div> <p>To test this on a few samples, we can just call it on a list of examples from our tokenized training set:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>batch = data_collator([tokenized_datasets[<span class="hljs-string">"train"</span>][i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">2</span>)]) batch[<span class="hljs-string">"labels"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tensor([[-<span class="hljs-number">100</span>, <span class="hljs-number">3</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, -<span class="hljs-number">100</span>], [-<span class="hljs-number">100</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>]])</pre></div> <p>Let’s compare this to the labels for the first and second elements in our dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">2</span>): <span class="hljs-built_in">print</span>(tokenized_datasets[<span class="hljs-string">"train"</span>][i][<span class="hljs-string">"labels"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[-<span class="hljs-number">100</span>, <span class="hljs-number">3</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, -<span class="hljs-number">100</span>] [-<span class="hljs-number">100</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, -<span class="hljs-number">100</span>]</pre></div> <p>As we can see, the second set of labels has been padded to the length of the first one using <code>-100</code>s.</p> <h3 class="relative group"><a id="metrics" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#metrics"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Metrics</span></h3> <p>To have the <code>Trainer</code> compute a metric every epoch, we will need to define a <code>compute_metrics()</code> function that takes the arrays of predictions and labels, and returns a dictionary with the metric names and values.</p> <p>The traditional framework used to evaluate token classification prediction is <a href="https://github.com/chakki-works/seqeval" rel="nofollow"><em>seqeval</em></a>. To use this metric, we first need to install the <em>seqeval</em> library:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!pip install seqeval</pre></div> <p>We can then load it via the <code>evaluate.load()</code> function like we did in <a href="/course/chapter3">Chapter 3</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> evaluate metric = evaluate.load(<span class="hljs-string">"seqeval"</span>)</pre></div> <p>This metric does not behave like the standard accuracy: it will actually take the lists of labels as strings, not integers, so we will need to fully decode the predictions and labels before passing them to the metric. Let’s see how it works. First, we’ll get the labels for our first training example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>labels = raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"ner_tags"</span>] labels = [label_names[i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> labels] labels</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'B-ORG'</span>, <span class="hljs-string">'O'</span>, <span class="hljs-string">'B-MISC'</span>, <span class="hljs-string">'O'</span>, <span class="hljs-string">'O'</span>, <span class="hljs-string">'O'</span>, <span class="hljs-string">'B-MISC'</span>, <span class="hljs-string">'O'</span>, <span class="hljs-string">'O'</span>]</pre></div> <p>We can then create fake predictions for those by just changing the value at index 2:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>predictions = labels.copy() predictions[<span class="hljs-number">2</span>] = <span class="hljs-string">"O"</span> metric.compute(predictions=[predictions], references=[labels])</pre></div> <p>Note that the metric takes a list of predictions (not just one) and a list of labels. Here’s the output:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'MISC'</span>: {<span class="hljs-string">'precision'</span>: <span class="hljs-number">1.0</span>, <span class="hljs-string">'recall'</span>: <span class="hljs-number">0.5</span>, <span class="hljs-string">'f1'</span>: <span class="hljs-number">0.67</span>, <span class="hljs-string">'number'</span>: <span class="hljs-number">2</span>}, <span class="hljs-string">'ORG'</span>: {<span class="hljs-string">'precision'</span>: <span class="hljs-number">1.0</span>, <span class="hljs-string">'recall'</span>: <span class="hljs-number">1.0</span>, <span class="hljs-string">'f1'</span>: <span class="hljs-number">1.0</span>, <span class="hljs-string">'number'</span>: <span class="hljs-number">1</span>}, <span class="hljs-string">'overall_precision'</span>: <span class="hljs-number">1.0</span>, <span class="hljs-string">'overall_recall'</span>: <span class="hljs-number">0.67</span>, <span class="hljs-string">'overall_f1'</span>: <span class="hljs-number">0.8</span>, <span class="hljs-string">'overall_accuracy'</span>: <span class="hljs-number">0.89</span>}</pre></div> <p>This is sending back a lot of information! We get the precision, recall, and F1 score for each separate entity, as well as overall. For our metric computation we will only keep the overall score, but feel free to tweak the <code>compute_metrics()</code> function to return all the metrics you would like reported.</p> <p>This <code>compute_metrics()</code> function first takes the argmax of the logits to convert them to predictions (as usual, the logits and the probabilities are in the same order, so we don’t need to apply the softmax). Then we have to convert both labels and predictions from integers to strings. We remove all the values where the label is <code>-100</code>, then pass the results to the <code>metric.compute()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_preds</span>): logits, labels = eval_preds predictions = np.argmax(logits, axis=-<span class="hljs-number">1</span>) <span class="hljs-comment"># Remove ignored index (special tokens) and convert to labels</span> true_labels = [[label_names[l] <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> label <span class="hljs-keyword">if</span> l != -<span class="hljs-number">100</span>] <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> labels] true_predictions = [ [label_names[p] <span class="hljs-keyword">for</span> (p, l) <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(prediction, label) <span class="hljs-keyword">if</span> l != -<span class="hljs-number">100</span>] <span class="hljs-keyword">for</span> prediction, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(predictions, labels) ] all_metrics = metric.compute(predictions=true_predictions, references=true_labels) <span class="hljs-keyword">return</span> { <span class="hljs-string">"precision"</span>: all_metrics[<span class="hljs-string">"overall_precision"</span>], <span class="hljs-string">"recall"</span>: all_metrics[<span class="hljs-string">"overall_recall"</span>], <span class="hljs-string">"f1"</span>: all_metrics[<span class="hljs-string">"overall_f1"</span>], <span class="hljs-string">"accuracy"</span>: all_metrics[<span class="hljs-string">"overall_accuracy"</span>], }</pre></div> <p>Now that this is done, we are almost ready to define our <code>Trainer</code>. We just need a <code>model</code> to fine-tune!</p> <h3 class="relative group"><a id="defining-the-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#defining-the-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Defining the model</span></h3> <p>Since we are working on a token classification problem, we will use the <code>AutoModelForTokenClassification</code> class. The main thing to remember when defining this model is to pass along some information on the number of labels we have. The easiest way to do this is to pass that number with the <code>num_labels</code> argument, but if we want a nice inference widget working like the one we saw at the beginning of this section, it’s better to set the correct label correspondences instead.</p> <p>They should be set by two dictionaries, <code>id2label</code> and <code>label2id</code>, which contain the mappings from ID to label and vice versa:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>id2label = {i: label <span class="hljs-keyword">for</span> i, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(label_names)} label2id = {v: k <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> id2label.items()}</pre></div> <p>Now we can just pass them to the <code>AutoModelForTokenClassification.from_pretrained()</code> method, and they will be set in the model’s configuration and then properly saved and uploaded to the Hub:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForTokenClassification model = AutoModelForTokenClassification.from_pretrained( model_checkpoint, id2label=id2label, label2id=label2id, )</pre></div> <p>Like when we defined our <code>AutoModelForSequenceClassification</code> in <a href="/course/chapter3">Chapter 3</a>, creating the model issues a warning that some weights were not used (the ones from the pretraining head) and some other weights are randomly initialized (the ones from the new token classification head), and that this model should be trained. We will do that in a minute, but first let’s double-check that our model has the right number of labels:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model.config.num_labels</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">9</span></pre></div> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>⚠️ If you have a model with the wrong number of labels, you will get an obscure error when calling the <code>Trainer.train()</code> method later on (something like “CUDA error: device-side assert triggered”). This is the number one cause of bugs reported by users for such errors, so make sure you do this check to confirm that you have the expected number of labels.</p></div> <h3 class="relative group"><a id="fine-tuning-the-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-the-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning the model</span></h3> <p>We are now ready to train our model! We just need to do two last things before we define our <code>Trainer</code>: log in to Hugging Face and define our training arguments. If you’re working in a notebook, there’s a convenience function to help you with this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login notebook_login()</pre></div> <p>This will display a widget where you can enter your Hugging Face login credentials.</p> <p>If you aren’t working in a notebook, just type the following line in your terminal:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-cli login</pre></div> <p>Once this is done, we can define our <code>TrainingArguments</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments args = TrainingArguments( <span class="hljs-string">"bert-finetuned-ner"</span>, evaluation_strategy=<span class="hljs-string">"epoch"</span>, save_strategy=<span class="hljs-string">"epoch"</span>, learning_rate=<span class="hljs-number">2e-5</span>, num_train_epochs=<span class="hljs-number">3</span>, weight_decay=<span class="hljs-number">0.01</span>, push_to_hub=<span class="hljs-literal">True</span>, )</pre></div> <p>You’ve seen most of those before: we set some hyperparameters (like the learning rate, the number of epochs to train for, and the weight decay), and we specify <code>push_to_hub=True</code> to indicate that we want to save the model and evaluate it at the end of every epoch, and that we want to upload our results to the Model Hub. Note that you can specify the name of the repository you want to push to with the <code>hub_model_id</code> argument (in particular, you will have to use this argument to push to an organization). For instance, when we pushed the model to the <a href="https://huggingface.co/huggingface-course" rel="nofollow"><code>huggingface-course</code> organization</a>, we added <code>hub_model_id="huggingface-course/bert-finetuned-ner"</code> to <code>TrainingArguments</code>. By default, the repository used will be in your namespace and named after the output directory you set, so in our case it will be <code>"sgugger/bert-finetuned-ner"</code>.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 If the output directory you are using already exists, it needs to be a local clone of the repository you want to push to. If it isn’t, you’ll get an error when defining your <code>Trainer</code> and will need to set a new name.</p></div> <p>Finally, we just pass everything to the <code>Trainer</code> and launch the training:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Trainer trainer = Trainer( model=model, args=args, train_dataset=tokenized_datasets[<span class="hljs-string">"train"</span>], eval_dataset=tokenized_datasets[<span class="hljs-string">"validation"</span>], data_collator=data_collator, compute_metrics=compute_metrics, tokenizer=tokenizer, ) trainer.train()</pre></div> <p>Note that while the training happens, each time the model is saved (here, every epoch) it is uploaded to the Hub in the background. This way, you will be able to to resume your training on another machine if necessary.</p> <p>Once the training is complete, we use the <code>push_to_hub()</code> method to make sure we upload the most recent version of the model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.push_to_hub(commit_message=<span class="hljs-string">"Training complete"</span>)</pre></div> <p>This command returns the URL of the commit it just did, if you want to inspect it:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'https://huggingface.co/sgugger/bert-finetuned-ner/commit/26ab21e5b1568f9afeccdaed2d8715f571d786ed'</span></pre></div> <p>The <code>Trainer</code> also drafts a model card with all the evaluation results and uploads it. At this stage, you can use the inference widget on the Model Hub to test your model and share it with your friends. You have successfully fine-tuned a model on a token classification task — congratulations!</p> <p>If you want to dive a bit more deeply into the training loop, we will now show you how to do the same thing using 🤗 Accelerate.</p> <h2 class="relative group"><a id="a-custom-training-loop" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#a-custom-training-loop"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>A custom training loop</span></h2> <p>Let’s now take a look at the full training loop, so you can easily customize the parts you need. It will look a lot like what we did in <a href="/course/chapter3/4">Chapter 3</a>, with a few changes for the evaluation.</p> <h3 class="relative group"><a id="preparing-everything-for-training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preparing-everything-for-training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preparing everything for training</span></h3> <p>First we need to build the <code>DataLoader</code>s from our datasets. We’ll reuse our <code>data_collator</code> as a <code>collate_fn</code> and shuffle the training set, but not the validation set:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader train_dataloader = DataLoader( tokenized_datasets[<span class="hljs-string">"train"</span>], shuffle=<span class="hljs-literal">True</span>, collate_fn=data_collator, batch_size=<span class="hljs-number">8</span>, ) eval_dataloader = DataLoader( tokenized_datasets[<span class="hljs-string">"validation"</span>], collate_fn=data_collator, batch_size=<span class="hljs-number">8</span> )</pre></div> <p>Next we reinstantiate our model, to make sure we’re not continuing the fine-tuning from before but starting from the BERT pretrained model again:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model = AutoModelForTokenClassification.from_pretrained( model_checkpoint, id2label=id2label, label2id=label2id, )</pre></div> <p>Then we will need an optimizer. We’ll use the classic <code>AdamW</code>, which is like <code>Adam</code>, but with a fix in the way weight decay is applied:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.optim <span class="hljs-keyword">import</span> AdamW optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">2e-5</span>)</pre></div> <p>Once we have all those objects, we can send them to the <code>accelerator.prepare()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>🚨 If you’re training on a TPU, you’ll need to move all the code starting from the cell above into a dedicated training function. See <a href="/course/chapter3">Chapter 3</a> for more details.</p></div> <p>Now that we have sent our <code>train_dataloader</code> to <code>accelerator.prepare()</code>, we can use its length to compute the number of training steps. Remember that we should always do this after preparing the dataloader, as that method will change its length. We use a classic linear schedule from the learning rate to 0:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler num_train_epochs = <span class="hljs-number">3</span> num_update_steps_per_epoch = <span class="hljs-built_in">len</span>(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( <span class="hljs-string">"linear"</span>, optimizer=optimizer, num_warmup_steps=<span class="hljs-number">0</span>, num_training_steps=num_training_steps, )</pre></div> <p>Lastly, to push our model to the Hub, we will need to create a <code>Repository</code> object in a working folder. First log in to Hugging Face, if you’re not logged in already. We’ll determine the repository name from the model ID we want to give our model (feel free to replace the <code>repo_name</code> with your own choice; it just needs to contain your username, which is what the function <code>get_full_repo_name()</code> does):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> Repository, get_full_repo_name model_name = <span class="hljs-string">"bert-finetuned-ner-accelerate"</span> repo_name = get_full_repo_name(model_name) repo_name</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'sgugger/bert-finetuned-ner-accelerate'</span></pre></div> <p>Then we can clone that repository in a local folder. If it already exists, this local folder should be an existing clone of the repository we are working with:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>output_dir = <span class="hljs-string">"bert-finetuned-ner-accelerate"</span> repo = Repository(output_dir, clone_from=repo_name)</pre></div> <p>We can now upload anything we save in <code>output_dir</code> by calling the <code>repo.push_to_hub()</code> method. This will help us upload the intermediate models at the end of each epoch.</p> <h3 class="relative group"><a id="training-loop" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-loop"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training loop</span></h3> <p>We are now ready to write the full training loop. To simplify its evaluation part, we define this <code>postprocess()</code> function that takes predictions and labels and converts them to lists of strings, like our <code>metric</code> object expects:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">postprocess</span>(<span class="hljs-params">predictions, labels</span>): predictions = predictions.detach().cpu().clone().numpy() labels = labels.detach().cpu().clone().numpy() <span class="hljs-comment"># Remove ignored index (special tokens) and convert to labels</span> true_labels = [[label_names[l] <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> label <span class="hljs-keyword">if</span> l != -<span class="hljs-number">100</span>] <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> labels] true_predictions = [ [label_names[p] <span class="hljs-keyword">for</span> (p, l) <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(prediction, label) <span class="hljs-keyword">if</span> l != -<span class="hljs-number">100</span>] <span class="hljs-keyword">for</span> prediction, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(predictions, labels) ] <span class="hljs-keyword">return</span> true_labels, true_predictions</pre></div> <p>Then we can write the training loop. After defining a progress bar to follow how training goes, the loop has three parts:</p> <ul><li>The training in itself, which is the classic iteration over the <code>train_dataloader</code>, forward pass through the model, then backward pass and optimizer step.</li> <li>The evaluation, in which there is a novelty after getting the outputs of our model on a batch: since two processes may have padded the inputs and labels to different shapes, we need to use <code>accelerator.pad_across_processes()</code> to make the predictions and labels the same shape before calling the <code>gather()</code> method. If we don’t do this, the evaluation will either error out or hang forever. Then we send the results to <code>metric.add_batch()</code> and call <code>metric.compute()</code> once the evaluation loop is over.</li> <li>Saving and uploading, where we first save the model and the tokenizer, then call <code>repo.push_to_hub()</code>. Notice that we use the argument <code>blocking=False</code> to tell the 🤗 Hub library to push in an asynchronous process. This way, training continues normally and this (long) instruction is executed in the background.</li></ul> <p>Here’s the complete code for the training loop:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm <span class="hljs-keyword">import</span> torch progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps)) <span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_train_epochs): <span class="hljs-comment"># Training</span> model.train() <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(<span class="hljs-number">1</span>) <span class="hljs-comment"># Evaluation</span> model.<span class="hljs-built_in">eval</span>() <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> eval_dataloader: <span class="hljs-keyword">with</span> torch.no_grad(): outputs = model(**batch) predictions = outputs.logits.argmax(dim=-<span class="hljs-number">1</span>) labels = batch[<span class="hljs-string">"labels"</span>] <span class="hljs-comment"># Necessary to pad predictions and labels for being gathered</span> predictions = accelerator.pad_across_processes(predictions, dim=<span class="hljs-number">1</span>, pad_index=-<span class="hljs-number">100</span>) labels = accelerator.pad_across_processes(labels, dim=<span class="hljs-number">1</span>, pad_index=-<span class="hljs-number">100</span>) predictions_gathered = accelerator.gather(predictions) labels_gathered = accelerator.gather(labels) true_predictions, true_labels = postprocess(predictions_gathered, labels_gathered) metric.add_batch(predictions=true_predictions, references=true_labels) results = metric.compute() <span class="hljs-built_in">print</span>( <span class="hljs-string">f"epoch <span class="hljs-subst">{epoch}</span>:"</span>, { key: results[<span class="hljs-string">f"overall_<span class="hljs-subst">{key}</span>"</span>] <span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> [<span class="hljs-string">"precision"</span>, <span class="hljs-string">"recall"</span>, <span class="hljs-string">"f1"</span>, <span class="hljs-string">"accuracy"</span>] }, ) <span class="hljs-comment"># Save and upload</span> accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) <span class="hljs-keyword">if</span> accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=<span class="hljs-string">f"Training in progress epoch <span class="hljs-subst">{epoch}</span>"</span>, blocking=<span class="hljs-literal">False</span> )</pre></div> <p>In case this is the first time you’re seeing a model saved with 🤗 Accelerate, let’s take a moment to inspect the three lines of code that go with it:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save)</pre></div> <p>The first line is self-explanatory: it tells all the processes to wait until everyone is at that stage before continuing. This is to make sure we have the same model in every process before saving. Then we grab the <code>unwrapped_model</code>, which is the base model we defined. The <code>accelerator.prepare()</code> method changes the model to work in distributed training, so it won’t have the <code>save_pretrained()</code> method anymore; the <code>accelerator.unwrap_model()</code> method undoes that step. Lastly, we call <code>save_pretrained()</code> but tell that method to use <code>accelerator.save()</code> instead of <code>torch.save()</code>.</p> <p>Once this is done, you should have a model that produces results pretty similar to the one trained with the <code>Trainer</code>. You can check the model we trained using this code at <a href="https://huggingface.co/huggingface-course/bert-finetuned-ner-accelerate" rel="nofollow"><em>huggingface-course/bert-finetuned-ner-accelerate</em></a>. And if you want to test out any tweaks to the training loop, you can directly implement them by editing the code shown above!</p> <h2 class="relative group"><a id="using-the-fine-tuned-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-the-fine-tuned-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using the fine-tuned model</span></h2> <p>We’ve already shown you how you can use the model we fine-tuned on the Model Hub with the inference widget. To use it locally in a <code>pipeline</code>, you just have to specify the proper model identifier:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline <span class="hljs-comment"># Replace this with your own checkpoint</span> model_checkpoint = <span class="hljs-string">"huggingface-course/bert-finetuned-ner"</span> token_classifier = pipeline( <span class="hljs-string">"token-classification"</span>, model=model_checkpoint, aggregation_strategy=<span class="hljs-string">"simple"</span> ) token_classifier(<span class="hljs-string">"My name is Sylvain and I work at Hugging Face in Brooklyn."</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'entity_group'</span>: <span class="hljs-string">'PER'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9988506</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Sylvain'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">11</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">18</span>}, {<span class="hljs-string">'entity_group'</span>: <span class="hljs-string">'ORG'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9647625</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Hugging Face'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">33</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">45</span>}, {<span class="hljs-string">'entity_group'</span>: <span class="hljs-string">'LOC'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9986118</span>, <span class="hljs-string">'word'</span>: <span class="hljs-string">'Brooklyn'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">49</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">57</span>}]</pre></div> <p>Great! Our model is working as well as the default one for this pipeline!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter7/1?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Introduction</a> <a href="/learn/nlp-course/chapter7/3?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Fine-tuning a masked language model<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Token classification&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;token-classification&quot;,&quot;url&quot;:&quot;#token-classification&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Preparing the data&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;preparing-the-data&quot;,&quot;url&quot;:&quot;#preparing-the-data&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;The CoNLL-2003 dataset&quot;,&quot;id&quot;:&quot;the-conll-2003-dataset&quot;,&quot;url&quot;:&quot;#the-conll-2003-dataset&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;processing-the-data&quot;,&quot;url&quot;:&quot;#processing-the-data&quot;}]},{&quot;title&quot;:&quot;Fine-tuning the model with the `Trainer` API&quot;,&quot;id&quot;:&quot;fine-tuning-the-model-with-the-trainer-api&quot;,&quot;url&quot;:&quot;#fine-tuning-the-model-with-the-trainer-api&quot;},{&quot;title&quot;:&quot;Fine-tuning the model with Keras&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;fine-tuning-the-model-with-keras&quot;,&quot;url&quot;:&quot;#fine-tuning-the-model-with-keras&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Data collation&quot;,&quot;id&quot;:&quot;data-collation&quot;,&quot;url&quot;:&quot;#data-collation&quot;},{&quot;title&quot;:&quot;Defining the model&quot;,&quot;id&quot;:&quot;defining-the-model&quot;,&quot;url&quot;:&quot;#defining-the-model&quot;},{&quot;title&quot;:&quot;Fine-tuning the model&quot;,&quot;id&quot;:&quot;fine-tuning-the-model&quot;,&quot;url&quot;:&quot;#fine-tuning-the-model&quot;},{&quot;title&quot;:&quot;Metrics&quot;,&quot;id&quot;:&quot;metrics&quot;,&quot;url&quot;:&quot;#metrics&quot;},{&quot;title&quot;:&quot;Defining the model&quot;,&quot;id&quot;:&quot;defining-the-model&quot;,&quot;url&quot;:&quot;#defining-the-model&quot;},{&quot;title&quot;:&quot;Fine-tuning the model&quot;,&quot;id&quot;:&quot;fine-tuning-the-model&quot;,&quot;url&quot;:&quot;#fine-tuning-the-model&quot;}]},{&quot;title&quot;:&quot;A custom training loop&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;a-custom-training-loop&quot;,&quot;url&quot;:&quot;#a-custom-training-loop&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Preparing everything for training&quot;,&quot;id&quot;:&quot;preparing-everything-for-training&quot;,&quot;url&quot;:&quot;#preparing-everything-for-training&quot;},{&quot;title&quot;:&quot;Training loop&quot;,&quot;id&quot;:&quot;training-loop&quot;,&quot;url&quot;:&quot;#training-loop&quot;}]},{&quot;title&quot;:&quot;Using the fine-tuned model&quot;,&quot;id&quot;:&quot;using-the-fine-tuned-model&quot;,&quot;url&quot;:&quot;#using-the-fine-tuned-model&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#token-classification" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-token-classification"><wbr>Token classification</a> <a href="#preparing-the-data" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preparing-the-data"><wbr>Preparing the data</a> <a href="#the-conll-2003-dataset" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-conll-2003-dataset"><wbr>The <wbr>CoNL<wbr>L-2003 dataset</a> <a href="#processing-the-data" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-processing-the-data"><wbr>Processing the data</a> <a href="#fine-tuning-the-model-with-the-trainer-api" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-the-model-with-the-trainer-api"><wbr>Fine-tuning the model with the `<wbr>Trainer` API</a> <a href="#data-collation" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-data-collation"><wbr>Data collation</a> <a href="#defining-the-model" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-defining-the-model"><wbr>Defining the model</a> <a href="#fine-tuning-the-model" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-the-model"><wbr>Fine-tuning the model</a> <a href="#metrics" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-metrics"><wbr>Metrics</a> <a href="#defining-the-model" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-defining-the-model"><wbr>Defining the model</a> <a href="#fine-tuning-the-model" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-the-model"><wbr>Fine-tuning the model</a> <a href="#a-custom-training-loop" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-a-custom-training-loop"><wbr>A custom training loop</a> <a href="#preparing-everything-for-training" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preparing-everything-for-training"><wbr>Preparing everything for training</a> <a href="#training-loop" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-loop"><wbr>Training loop</a> <a href="#using-the-fine-tuned-model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-the-fine-tuned-model"><wbr>Using the fine-tuned model</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter7/2" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter7/2"); } </script> <iframe name="__privateStripeMetricsController8190" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter7%2F2%3Ffw%3Dpt&amp;title=Token%20classification%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:28.281Z
Fine-tuning a masked language model - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter7/3?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#fine-tuning-a-masked-language-model)Fine-tuning a masked language model [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-7-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section3_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section3_pt.ipynb) For many NLP applications involving Transformer models, you can simply take a pretrained model from the Hugging Face Hub and fine-tune it directly on your data for the task at hand. Provided that the corpus used for pretraining is not too different from the corpus used for fine-tuning, transfer learning will usually produce good results. However, there are a few cases where you’ll want to first fine-tune the language models on your data, before training a task-specific head. For example, if your dataset contains legal contracts or scientific articles, a vanilla Transformer model like BERT will typically treat the domain-specific words in your corpus as rare tokens, and the resulting performance may be less than satisfactory. By fine-tuning the language model on in-domain data you can boost the performance of many downstream tasks, which means you usually only have to do this step once! This process of fine-tuning a pretrained language model on in-domain data is usually called _domain adaptation_. It was popularized in 2018 by [ULMFiT](https://arxiv.org/abs/1801.06146), which was one of the first neural architectures (based on LSTMs) to make transfer learning really work for NLP. An example of domain adaptation with ULMFiT is shown in the image below; in this section we’ll do something similar, but with a Transformer instead of an LSTM! ![ULMFiT.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/ulmfit.svg) ![ULMFiT.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/ulmfit-dark.svg) By the end of this section you’ll have a [masked language model](https://huggingface.co/huggingface-course/distilbert-base-uncased-finetuned-imdb?text=This+is+a+great+%5BMASK%5D.) on the Hub that can autocomplete sentences as shown below: Let’s dive in! 🙋 If the terms “masked language modeling” and “pretrained model” sound unfamiliar to you, go check out [Chapter 1](/course/chapter1), where we explain all these core concepts, complete with videos! ## [](#picking-a-pretrained-model-for-masked-language-modeling)Picking a pretrained model for masked language modeling To get started, let’s pick a suitable pretrained model for masked language modeling. As shown in the following screenshot, you can find a list of candidates by applying the “Fill-Mask” filter on the [Hugging Face Hub](https://huggingface.co/models?pipeline_tag=fill-mask&sort=downloads): ![Hub models.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/mlm-models.png) Although the BERT and RoBERTa family of models are the most downloaded, we’ll use a model called [DistilBERT](https://huggingface.co/distilbert-base-uncased) that can be trained much faster with little to no loss in downstream performance. This model was trained using a special technique called [_knowledge distillation_](https://en.wikipedia.org/wiki/Knowledge_distillation), where a large “teacher model” like BERT is used to guide the training of a “student model” that has far fewer parameters. An explanation of the details of knowledge distillation would take us too far afield in this section, but if you’re interested you can read all about it in [_Natural Language Processing with Transformers_](https://www.oreilly.com/library/view/natural-language-processing/9781098136789/) (colloquially known as the Transformers textbook). Let’s go ahead and download DistilBERT using the `AutoModelForMaskedLM` class: ``` from transformers import AutoModelForMaskedLM model_checkpoint = "distilbert-base-uncased" model = AutoModelForMaskedLM.from_pretrained(model_checkpoint)``` We can see how many parameters this model has by calling the `num_parameters()` method: ``` distilbert_num_parameters = model.num_parameters() / 1_000_000 print(f"'>>> DistilBERT number of parameters: {round(distilbert_num_parameters)}M'") print(f"'>>> BERT number of parameters: 110M'")``` ``` '>>> DistilBERT number of parameters: 67M' '>>> BERT number of parameters: 110M'``` With around 67 million parameters, DistilBERT is approximately two times smaller than the BERT base model, which roughly translates into a two-fold speedup in training — nice! Let’s now see what kinds of tokens this model predicts are the most likely completions of a small sample of text: ``` text = "This is a great [MASK]."``` As humans, we can imagine many possibilities for the `[MASK]` token, such as “day”, “ride”, or “painting”. For pretrained models, the predictions depend on the corpus the model was trained on, since it learns to pick up the statistical patterns present in the data. Like BERT, DistilBERT was pretrained on the [English Wikipedia](https://huggingface.co/datasets/wikipedia) and [BookCorpus](https://huggingface.co/datasets/bookcorpus) datasets, so we expect the predictions for `[MASK]` to reflect these domains. To predict the mask we need DistilBERT’s tokenizer to produce the inputs for the model, so let’s download that from the Hub as well: ``` from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)``` With a tokenizer and a model, we can now pass our text example to the model, extract the logits, and print out the top 5 candidates: ``` import torch inputs = tokenizer(text, return_tensors="pt") token_logits = model(**inputs).logits mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1] mask_token_logits = token_logits[0, mask_token_index, :] top_5_tokens = torch.topk(mask_token_logits, 5, dim=1).indices[0].tolist() for token in top_5_tokens: print(f"'>>> {text.replace(tokenizer.mask_token, tokenizer.decode([token]))}'")``` ``` '>>> This is a great deal.' '>>> This is a great success.' '>>> This is a great adventure.' '>>> This is a great idea.' '>>> This is a great feat.'``` We can see from the outputs that the model’s predictions refer to everyday terms, which is perhaps not surprising given the foundation of English Wikipedia. Let’s see how we can change this domain to something a bit more niche — highly polarized movie reviews! ## [](#the-dataset)The dataset To showcase domain adaptation, we’ll use the famous [Large Movie Review Dataset](https://huggingface.co/datasets/imdb) (or IMDb for short), which is a corpus of movie reviews that is often used to benchmark sentiment analysis models. By fine-tuning DistilBERT on this corpus, we expect the language model will adapt its vocabulary from the factual data of Wikipedia that it was pretrained on to the more subjective elements of movie reviews. We can get the data from the Hugging Face Hub with the `load_dataset()` function from 🤗 Datasets: ``` from datasets import load_dataset imdb_dataset = load_dataset("imdb") imdb_dataset``` ``` DatasetDict({ train: Dataset({ features: ['text', 'label'], num_rows: 25000 }) test: Dataset({ features: ['text', 'label'], num_rows: 25000 }) unsupervised: Dataset({ features: ['text', 'label'], num_rows: 50000 }) })``` We can see that the `train` and `test` splits each consist of 25,000 reviews, while there is an unlabeled split called `unsupervised` that contains 50,000 reviews. Let’s take a look at a few samples to get an idea of what kind of text we’re dealing with. As we’ve done in previous chapters of the course, we’ll chain the `Dataset.shuffle()` and `Dataset.select()` functions to create a random sample: ``` sample = imdb_dataset["train"].shuffle(seed=42).select(range(3)) for row in sample: print(f"\n'>>> Review: {row['text']}'") print(f"'>>> Label: {row['label']}'")``` ``` '>>> Review: This is your typical Priyadarshan movie--a bunch of loony characters out on some silly mission. His signature climax has the entire cast of the film coming together and fighting each other in some crazy moshpit over hidden money. Whether it is a winning lottery ticket in Malamaal Weekly, black money in Hera Pheri, "kodokoo" in Phir Hera Pheri, etc., etc., the director is becoming ridiculously predictable. Don\'t get me wrong; as clichéd and preposterous his movies may be, I usually end up enjoying the comedy. However, in most his previous movies there has actually been some good humor, (Hungama and Hera Pheri being noteworthy ones). Now, the hilarity of his films is fading as he is using the same formula over and over again.<br /><br />Songs are good. Tanushree Datta looks awesome. Rajpal Yadav is irritating, and Tusshar is not a whole lot better. Kunal Khemu is OK, and Sharman Joshi is the best.' '>>> Label: 0' '>>> Review: Okay, the story makes no sense, the characters lack any dimensionally, the best dialogue is ad-libs about the low quality of movie, the cinematography is dismal, and only editing saves a bit of the muddle, but Sam" Peckinpah directed the film. Somehow, his direction is not enough. For those who appreciate Peckinpah and his great work, this movie is a disappointment. Even a great cast cannot redeem the time the viewer wastes with this minimal effort.<br /><br />The proper response to the movie is the contempt that the director San Peckinpah, James Caan, Robert Duvall, Burt Young, Bo Hopkins, Arthur Hill, and even Gig Young bring to their work. Watch the great Peckinpah films. Skip this mess.' '>>> Label: 0' '>>> Review: I saw this movie at the theaters when I was about 6 or 7 years old. I loved it then, and have recently come to own a VHS version. <br /><br />My 4 and 6 year old children love this movie and have been asking again and again to watch it. <br /><br />I have enjoyed watching it again too. Though I have to admit it is not as good on a little TV.<br /><br />I do not have older children so I do not know what they would think of it. <br /><br />The songs are very cute. My daughter keeps singing them over and over.<br /><br />Hope this helps.' '>>> Label: 1'``` Yep, these are certainly movie reviews, and if you’re old enough you may even understand the comment in the last review about owning a VHS version 😜! Although we won’t need the labels for language modeling, we can already see that a `0` denotes a negative review, while a `1` corresponds to a positive one. ✏️ **Try it out!** Create a random sample of the `unsupervised` split and verify that the labels are neither `0` nor `1`. While you’re at it, you could also check that the labels in the `train` and `test` splits are indeed `0` or `1` — this is a useful sanity check that every NLP practitioner should perform at the start of a new project! Now that we’ve had a quick look at the data, let’s dive into preparing it for masked language modeling. As we’ll see, there are some additional steps that one needs to take compared to the sequence classification tasks we saw in [Chapter 3](/course/chapter3). Let’s go! ## [](#preprocessing-the-data)Preprocessing the data For both auto-regressive and masked language modeling, a common preprocessing step is to concatenate all the examples and then split the whole corpus into chunks of equal size. This is quite different from our usual approach, where we simply tokenize individual examples. Why concatenate everything together? The reason is that individual examples might get truncated if they’re too long, and that would result in losing information that might be useful for the language modeling task! So to get started, we’ll first tokenize our corpus as usual, but _without_ setting the `truncation=True` option in our tokenizer. We’ll also grab the word IDs if they are available ((which they will be if we’re using a fast tokenizer, as described in [Chapter 6](/course/chapter6/3)), as we will need them later on to do whole word masking. We’ll wrap this in a simple function, and while we’re at it we’ll remove the `text` and `label` columns since we don’t need them any longer: ``` def tokenize_function(examples): result = tokenizer(examples["text"]) if tokenizer.is_fast: result["word_ids"] = [result.word_ids(i) for i in range(len(result["input_ids"]))] return result tokenized_datasets = imdb_dataset.map( tokenize_function, batched=True, remove_columns=["text", "label"] ) tokenized_datasets``` ``` DatasetDict({ train: Dataset({ features: ['attention_mask', 'input_ids', 'word_ids'], num_rows: 25000 }) test: Dataset({ features: ['attention_mask', 'input_ids', 'word_ids'], num_rows: 25000 }) unsupervised: Dataset({ features: ['attention_mask', 'input_ids', 'word_ids'], num_rows: 50000 }) })``` Since DistilBERT is a BERT-like model, we can see that the encoded texts consist of the `input_ids` and `attention_mask` that we’ve seen in other chapters, as well as the `word_ids` we added. Now that we’ve tokenized our movie reviews, the next step is to group them all together and split the result into chunks. But how big should these chunks be? This will ultimately be determined by the amount of GPU memory that you have available, but a good starting point is to see what the model’s maximum context size is. This can be inferred by inspecting the `model_max_length` attribute of the tokenizer: ``` tokenizer.model_max_length``` This value is derived from the _tokenizer\_config.json_ file associated with a checkpoint; in this case we can see that the context size is 512 tokens, just like with BERT. ✏️ **Try it out!** Some Transformer models, like [BigBird](https://huggingface.co/google/bigbird-roberta-base) and [Longformer](hf.co/allenai/longformer-base-4096), have a much longer context length than BERT and other early Transformer models. Instantiate the tokenizer for one of these checkpoints and verify that the `model_max_length` agrees with what’s quoted on its model card. So, in order to run our experiments on GPUs like those found on Google Colab, we’ll pick something a bit smaller that can fit in memory: Note that using a small chunk size can be detrimental in real-world scenarios, so you should use a size that corresponds to the use case you will apply your model to. Now comes the fun part. To show how the concatenation works, let’s take a few reviews from our tokenized training set and print out the number of tokens per review: ``` tokenized_samples = tokenized_datasets["train"][:3] for idx, sample in enumerate(tokenized_samples["input_ids"]): print(f"'>>> Review {idx} length: {len(sample)}'")``` ``` '>>> Review 0 length: 200' '>>> Review 1 length: 559' '>>> Review 2 length: 192'``` We can then concatenate all these examples with a simple dictionary comprehension, as follows: ``` concatenated_examples = { k: sum(tokenized_samples[k], []) for k in tokenized_samples.keys() } total_length = len(concatenated_examples["input_ids"]) print(f"'>>> Concatenated reviews length: {total_length}'")``` ``` '>>> Concatenated reviews length: 951'``` Great, the total length checks out — so now let’s split the concatenated reviews into chunks of the size given by `block_size`. To do so, we iterate over the features in `concatenated_examples` and use a list comprehension to create slices of each feature. The result is a dictionary of chunks for each feature: ``` chunks = { k: [t[i : i + chunk_size] for i in range(0, total_length, chunk_size)] for k, t in concatenated_examples.items() } for chunk in chunks["input_ids"]: print(f"'>>> Chunk length: {len(chunk)}'")``` ``` '>>> Chunk length: 128' '>>> Chunk length: 128' '>>> Chunk length: 128' '>>> Chunk length: 128' '>>> Chunk length: 128' '>>> Chunk length: 128' '>>> Chunk length: 128' '>>> Chunk length: 55'``` As you can see in this example, the last chunk will generally be smaller than the maximum chunk size. There are two main strategies for dealing with this: - Drop the last chunk if it’s smaller than `chunk_size`. - Pad the last chunk until its length equals `chunk_size`. We’ll take the first approach here, so let’s wrap all of the above logic in a single function that we can apply to our tokenized datasets: ``` def group_texts(examples): concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()} total_length = len(concatenated_examples[list(examples.keys())[0]]) total_length = (total_length // chunk_size) * chunk_size result = { k: [t[i : i + chunk_size] for i in range(0, total_length, chunk_size)] for k, t in concatenated_examples.items() } result["labels"] = result["input_ids"].copy() return result``` Note that in the last step of `group_texts()` we create a new `labels` column which is a copy of the `input_ids` one. As we’ll see shortly, that’s because in masked language modeling the objective is to predict randomly masked tokens in the input batch, and by creating a `labels` column we provide the ground truth for our language model to learn from. Let’s now apply `group_texts()` to our tokenized datasets using our trusty `Dataset.map()` function: ``` lm_datasets = tokenized_datasets.map(group_texts, batched=True) lm_datasets``` ``` DatasetDict({ train: Dataset({ features: ['attention_mask', 'input_ids', 'labels', 'word_ids'], num_rows: 61289 }) test: Dataset({ features: ['attention_mask', 'input_ids', 'labels', 'word_ids'], num_rows: 59905 }) unsupervised: Dataset({ features: ['attention_mask', 'input_ids', 'labels', 'word_ids'], num_rows: 122963 }) })``` You can see that grouping and then chunking the texts has produced many more examples than our original 25,000 for the `train` and `test` splits. That’s because we now have examples involving _contiguous tokens_ that span across multiple examples from the original corpus. You can see this explicitly by looking for the special `[SEP]` and `[CLS]` tokens in one of the chunks: ``` tokenizer.decode(lm_datasets["train"][1]["input_ids"])``` ``` ".... at.......... high. a classic line : inspector : i'm here to sack one of your teachers. student : welcome to bromwell high. i expect that many adults of my age think that bromwell high is far fetched. what a pity that it isn't! [SEP] [CLS] homelessness ( or houselessness as george carlin stated ) has been an issue for years but never a plan to help those on the street that were once considered human who did everything from going to school, work, or vote for the matter. most people think of the homeless"``` In this example you can see two overlapping movie reviews, one about a high school movie and the other about homelessness. Let’s also check out what the labels look like for masked language modeling: ``` tokenizer.decode(lm_datasets["train"][1]["labels"])``` ``` ".... at.......... high. a classic line : inspector : i'm here to sack one of your teachers. student : welcome to bromwell high. i expect that many adults of my age think that bromwell high is far fetched. what a pity that it isn't! [SEP] [CLS] homelessness ( or houselessness as george carlin stated ) has been an issue for years but never a plan to help those on the street that were once considered human who did everything from going to school, work, or vote for the matter. most people think of the homeless"``` As expected from our `group_texts()` function above, this looks identical to the decoded `input_ids` — but then how can our model possibly learn anything? We’re missing a key step: inserting `[MASK]` tokens at random positions in the inputs! Let’s see how we can do this on the fly during fine-tuning using a special data collator. ## [](#fine-tuning-distilbert-with-the-trainer-api)Fine-tuning DistilBERT with the `Trainer` API Fine-tuning a masked language model is almost identical to fine-tuning a sequence classification model, like we did in [Chapter 3](/course/chapter3). The only difference is that we need a special data collator that can randomly mask some of the tokens in each batch of texts. Fortunately, 🤗 Transformers comes prepared with a dedicated `DataCollatorForLanguageModeling` for just this task. We just have to pass it the tokenizer and an `mlm_probability` argument that specifies what fraction of the tokens to mask. We’ll pick 15%, which is the amount used for BERT and a common choice in the literature: ``` from transformers import DataCollatorForLanguageModeling data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm_probability=0.15)``` To see how the random masking works, let’s feed a few examples to the data collator. Since it expects a list of `dict`s, where each `dict` represents a single chunk of contiguous text, we first iterate over the dataset before feeding the batch to the collator. We remove the `"word_ids"` key for this data collator as it does not expect it: ``` samples = [lm_datasets["train"][i] for i in range(2)] for sample in samples: _ = sample.pop("word_ids") for chunk in data_collator(samples)["input_ids"]: print(f"\n'>>> {tokenizer.decode(chunk)}'")``` ``` '>>> [CLS] bromwell [MASK] is a cartoon comedy. it ran at the same [MASK] as some other [MASK] about school life, [MASK] as " teachers ". [MASK] [MASK] [MASK] in the teaching [MASK] lead [MASK] to believe that bromwell high\'[MASK] satire is much closer to reality than is " teachers ". the scramble [MASK] [MASK] financially, the [MASK]ful students whogn [MASK] right through [MASK] pathetic teachers\'pomp, the pettiness of the whole situation, distinction remind me of the schools i knew and their students. when i saw [MASK] episode in [MASK] a student repeatedly tried to burn down the school, [MASK] immediately recalled. [MASK]...' '>>> .... at.. [MASK]... [MASK]... high. a classic line plucked inspector : i\'[MASK] here to [MASK] one of your [MASK]. student : welcome to bromwell [MASK]. i expect that many adults of my age think that [MASK]mwell [MASK] is [MASK] fetched. what a pity that it isn\'t! [SEP] [CLS] [MASK]ness ( or [MASK]lessness as george 宇in stated )公 been an issue for years but never [MASK] plan to help those on the street that were once considered human [MASK] did everything from going to school, [MASK], [MASK] vote for the matter. most people think [MASK] the homeless'``` Nice, it worked! We can see that the `[MASK]` token has been randomly inserted at various locations in our text. These will be the tokens which our model will have to predict during training — and the beauty of the data collator is that it will randomize the `[MASK]` insertion with every batch! ✏️ **Try it out!** Run the code snippet above several times to see the random masking happen in front of your very eyes! Also replace the `tokenizer.decode()` method with `tokenizer.convert_ids_to_tokens()` to see that sometimes a single token from a given word is masked, and not the others. One side effect of random masking is that our evaluation metrics will not be deterministic when using the `Trainer`, since we use the same data collator for the training and test sets. We’ll see later, when we look at fine-tuning with 🤗 Accelerate, how we can use the flexibility of a custom evaluation loop to freeze the randomness. When training models for masked language modeling, one technique that can be used is to mask whole words together, not just individual tokens. This approach is called _whole word masking_. If we want to use whole word masking, we will need to build a data collator ourselves. A data collator is just a function that takes a list of samples and converts them into a batch, so let’s do this now! We’ll use the word IDs computed earlier to make a map between word indices and the corresponding tokens, then randomly decide which words to mask and apply that mask on the inputs. Note that the labels are all `-100` except for the ones corresponding to mask words. ``` import collections import numpy as np from transformers import default_data_collator wwm_probability = 0.2 def whole_word_masking_data_collator(features): for feature in features: word_ids = feature.pop("word_ids") mapping = collections.defaultdict(list) current_word_index = -1 current_word = None for idx, word_id in enumerate(word_ids): if word_id is not None: if word_id != current_word: current_word = word_id current_word_index += 1 mapping[current_word_index].append(idx) mask = np.random.binomial(1, wwm_probability, (len(mapping),)) input_ids = feature["input_ids"] labels = feature["labels"] new_labels = [-100] * len(labels) for word_id in np.where(mask)[0]: word_id = word_id.item() for idx in mapping[word_id]: new_labels[idx] = labels[idx] input_ids[idx] = tokenizer.mask_token_id feature["labels"] = new_labels return default_data_collator(features)``` Next, we can try it on the same samples as before: ``` samples = [lm_datasets["train"][i] for i in range(2)] batch = whole_word_masking_data_collator(samples) for chunk in batch["input_ids"]: print(f"\n'>>> {tokenizer.decode(chunk)}'")``` ``` '>>> [CLS] bromwell high is a cartoon comedy [MASK] it ran at the same time as some other programs about school life, such as " teachers ". my 35 years in the teaching profession lead me to believe that bromwell high\'s satire is much closer to reality than is " teachers ". the scramble to survive financially, the insightful students who can see right through their pathetic teachers\'pomp, the pettiness of the whole situation, all remind me of the schools i knew and their students. when i saw the episode in which a student repeatedly tried to burn down the school, i immediately recalled.....' '>>> .... [MASK] [MASK] [MASK] [MASK]....... high. a classic line : inspector : i\'m here to sack one of your teachers. student : welcome to bromwell high. i expect that many adults of my age think that bromwell high is far fetched. what a pity that it isn\'t! [SEP] [CLS] homelessness ( or houselessness as george carlin stated ) has been an issue for years but never a plan to help those on the street that were once considered human who did everything from going to school, work, or vote for the matter. most people think of the homeless'``` ✏️ **Try it out!** Run the code snippet above several times to see the random masking happen in front of your very eyes! Also replace the `tokenizer.decode()` method with `tokenizer.convert_ids_to_tokens()` to see that the tokens from a given word are always masked together. Now that we have two data collators, the rest of the fine-tuning steps are standard. Training can take a while on Google Colab if you’re not lucky enough to score a mythical P100 GPU 😭, so we’ll first downsample the size of the training set to a few thousand examples. Don’t worry, we’ll still get a pretty decent language model! A quick way to downsample a dataset in 🤗 Datasets is via the `Dataset.train_test_split()` function that we saw in [Chapter 5](/course/chapter5): ``` train_size = 10_000 test_size = int(0.1 * train_size) downsampled_dataset = lm_datasets["train"].train_test_split( train_size=train_size, test_size=test_size, seed=42 ) downsampled_dataset``` ``` DatasetDict({ train: Dataset({ features: ['attention_mask', 'input_ids', 'labels', 'word_ids'], num_rows: 10000 }) test: Dataset({ features: ['attention_mask', 'input_ids', 'labels', 'word_ids'], num_rows: 1000 }) })``` This has automatically created new `train` and `test` splits, with the training set size set to 10,000 examples and the validation set to 10% of that — feel free to increase this if you have a beefy GPU! The next thing we need to do is log in to the Hugging Face Hub. If you’re running this code in a notebook, you can do so with the following utility function: ``` from huggingface_hub import notebook_login notebook_login()``` which will display a widget where you can enter your credentials. Alternatively, you can run: in your favorite terminal and log in there. Once we’re logged in, we can specify the arguments for the `Trainer`: ``` from transformers import TrainingArguments batch_size = 64 logging_steps = len(downsampled_dataset["train"]) // batch_size model_name = model_checkpoint.split("/")[-1] training_args = TrainingArguments( output_dir=f"{model_name}-finetuned-imdb", overwrite_output_dir=True, evaluation_strategy="epoch", learning_rate=2e-5, weight_decay=0.01, per_device_train_batch_size=batch_size, per_device_eval_batch_size=batch_size, push_to_hub=True, fp16=True, logging_steps=logging_steps, )``` Here we tweaked a few of the default options, including `logging_steps` to ensure we track the training loss with each epoch. We’ve also used `fp16=True` to enable mixed-precision training, which gives us another boost in speed. By default, the `Trainer` will remove any columns that are not part of the model’s `forward()` method. This means that if you’re using the whole word masking collator, you’ll also need to set `remove_unused_columns=False` to ensure we don’t lose the `word_ids` column during training. Note that you can specify the name of the repository you want to push to with the `hub_model_id` argument (in particular, you will have to use this argument to push to an organization). For instance, when we pushed the model to the [`huggingface-course` organization](https://huggingface.co/huggingface-course), we added `hub_model_id="huggingface-course/distilbert-finetuned-imdb"` to `TrainingArguments`. By default, the repository used will be in your namespace and named after the output directory you set, so in our case it will be `"lewtun/distilbert-finetuned-imdb"`. We now have all the ingredients to instantiate the `Trainer`. Here we just use the standard `data_collator`, but you can try the whole word masking collator and compare the results as an exercise: ``` from transformers import Trainer trainer = Trainer( model=model, args=training_args, train_dataset=downsampled_dataset["train"], eval_dataset=downsampled_dataset["test"], data_collator=data_collator, tokenizer=tokenizer, )``` We’re now ready to run `trainer.train()` — but before doing so let’s briefly look at _perplexity_, which is a common metric to evaluate the performance of language models. ### [](#perplexity-for-language-models)Perplexity for language models Unlike other tasks like text classification or question answering where we’re given a labeled corpus to train on, with language modeling we don’t have any explicit labels. So how do we determine what makes a good language model? Like with the autocorrect feature in your phone, a good language model is one that assigns high probabilities to sentences that are grammatically correct, and low probabilities to nonsense sentences. To give you a better idea of what this looks like, you can find whole sets of “autocorrect fails” online, where the model in a person’s phone has produced some rather funny (and often inappropriate) completions! Assuming our test set consists mostly of sentences that are grammatically correct, then one way to measure the quality of our language model is to calculate the probabilities it assigns to the next word in all the sentences of the test set. High probabilities indicates that the model is not “surprised” or “perplexed” by the unseen examples, and suggests it has learned the basic patterns of grammar in the language. There are various mathematical definitions of perplexity, but the one we’ll use defines it as the exponential of the cross-entropy loss. Thus, we can calculate the perplexity of our pretrained model by using the `Trainer.evaluate()` function to compute the cross-entropy loss on the test set and then taking the exponential of the result: ``` import math eval_results = trainer.evaluate() print(f">>> Perplexity: {math.exp(eval_results['eval_loss']):.2f}")``` A lower perplexity score means a better language model, and we can see here that our starting model has a somewhat large value. Let’s see if we can lower it by fine-tuning! To do that, we first run the training loop: and then compute the resulting perplexity on the test set as before: ``` eval_results = trainer.evaluate() print(f">>> Perplexity: {math.exp(eval_results['eval_loss']):.2f}")``` Nice — this is quite a reduction in perplexity, which tells us the model has learned something about the domain of movie reviews! Once training is finished, we can push the model card with the training information to the Hub (the checkpoints are saved during training itself): ✏️ **Your turn!** Run the training above after changing the data collator to the whole word masking collator. Do you get better results? In our use case we didn’t need to do anything special with the training loop, but in some cases you might need to implement some custom logic. For these applications, you can use 🤗 Accelerate — let’s take a look! ## [](#fine-tuning-distilbert-with-accelerate)Fine-tuning DistilBERT with 🤗 Accelerate As we saw with the `Trainer`, fine-tuning a masked language model is very similar to the text classification example from [Chapter 3](/course/chapter3). In fact, the only subtlety is the use of a special data collator, and we’ve already covered that earlier in this section! However, we saw that `DataCollatorForLanguageModeling` also applies random masking with each evaluation, so we’ll see some fluctuations in our perplexity scores with each training run. One way to eliminate this source of randomness is to apply the masking _once_ on the whole test set, and then use the default data collator in 🤗 Transformers to collect the batches during evaluation. To see how this works, let’s implement a simple function that applies the masking on a batch, similar to our first encounter with `DataCollatorForLanguageModeling`: ``` def insert_random_mask(batch): features = [dict(zip(batch, t)) for t in zip(*batch.values())] masked_inputs = data_collator(features) return {"masked_" + k: v.numpy() for k, v in masked_inputs.items()}``` Next, we’ll apply this function to our test set and drop the unmasked columns so we can replace them with the masked ones. You can use whole word masking by replacing the `data_collator` above with the appropriate one, in which case you should remove the first line here: ``` downsampled_dataset = downsampled_dataset.remove_columns(["word_ids"]) eval_dataset = downsampled_dataset["test"].map( insert_random_mask, batched=True, remove_columns=downsampled_dataset["test"].column_names, ) eval_dataset = eval_dataset.rename_columns( { "masked_input_ids": "input_ids", "masked_attention_mask": "attention_mask", "masked_labels": "labels", } )``` We can then set up the dataloaders as usual, but we’ll use the `default_data_collator` from 🤗 Transformers for the evaluation set: ``` from torch.utils.data import DataLoader from transformers import default_data_collator batch_size = 64 train_dataloader = DataLoader( downsampled_dataset["train"], shuffle=True, batch_size=batch_size, collate_fn=data_collator, ) eval_dataloader = DataLoader( eval_dataset, batch_size=batch_size, collate_fn=default_data_collator )``` Form here, we follow the standard steps with 🤗 Accelerate. The first order of business is to load a fresh version of the pretrained model: ``` model = AutoModelForMaskedLM.from_pretrained(model_checkpoint)``` Then we need to specify the optimizer; we’ll use the standard `AdamW`: ``` from torch.optim import AdamW optimizer = AdamW(model.parameters(), lr=5e-5)``` With these objects, we can now prepare everything for training with the `Accelerator` object: ``` from accelerate import Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )``` Now that our model, optimizer, and dataloaders are configured, we can specify the learning rate scheduler as follows: ``` from transformers import get_scheduler num_train_epochs = 3 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, )``` There is just one last thing to do before training: create a model repository on the Hugging Face Hub! We can use the 🤗 Hub library to first generate the full name of our repo: ``` from huggingface_hub import get_full_repo_name model_name = "distilbert-base-uncased-finetuned-imdb-accelerate" repo_name = get_full_repo_name(model_name) repo_name``` ``` 'lewtun/distilbert-base-uncased-finetuned-imdb-accelerate'``` then create and clone the repository using the `Repository` class from 🤗 Hub: ``` from huggingface_hub import Repository output_dir = model_name repo = Repository(output_dir, clone_from=repo_name)``` With that done, it’s just a simple matter of writing out the full training and evaluation loop: ``` from tqdm.auto import tqdm import torch import math progress_bar = tqdm(range(num_training_steps)) for epoch in range(num_train_epochs): model.train() for batch in train_dataloader: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) model.eval() losses = [] for step, batch in enumerate(eval_dataloader): with torch.no_grad(): outputs = model(**batch) loss = outputs.loss losses.append(accelerator.gather(loss.repeat(batch_size))) losses = torch.cat(losses) losses = losses[: len(eval_dataset)] try: perplexity = math.exp(torch.mean(losses)) except OverflowError: perplexity = float("inf") print(f">>> Epoch {epoch}: Perplexity: {perplexity}") accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=f"Training in progress epoch {epoch}", blocking=False )``` ``` >>> Epoch 0: Perplexity: 11.397545307900472 >>> Epoch 1: Perplexity: 10.904909330983092 >>> Epoch 2: Perplexity: 10.729503505340409``` Cool, we’ve been able to evaluate perplexity with each epoch and ensure that multiple training runs are reproducible! ## [](#using-our-fine-tuned-model)Using our fine-tuned model You can interact with your fine-tuned model either by using its widget on the Hub or locally with the `pipeline` from 🤗 Transformers. Let’s use the latter to download our model using the `fill-mask` pipeline: ``` from transformers import pipeline mask_filler = pipeline( "fill-mask", model="huggingface-course/distilbert-base-uncased-finetuned-imdb" )``` We can then feed the pipeline our sample text of “This is a great \[MASK\]” and see what the top 5 predictions are: ``` preds = mask_filler(text) for pred in preds: print(f">>> {pred['sequence']}")``` ``` '>>> this is a great movie.' '>>> this is a great film.' '>>> this is a great story.' '>>> this is a great movies.' '>>> this is a great character.'``` Neat — our model has clearly adapted its weights to predict words that are more strongly associated with movies! This wraps up our first experiment with training a language model. In [section 6](/course/en/chapter7/section6) you’ll learn how to train an auto-regressive model like GPT-2 from scratch; head over there if you’d like to see how you can pretrain your very own Transformer model! ✏️ **Try it out!** To quantify the benefits of domain adaptation, fine-tune a classifier on the IMDb labels for both the pretrained and fine-tuned DistilBERT checkpoints. If you need a refresher on text classification, check out [Chapter 3](/course/chapter3).
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Fine-tuning a masked language model - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter7/3"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Fine-tuning a masked language model - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;fine-tuning-a-masked-language-model&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;picking-a-pretrained-model-for-masked-language-modeling&quot;,&quot;title&quot;:&quot;Picking a pretrained model for masked language modeling&quot;},{&quot;local&quot;:&quot;the-dataset&quot;,&quot;title&quot;:&quot;The dataset&quot;},{&quot;local&quot;:&quot;preprocessing-the-data&quot;,&quot;title&quot;:&quot;Preprocessing the data&quot;},{&quot;local&quot;:&quot;fine-tuning-distilbert-with-the-trainer-api&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;perplexity-for-language-models&quot;,&quot;title&quot;:&quot;Perplexity for language models&quot;}],&quot;title&quot;:&quot;Fine-tuning DistilBERT with the `Trainer` API&quot;},{&quot;local&quot;:&quot;fine-tuning-distilbert-with-accelerate&quot;,&quot;title&quot;:&quot;Fine-tuning DistilBERT with 🤗 Accelerate&quot;},{&quot;local&quot;:&quot;using-our-fine-tuned-model&quot;,&quot;title&quot;:&quot;Using our fine-tuned model&quot;}],&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter7/3&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Fine-tuning a masked language model</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/2?fw=pt">Token classification </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter7/3?fw=pt">Fine-tuning a masked language model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/4?fw=pt">Translation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/5?fw=pt">Summarization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/6?fw=pt">Training a causal language model from scratch </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/7?fw=pt">Question answering </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/8?fw=pt">Mastering NLP </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="fine-tuning-a-masked-language-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-a-masked-language-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning a masked language model</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-7-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section3_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section3_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>For many NLP applications involving Transformer models, you can simply take a pretrained model from the Hugging Face Hub and fine-tune it directly on your data for the task at hand. Provided that the corpus used for pretraining is not too different from the corpus used for fine-tuning, transfer learning will usually produce good results.</p> <p>However, there are a few cases where you’ll want to first fine-tune the language models on your data, before training a task-specific head. For example, if your dataset contains legal contracts or scientific articles, a vanilla Transformer model like BERT will typically treat the domain-specific words in your corpus as rare tokens, and the resulting performance may be less than satisfactory. By fine-tuning the language model on in-domain data you can boost the performance of many downstream tasks, which means you usually only have to do this step once!</p> <p>This process of fine-tuning a pretrained language model on in-domain data is usually called <em>domain adaptation</em>. It was popularized in 2018 by <a href="https://arxiv.org/abs/1801.06146" rel="nofollow">ULMFiT</a>, which was one of the first neural architectures (based on LSTMs) to make transfer learning really work for NLP. An example of domain adaptation with ULMFiT is shown in the image below; in this section we’ll do something similar, but with a Transformer instead of an LSTM!</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/ulmfit.svg" alt="ULMFiT."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/ulmfit-dark.svg" alt="ULMFiT."></div> <p>By the end of this section you’ll have a <a href="https://huggingface.co/huggingface-course/distilbert-base-uncased-finetuned-imdb?text=This+is+a+great+%5BMASK%5D." rel="nofollow">masked language model</a> on the Hub that can autocomplete sentences as shown below:</p> <iframe src="https://course-demos-distilbert-base-uncased-finetuned-imdb.hf.space" frameborder="0" height="300" title="Gradio app" class="block dark:hidden container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>Let’s dive in!</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/mqElG5QJWUg" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>🙋 If the terms “masked language modeling” and “pretrained model” sound unfamiliar to you, go check out <a href="/course/chapter1">Chapter 1</a>, where we explain all these core concepts, complete with videos!</p></div> <h2 class="relative group"><a id="picking-a-pretrained-model-for-masked-language-modeling" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#picking-a-pretrained-model-for-masked-language-modeling"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Picking a pretrained model for masked language modeling</span></h2> <p>To get started, let’s pick a suitable pretrained model for masked language modeling. As shown in the following screenshot, you can find a list of candidates by applying the “Fill-Mask” filter on the <a href="https://huggingface.co/models?pipeline_tag=fill-mask&amp;sort=downloads" rel="nofollow">Hugging Face Hub</a>:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/mlm-models.png" alt="Hub models." width="80%"></div> <p>Although the BERT and RoBERTa family of models are the most downloaded, we’ll use a model called <a href="https://huggingface.co/distilbert-base-uncased" rel="nofollow">DistilBERT</a> that can be trained much faster with little to no loss in downstream performance. This model was trained using a special technique called <a href="https://en.wikipedia.org/wiki/Knowledge_distillation" rel="nofollow"><em>knowledge distillation</em></a>, where a large “teacher model” like BERT is used to guide the training of a “student model” that has far fewer parameters. An explanation of the details of knowledge distillation would take us too far afield in this section, but if you’re interested you can read all about it in <a href="https://www.oreilly.com/library/view/natural-language-processing/9781098136789/" rel="nofollow"><em>Natural Language Processing with Transformers</em></a> (colloquially known as the Transformers textbook).</p> <p>Let’s go ahead and download DistilBERT using the <code>AutoModelForMaskedLM</code> class:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForMaskedLM model_checkpoint = <span class="hljs-string">"distilbert-base-uncased"</span> model = AutoModelForMaskedLM.from_pretrained(model_checkpoint)</pre></div> <p>We can see how many parameters this model has by calling the <code>num_parameters()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>distilbert_num_parameters = model.num_parameters() / <span class="hljs-number">1_000_000</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">f"'&gt;&gt;&gt; DistilBERT number of parameters: <span class="hljs-subst">{<span class="hljs-built_in">round</span>(distilbert_num_parameters)}</span>M'"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"'&gt;&gt;&gt; BERT number of parameters: 110M'"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt;&gt; DistilBERT number of parameters: 67M'</span> <span class="hljs-string">'&gt;&gt;&gt; BERT number of parameters: 110M'</span></pre></div> <p>With around 67 million parameters, DistilBERT is approximately two times smaller than the BERT base model, which roughly translates into a two-fold speedup in training — nice! Let’s now see what kinds of tokens this model predicts are the most likely completions of a small sample of text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>text = <span class="hljs-string">"This is a great [MASK]."</span></pre></div> <p>As humans, we can imagine many possibilities for the <code>[MASK]</code> token, such as “day”, “ride”, or “painting”. For pretrained models, the predictions depend on the corpus the model was trained on, since it learns to pick up the statistical patterns present in the data. Like BERT, DistilBERT was pretrained on the <a href="https://huggingface.co/datasets/wikipedia" rel="nofollow">English Wikipedia</a> and <a href="https://huggingface.co/datasets/bookcorpus" rel="nofollow">BookCorpus</a> datasets, so we expect the predictions for <code>[MASK]</code> to reflect these domains. To predict the mask we need DistilBERT’s tokenizer to produce the inputs for the model, so let’s download that from the Hub as well:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)</pre></div> <p>With a tokenizer and a model, we can now pass our text example to the model, extract the logits, and print out the top 5 candidates:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch inputs = tokenizer(text, return_tensors=<span class="hljs-string">"pt"</span>) token_logits = model(**inputs).logits <span class="hljs-comment"># Find the location of [MASK] and extract its logits</span> mask_token_index = torch.where(inputs[<span class="hljs-string">"input_ids"</span>] == tokenizer.mask_token_id)[<span class="hljs-number">1</span>] mask_token_logits = token_logits[<span class="hljs-number">0</span>, mask_token_index, :] <span class="hljs-comment"># Pick the [MASK] candidates with the highest logits</span> top_5_tokens = torch.topk(mask_token_logits, <span class="hljs-number">5</span>, dim=<span class="hljs-number">1</span>).indices[<span class="hljs-number">0</span>].tolist() <span class="hljs-keyword">for</span> token <span class="hljs-keyword">in</span> top_5_tokens: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"'&gt;&gt;&gt; <span class="hljs-subst">{text.replace(tokenizer.mask_token, tokenizer.decode([token]))}</span>'"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt;&gt; This is a great deal.'</span> <span class="hljs-string">'&gt;&gt;&gt; This is a great success.'</span> <span class="hljs-string">'&gt;&gt;&gt; This is a great adventure.'</span> <span class="hljs-string">'&gt;&gt;&gt; This is a great idea.'</span> <span class="hljs-string">'&gt;&gt;&gt; This is a great feat.'</span></pre></div> <p>We can see from the outputs that the model’s predictions refer to everyday terms, which is perhaps not surprising given the foundation of English Wikipedia. Let’s see how we can change this domain to something a bit more niche — highly polarized movie reviews!</p> <h2 class="relative group"><a id="the-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The dataset</span></h2> <p>To showcase domain adaptation, we’ll use the famous <a href="https://huggingface.co/datasets/imdb" rel="nofollow">Large Movie Review Dataset</a> (or IMDb for short), which is a corpus of movie reviews that is often used to benchmark sentiment analysis models. By fine-tuning DistilBERT on this corpus, we expect the language model will adapt its vocabulary from the factual data of Wikipedia that it was pretrained on to the more subjective elements of movie reviews. We can get the data from the Hugging Face Hub with the <code>load_dataset()</code> function from 🤗 Datasets:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset imdb_dataset = load_dataset(<span class="hljs-string">"imdb"</span>) imdb_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'text'</span>, <span class="hljs-string">'label'</span>], num_rows: <span class="hljs-number">25000</span> }) test: Dataset({ features: [<span class="hljs-string">'text'</span>, <span class="hljs-string">'label'</span>], num_rows: <span class="hljs-number">25000</span> }) unsupervised: Dataset({ features: [<span class="hljs-string">'text'</span>, <span class="hljs-string">'label'</span>], num_rows: <span class="hljs-number">50000</span> }) })</pre></div> <p>We can see that the <code>train</code> and <code>test</code> splits each consist of 25,000 reviews, while there is an unlabeled split called <code>unsupervised</code> that contains 50,000 reviews. Let’s take a look at a few samples to get an idea of what kind of text we’re dealing with. As we’ve done in previous chapters of the course, we’ll chain the <code>Dataset.shuffle()</code> and <code>Dataset.select()</code> functions to create a random sample:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sample = imdb_dataset[<span class="hljs-string">"train"</span>].shuffle(seed=<span class="hljs-number">42</span>).select(<span class="hljs-built_in">range</span>(<span class="hljs-number">3</span>)) <span class="hljs-keyword">for</span> row <span class="hljs-keyword">in</span> sample: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"\n'&gt;&gt;&gt; Review: <span class="hljs-subst">{row[<span class="hljs-string">'text'</span>]}</span>'"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"'&gt;&gt;&gt; Label: <span class="hljs-subst">{row[<span class="hljs-string">'label'</span>]}</span>'"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre> <span class="hljs-string">'&gt;&gt;&gt; Review: This is your typical Priyadarshan movie--a bunch of loony characters out on some silly mission. His signature climax has the entire cast of the film coming together and fighting each other in some crazy moshpit over hidden money. Whether it is a winning lottery ticket in Malamaal Weekly, black money in Hera Pheri, "kodokoo" in Phir Hera Pheri, etc., etc., the director is becoming ridiculously predictable. Don\'t get me wrong; as clichéd and preposterous his movies may be, I usually end up enjoying the comedy. However, in most his previous movies there has actually been some good humor, (Hungama and Hera Pheri being noteworthy ones). Now, the hilarity of his films is fading as he is using the same formula over and over again.&lt;br /&gt;&lt;br /&gt;Songs are good. Tanushree Datta looks awesome. Rajpal Yadav is irritating, and Tusshar is not a whole lot better. Kunal Khemu is OK, and Sharman Joshi is the best.'</span> <span class="hljs-string">'&gt;&gt;&gt; Label: 0'</span> <span class="hljs-string">'&gt;&gt;&gt; Review: Okay, the story makes no sense, the characters lack any dimensionally, the best dialogue is ad-libs about the low quality of movie, the cinematography is dismal, and only editing saves a bit of the muddle, but Sam" Peckinpah directed the film. Somehow, his direction is not enough. For those who appreciate Peckinpah and his great work, this movie is a disappointment. Even a great cast cannot redeem the time the viewer wastes with this minimal effort.&lt;br /&gt;&lt;br /&gt;The proper response to the movie is the contempt that the director San Peckinpah, James Caan, Robert Duvall, Burt Young, Bo Hopkins, Arthur Hill, and even Gig Young bring to their work. Watch the great Peckinpah films. Skip this mess.'</span> <span class="hljs-string">'&gt;&gt;&gt; Label: 0'</span> <span class="hljs-string">'&gt;&gt;&gt; Review: I saw this movie at the theaters when I was about 6 or 7 years old. I loved it then, and have recently come to own a VHS version. &lt;br /&gt;&lt;br /&gt;My 4 and 6 year old children love this movie and have been asking again and again to watch it. &lt;br /&gt;&lt;br /&gt;I have enjoyed watching it again too. Though I have to admit it is not as good on a little TV.&lt;br /&gt;&lt;br /&gt;I do not have older children so I do not know what they would think of it. &lt;br /&gt;&lt;br /&gt;The songs are very cute. My daughter keeps singing them over and over.&lt;br /&gt;&lt;br /&gt;Hope this helps.'</span> <span class="hljs-string">'&gt;&gt;&gt; Label: 1'</span></pre></div> <p>Yep, these are certainly movie reviews, and if you’re old enough you may even understand the comment in the last review about owning a VHS version 😜! Although we won’t need the labels for language modeling, we can already see that a <code>0</code> denotes a negative review, while a <code>1</code> corresponds to a positive one.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Create a random sample of the <code>unsupervised</code> split and verify that the labels are neither <code>0</code> nor <code>1</code>. While you’re at it, you could also check that the labels in the <code>train</code> and <code>test</code> splits are indeed <code>0</code> or <code>1</code> — this is a useful sanity check that every NLP practitioner should perform at the start of a new project!</p></div> <p>Now that we’ve had a quick look at the data, let’s dive into preparing it for masked language modeling. As we’ll see, there are some additional steps that one needs to take compared to the sequence classification tasks we saw in <a href="/course/chapter3">Chapter 3</a>. Let’s go!</p> <h2 class="relative group"><a id="preprocessing-the-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preprocessing-the-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preprocessing the data</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/8PmhEIXhBvI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>For both auto-regressive and masked language modeling, a common preprocessing step is to concatenate all the examples and then split the whole corpus into chunks of equal size. This is quite different from our usual approach, where we simply tokenize individual examples. Why concatenate everything together? The reason is that individual examples might get truncated if they’re too long, and that would result in losing information that might be useful for the language modeling task!</p> <p>So to get started, we’ll first tokenize our corpus as usual, but <em>without</em> setting the <code>truncation=True</code> option in our tokenizer. We’ll also grab the word IDs if they are available ((which they will be if we’re using a fast tokenizer, as described in <a href="/course/chapter6/3">Chapter 6</a>), as we will need them later on to do whole word masking. We’ll wrap this in a simple function, and while we’re at it we’ll remove the <code>text</code> and <code>label</code> columns since we don’t need them any longer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_function</span>(<span class="hljs-params">examples</span>): result = tokenizer(examples[<span class="hljs-string">"text"</span>]) <span class="hljs-keyword">if</span> tokenizer.is_fast: result[<span class="hljs-string">"word_ids"</span>] = [result.word_ids(i) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(result[<span class="hljs-string">"input_ids"</span>]))] <span class="hljs-keyword">return</span> result <span class="hljs-comment"># Use batched=True to activate fast multithreading!</span> tokenized_datasets = imdb_dataset.<span class="hljs-built_in">map</span>( tokenize_function, batched=<span class="hljs-literal">True</span>, remove_columns=[<span class="hljs-string">"text"</span>, <span class="hljs-string">"label"</span>] ) tokenized_datasets</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'word_ids'</span>], num_rows: <span class="hljs-number">25000</span> }) test: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'word_ids'</span>], num_rows: <span class="hljs-number">25000</span> }) unsupervised: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'word_ids'</span>], num_rows: <span class="hljs-number">50000</span> }) })</pre></div> <p>Since DistilBERT is a BERT-like model, we can see that the encoded texts consist of the <code>input_ids</code> and <code>attention_mask</code> that we’ve seen in other chapters, as well as the <code>word_ids</code> we added.</p> <p>Now that we’ve tokenized our movie reviews, the next step is to group them all together and split the result into chunks. But how big should these chunks be? This will ultimately be determined by the amount of GPU memory that you have available, but a good starting point is to see what the model’s maximum context size is. This can be inferred by inspecting the <code>model_max_length</code> attribute of the tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.model_max_length</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">512</span></pre></div> <p>This value is derived from the <em>tokenizer_config.json</em> file associated with a checkpoint; in this case we can see that the context size is 512 tokens, just like with BERT.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Some Transformer models, like <a href="https://huggingface.co/google/bigbird-roberta-base" rel="nofollow">BigBird</a> and <a href="hf.co/allenai/longformer-base-4096">Longformer</a>, have a much longer context length than BERT and other early Transformer models. Instantiate the tokenizer for one of these checkpoints and verify that the <code>model_max_length</code> agrees with what’s quoted on its model card.</p></div> <p>So, in order to run our experiments on GPUs like those found on Google Colab, we’ll pick something a bit smaller that can fit in memory:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>chunk_size = <span class="hljs-number">128</span></pre></div> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>Note that using a small chunk size can be detrimental in real-world scenarios, so you should use a size that corresponds to the use case you will apply your model to.</p></div> <p>Now comes the fun part. To show how the concatenation works, let’s take a few reviews from our tokenized training set and print out the number of tokens per review:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># Slicing produces a list of lists for each feature</span> tokenized_samples = tokenized_datasets[<span class="hljs-string">"train"</span>][:<span class="hljs-number">3</span>] <span class="hljs-keyword">for</span> idx, sample <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(tokenized_samples[<span class="hljs-string">"input_ids"</span>]): <span class="hljs-built_in">print</span>(<span class="hljs-string">f"'&gt;&gt;&gt; Review <span class="hljs-subst">{idx}</span> length: <span class="hljs-subst">{<span class="hljs-built_in">len</span>(sample)}</span>'"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt;&gt; Review 0 length: 200'</span> <span class="hljs-string">'&gt;&gt;&gt; Review 1 length: 559'</span> <span class="hljs-string">'&gt;&gt;&gt; Review 2 length: 192'</span></pre></div> <p>We can then concatenate all these examples with a simple dictionary comprehension, as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>concatenated_examples = { k: <span class="hljs-built_in">sum</span>(tokenized_samples[k], []) <span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> tokenized_samples.keys() } total_length = <span class="hljs-built_in">len</span>(concatenated_examples[<span class="hljs-string">"input_ids"</span>]) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"'&gt;&gt;&gt; Concatenated reviews length: <span class="hljs-subst">{total_length}</span>'"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt;&gt; Concatenated reviews length: 951'</span></pre></div> <p>Great, the total length checks out — so now let’s split the concatenated reviews into chunks of the size given by <code>block_size</code>. To do so, we iterate over the features in <code>concatenated_examples</code> and use a list comprehension to create slices of each feature. The result is a dictionary of chunks for each feature:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>chunks = { k: [t[i : i + chunk_size] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, total_length, chunk_size)] <span class="hljs-keyword">for</span> k, t <span class="hljs-keyword">in</span> concatenated_examples.items() } <span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> chunks[<span class="hljs-string">"input_ids"</span>]: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"'&gt;&gt;&gt; Chunk length: <span class="hljs-subst">{<span class="hljs-built_in">len</span>(chunk)}</span>'"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt;&gt; Chunk length: 128'</span> <span class="hljs-string">'&gt;&gt;&gt; Chunk length: 128'</span> <span class="hljs-string">'&gt;&gt;&gt; Chunk length: 128'</span> <span class="hljs-string">'&gt;&gt;&gt; Chunk length: 128'</span> <span class="hljs-string">'&gt;&gt;&gt; Chunk length: 128'</span> <span class="hljs-string">'&gt;&gt;&gt; Chunk length: 128'</span> <span class="hljs-string">'&gt;&gt;&gt; Chunk length: 128'</span> <span class="hljs-string">'&gt;&gt;&gt; Chunk length: 55'</span></pre></div> <p>As you can see in this example, the last chunk will generally be smaller than the maximum chunk size. There are two main strategies for dealing with this:</p> <ul><li>Drop the last chunk if it’s smaller than <code>chunk_size</code>.</li> <li>Pad the last chunk until its length equals <code>chunk_size</code>.</li></ul> <p>We’ll take the first approach here, so let’s wrap all of the above logic in a single function that we can apply to our tokenized datasets:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">group_texts</span>(<span class="hljs-params">examples</span>): <span class="hljs-comment"># Concatenate all texts</span> concatenated_examples = {k: <span class="hljs-built_in">sum</span>(examples[k], []) <span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> examples.keys()} <span class="hljs-comment"># Compute length of concatenated texts</span> total_length = <span class="hljs-built_in">len</span>(concatenated_examples[<span class="hljs-built_in">list</span>(examples.keys())[<span class="hljs-number">0</span>]]) <span class="hljs-comment"># We drop the last chunk if it's smaller than chunk_size</span> total_length = (total_length // chunk_size) * chunk_size <span class="hljs-comment"># Split by chunks of max_len</span> result = { k: [t[i : i + chunk_size] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, total_length, chunk_size)] <span class="hljs-keyword">for</span> k, t <span class="hljs-keyword">in</span> concatenated_examples.items() } <span class="hljs-comment"># Create a new labels column</span> result[<span class="hljs-string">"labels"</span>] = result[<span class="hljs-string">"input_ids"</span>].copy() <span class="hljs-keyword">return</span> result</pre></div> <p>Note that in the last step of <code>group_texts()</code> we create a new <code>labels</code> column which is a copy of the <code>input_ids</code> one. As we’ll see shortly, that’s because in masked language modeling the objective is to predict randomly masked tokens in the input batch, and by creating a <code>labels</code> column we provide the ground truth for our language model to learn from.</p> <p>Let’s now apply <code>group_texts()</code> to our tokenized datasets using our trusty <code>Dataset.map()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>lm_datasets = tokenized_datasets.<span class="hljs-built_in">map</span>(group_texts, batched=<span class="hljs-literal">True</span>) lm_datasets</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'labels'</span>, <span class="hljs-string">'word_ids'</span>], num_rows: <span class="hljs-number">61289</span> }) test: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'labels'</span>, <span class="hljs-string">'word_ids'</span>], num_rows: <span class="hljs-number">59905</span> }) unsupervised: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'labels'</span>, <span class="hljs-string">'word_ids'</span>], num_rows: <span class="hljs-number">122963</span> }) })</pre></div> <p>You can see that grouping and then chunking the texts has produced many more examples than our original 25,000 for the <code>train</code> and <code>test</code> splits. That’s because we now have examples involving <em>contiguous tokens</em> that span across multiple examples from the original corpus. You can see this explicitly by looking for the special <code>[SEP]</code> and <code>[CLS]</code> tokens in one of the chunks:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.decode(lm_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">1</span>][<span class="hljs-string">"input_ids"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">".... at.......... high. a classic line : inspector : i'm here to sack one of your teachers. student : welcome to bromwell high. i expect that many adults of my age think that bromwell high is far fetched. what a pity that it isn't! [SEP] [CLS] homelessness ( or houselessness as george carlin stated ) has been an issue for years but never a plan to help those on the street that were once considered human who did everything from going to school, work, or vote for the matter. most people think of the homeless"</span></pre></div> <p>In this example you can see two overlapping movie reviews, one about a high school movie and the other about homelessness. Let’s also check out what the labels look like for masked language modeling:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.decode(lm_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">1</span>][<span class="hljs-string">"labels"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">".... at.......... high. a classic line : inspector : i'm here to sack one of your teachers. student : welcome to bromwell high. i expect that many adults of my age think that bromwell high is far fetched. what a pity that it isn't! [SEP] [CLS] homelessness ( or houselessness as george carlin stated ) has been an issue for years but never a plan to help those on the street that were once considered human who did everything from going to school, work, or vote for the matter. most people think of the homeless"</span></pre></div> <p>As expected from our <code>group_texts()</code> function above, this looks identical to the decoded <code>input_ids</code> — but then how can our model possibly learn anything? We’re missing a key step: inserting <code>[MASK]</code> tokens at random positions in the inputs! Let’s see how we can do this on the fly during fine-tuning using a special data collator.</p> <h2 class="relative group"><a id="fine-tuning-distilbert-with-the-trainer-api" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-distilbert-with-the-trainer-api"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning DistilBERT with the <code>Trainer</code> API</span></h2> <p>Fine-tuning a masked language model is almost identical to fine-tuning a sequence classification model, like we did in <a href="/course/chapter3">Chapter 3</a>. The only difference is that we need a special data collator that can randomly mask some of the tokens in each batch of texts. Fortunately, 🤗 Transformers comes prepared with a dedicated <code>DataCollatorForLanguageModeling</code> for just this task. We just have to pass it the tokenizer and an <code>mlm_probability</code> argument that specifies what fraction of the tokens to mask. We’ll pick 15%, which is the amount used for BERT and a common choice in the literature:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForLanguageModeling data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm_probability=<span class="hljs-number">0.15</span>)</pre></div> <p>To see how the random masking works, let’s feed a few examples to the data collator. Since it expects a list of <code>dict</code>s, where each <code>dict</code> represents a single chunk of contiguous text, we first iterate over the dataset before feeding the batch to the collator. We remove the <code>"word_ids"</code> key for this data collator as it does not expect it:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>samples = [lm_datasets[<span class="hljs-string">"train"</span>][i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">2</span>)] <span class="hljs-keyword">for</span> sample <span class="hljs-keyword">in</span> samples: _ = sample.pop(<span class="hljs-string">"word_ids"</span>) <span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> data_collator(samples)[<span class="hljs-string">"input_ids"</span>]: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"\n'&gt;&gt;&gt; <span class="hljs-subst">{tokenizer.decode(chunk)}</span>'"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt;&gt; [CLS] bromwell [MASK] is a cartoon comedy. it ran at the same [MASK] as some other [MASK] about school life, [MASK] as " teachers ". [MASK] [MASK] [MASK] in the teaching [MASK] lead [MASK] to believe that bromwell high\'[MASK] satire is much closer to reality than is " teachers ". the scramble [MASK] [MASK] financially, the [MASK]ful students whogn [MASK] right through [MASK] pathetic teachers\'pomp, the pettiness of the whole situation, distinction remind me of the schools i knew and their students. when i saw [MASK] episode in [MASK] a student repeatedly tried to burn down the school, [MASK] immediately recalled. [MASK]...'</span> <span class="hljs-string">'&gt;&gt;&gt; .... at.. [MASK]... [MASK]... high. a classic line plucked inspector : i\'[MASK] here to [MASK] one of your [MASK]. student : welcome to bromwell [MASK]. i expect that many adults of my age think that [MASK]mwell [MASK] is [MASK] fetched. what a pity that it isn\'t! [SEP] [CLS] [MASK]ness ( or [MASK]lessness as george 宇in stated )公 been an issue for years but never [MASK] plan to help those on the street that were once considered human [MASK] did everything from going to school, [MASK], [MASK] vote for the matter. most people think [MASK] the homeless'</span></pre></div> <p>Nice, it worked! We can see that the <code>[MASK]</code> token has been randomly inserted at various locations in our text. These will be the tokens which our model will have to predict during training — and the beauty of the data collator is that it will randomize the <code>[MASK]</code> insertion with every batch!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Run the code snippet above several times to see the random masking happen in front of your very eyes! Also replace the <code>tokenizer.decode()</code> method with <code>tokenizer.convert_ids_to_tokens()</code> to see that sometimes a single token from a given word is masked, and not the others.</p></div> <p>One side effect of random masking is that our evaluation metrics will not be deterministic when using the <code>Trainer</code>, since we use the same data collator for the training and test sets. We’ll see later, when we look at fine-tuning with 🤗 Accelerate, how we can use the flexibility of a custom evaluation loop to freeze the randomness.</p> <p>When training models for masked language modeling, one technique that can be used is to mask whole words together, not just individual tokens. This approach is called <em>whole word masking</em>. If we want to use whole word masking, we will need to build a data collator ourselves. A data collator is just a function that takes a list of samples and converts them into a batch, so let’s do this now! We’ll use the word IDs computed earlier to make a map between word indices and the corresponding tokens, then randomly decide which words to mask and apply that mask on the inputs. Note that the labels are all <code>-100</code> except for the ones corresponding to mask words.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> collections <span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> default_data_collator wwm_probability = <span class="hljs-number">0.2</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">whole_word_masking_data_collator</span>(<span class="hljs-params">features</span>): <span class="hljs-keyword">for</span> feature <span class="hljs-keyword">in</span> features: word_ids = feature.pop(<span class="hljs-string">"word_ids"</span>) <span class="hljs-comment"># Create a map between words and corresponding token indices</span> mapping = collections.defaultdict(<span class="hljs-built_in">list</span>) current_word_index = -<span class="hljs-number">1</span> current_word = <span class="hljs-literal">None</span> <span class="hljs-keyword">for</span> idx, word_id <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(word_ids): <span class="hljs-keyword">if</span> word_id <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>: <span class="hljs-keyword">if</span> word_id != current_word: current_word = word_id current_word_index += <span class="hljs-number">1</span> mapping[current_word_index].append(idx) <span class="hljs-comment"># Randomly mask words</span> mask = np.random.binomial(<span class="hljs-number">1</span>, wwm_probability, (<span class="hljs-built_in">len</span>(mapping),)) input_ids = feature[<span class="hljs-string">"input_ids"</span>] labels = feature[<span class="hljs-string">"labels"</span>] new_labels = [-<span class="hljs-number">100</span>] * <span class="hljs-built_in">len</span>(labels) <span class="hljs-keyword">for</span> word_id <span class="hljs-keyword">in</span> np.where(mask)[<span class="hljs-number">0</span>]: word_id = word_id.item() <span class="hljs-keyword">for</span> idx <span class="hljs-keyword">in</span> mapping[word_id]: new_labels[idx] = labels[idx] input_ids[idx] = tokenizer.mask_token_id feature[<span class="hljs-string">"labels"</span>] = new_labels <span class="hljs-keyword">return</span> default_data_collator(features)</pre></div> <p>Next, we can try it on the same samples as before:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>samples = [lm_datasets[<span class="hljs-string">"train"</span>][i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">2</span>)] batch = whole_word_masking_data_collator(samples) <span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> batch[<span class="hljs-string">"input_ids"</span>]: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"\n'&gt;&gt;&gt; <span class="hljs-subst">{tokenizer.decode(chunk)}</span>'"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt;&gt; [CLS] bromwell high is a cartoon comedy [MASK] it ran at the same time as some other programs about school life, such as " teachers ". my 35 years in the teaching profession lead me to believe that bromwell high\'s satire is much closer to reality than is " teachers ". the scramble to survive financially, the insightful students who can see right through their pathetic teachers\'pomp, the pettiness of the whole situation, all remind me of the schools i knew and their students. when i saw the episode in which a student repeatedly tried to burn down the school, i immediately recalled.....'</span> <span class="hljs-string">'&gt;&gt;&gt; .... [MASK] [MASK] [MASK] [MASK]....... high. a classic line : inspector : i\'m here to sack one of your teachers. student : welcome to bromwell high. i expect that many adults of my age think that bromwell high is far fetched. what a pity that it isn\'t! [SEP] [CLS] homelessness ( or houselessness as george carlin stated ) has been an issue for years but never a plan to help those on the street that were once considered human who did everything from going to school, work, or vote for the matter. most people think of the homeless'</span></pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Run the code snippet above several times to see the random masking happen in front of your very eyes! Also replace the <code>tokenizer.decode()</code> method with <code>tokenizer.convert_ids_to_tokens()</code> to see that the tokens from a given word are always masked together.</p></div> <p>Now that we have two data collators, the rest of the fine-tuning steps are standard. Training can take a while on Google Colab if you’re not lucky enough to score a mythical P100 GPU 😭, so we’ll first downsample the size of the training set to a few thousand examples. Don’t worry, we’ll still get a pretty decent language model! A quick way to downsample a dataset in 🤗 Datasets is via the <code>Dataset.train_test_split()</code> function that we saw in <a href="/course/chapter5">Chapter 5</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>train_size = <span class="hljs-number">10_000</span> test_size = <span class="hljs-built_in">int</span>(<span class="hljs-number">0.1</span> * train_size) downsampled_dataset = lm_datasets[<span class="hljs-string">"train"</span>].train_test_split( train_size=train_size, test_size=test_size, seed=<span class="hljs-number">42</span> ) downsampled_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'labels'</span>, <span class="hljs-string">'word_ids'</span>], num_rows: <span class="hljs-number">10000</span> }) test: Dataset({ features: [<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'labels'</span>, <span class="hljs-string">'word_ids'</span>], num_rows: <span class="hljs-number">1000</span> }) })</pre></div> <p>This has automatically created new <code>train</code> and <code>test</code> splits, with the training set size set to 10,000 examples and the validation set to 10% of that — feel free to increase this if you have a beefy GPU! The next thing we need to do is log in to the Hugging Face Hub. If you’re running this code in a notebook, you can do so with the following utility function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login notebook_login()</pre></div> <p>which will display a widget where you can enter your credentials. Alternatively, you can run:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-<span class="hljs-keyword">cli</span> login</pre></div> <p>in your favorite terminal and log in there.</p> <p>Once we’re logged in, we can specify the arguments for the <code>Trainer</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments batch_size = <span class="hljs-number">64</span> <span class="hljs-comment"># Show the training loss with every epoch</span> logging_steps = <span class="hljs-built_in">len</span>(downsampled_dataset[<span class="hljs-string">"train"</span>]) // batch_size model_name = model_checkpoint.split(<span class="hljs-string">"/"</span>)[-<span class="hljs-number">1</span>] training_args = TrainingArguments( output_dir=<span class="hljs-string">f"<span class="hljs-subst">{model_name}</span>-finetuned-imdb"</span>, overwrite_output_dir=<span class="hljs-literal">True</span>, evaluation_strategy=<span class="hljs-string">"epoch"</span>, learning_rate=<span class="hljs-number">2e-5</span>, weight_decay=<span class="hljs-number">0.01</span>, per_device_train_batch_size=batch_size, per_device_eval_batch_size=batch_size, push_to_hub=<span class="hljs-literal">True</span>, fp16=<span class="hljs-literal">True</span>, logging_steps=logging_steps, )</pre></div> <p>Here we tweaked a few of the default options, including <code>logging_steps</code> to ensure we track the training loss with each epoch. We’ve also used <code>fp16=True</code> to enable mixed-precision training, which gives us another boost in speed. By default, the <code>Trainer</code> will remove any columns that are not part of the model’s <code>forward()</code> method. This means that if you’re using the whole word masking collator, you’ll also need to set <code>remove_unused_columns=False</code> to ensure we don’t lose the <code>word_ids</code> column during training.</p> <p>Note that you can specify the name of the repository you want to push to with the <code>hub_model_id</code> argument (in particular, you will have to use this argument to push to an organization). For instance, when we pushed the model to the <a href="https://huggingface.co/huggingface-course" rel="nofollow"><code>huggingface-course</code> organization</a>, we added <code>hub_model_id="huggingface-course/distilbert-finetuned-imdb"</code> to <code>TrainingArguments</code>. By default, the repository used will be in your namespace and named after the output directory you set, so in our case it will be <code>"lewtun/distilbert-finetuned-imdb"</code>.</p> <p>We now have all the ingredients to instantiate the <code>Trainer</code>. Here we just use the standard <code>data_collator</code>, but you can try the whole word masking collator and compare the results as an exercise:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Trainer trainer = Trainer( model=model, args=training_args, train_dataset=downsampled_dataset[<span class="hljs-string">"train"</span>], eval_dataset=downsampled_dataset[<span class="hljs-string">"test"</span>], data_collator=data_collator, tokenizer=tokenizer, )</pre></div> <p>We’re now ready to run <code>trainer.train()</code> — but before doing so let’s briefly look at <em>perplexity</em>, which is a common metric to evaluate the performance of language models.</p> <h3 class="relative group"><a id="perplexity-for-language-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#perplexity-for-language-models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Perplexity for language models</span></h3> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/NURcDHhYe98" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Unlike other tasks like text classification or question answering where we’re given a labeled corpus to train on, with language modeling we don’t have any explicit labels. So how do we determine what makes a good language model? Like with the autocorrect feature in your phone, a good language model is one that assigns high probabilities to sentences that are grammatically correct, and low probabilities to nonsense sentences. To give you a better idea of what this looks like, you can find whole sets of “autocorrect fails” online, where the model in a person’s phone has produced some rather funny (and often inappropriate) completions!</p> <p>Assuming our test set consists mostly of sentences that are grammatically correct, then one way to measure the quality of our language model is to calculate the probabilities it assigns to the next word in all the sentences of the test set. High probabilities indicates that the model is not “surprised” or “perplexed” by the unseen examples, and suggests it has learned the basic patterns of grammar in the language. There are various mathematical definitions of perplexity, but the one we’ll use defines it as the exponential of the cross-entropy loss. Thus, we can calculate the perplexity of our pretrained model by using the <code>Trainer.evaluate()</code> function to compute the cross-entropy loss on the test set and then taking the exponential of the result:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> math eval_results = trainer.evaluate() <span class="hljs-built_in">print</span>(<span class="hljs-string">f"&gt;&gt;&gt; Perplexity: <span class="hljs-subst">{math.exp(eval_results[<span class="hljs-string">'eval_loss'</span>]):<span class="hljs-number">.2</span>f}</span>"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>Perplexity: <span class="hljs-number">21.75</span></pre></div> <p>A lower perplexity score means a better language model, and we can see here that our starting model has a somewhat large value. Let’s see if we can lower it by fine-tuning! To do that, we first run the training loop:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.train()</pre></div> <p>and then compute the resulting perplexity on the test set as before:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>eval_results = trainer.evaluate() <span class="hljs-built_in">print</span>(<span class="hljs-string">f"&gt;&gt;&gt; Perplexity: <span class="hljs-subst">{math.exp(eval_results[<span class="hljs-string">'eval_loss'</span>]):<span class="hljs-number">.2</span>f}</span>"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>Perplexity: <span class="hljs-number">11.32</span></pre></div> <p>Nice — this is quite a reduction in perplexity, which tells us the model has learned something about the domain of movie reviews!</p> <p>Once training is finished, we can push the model card with the training information to the Hub (the checkpoints are saved during training itself):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.push_to_hub()</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Your turn!</strong> Run the training above after changing the data collator to the whole word masking collator. Do you get better results?</p></div> <p>In our use case we didn’t need to do anything special with the training loop, but in some cases you might need to implement some custom logic. For these applications, you can use 🤗 Accelerate — let’s take a look!</p> <h2 class="relative group"><a id="fine-tuning-distilbert-with-accelerate" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-distilbert-with-accelerate"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning DistilBERT with 🤗 Accelerate</span></h2> <p>As we saw with the <code>Trainer</code>, fine-tuning a masked language model is very similar to the text classification example from <a href="/course/chapter3">Chapter 3</a>. In fact, the only subtlety is the use of a special data collator, and we’ve already covered that earlier in this section!</p> <p>However, we saw that <code>DataCollatorForLanguageModeling</code> also applies random masking with each evaluation, so we’ll see some fluctuations in our perplexity scores with each training run. One way to eliminate this source of randomness is to apply the masking <em>once</em> on the whole test set, and then use the default data collator in 🤗 Transformers to collect the batches during evaluation. To see how this works, let’s implement a simple function that applies the masking on a batch, similar to our first encounter with <code>DataCollatorForLanguageModeling</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">insert_random_mask</span>(<span class="hljs-params">batch</span>): features = [<span class="hljs-built_in">dict</span>(<span class="hljs-built_in">zip</span>(batch, t)) <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(*batch.values())] masked_inputs = data_collator(features) <span class="hljs-comment"># Create a new "masked" column for each column in the dataset</span> <span class="hljs-keyword">return</span> {<span class="hljs-string">"masked_"</span> + k: v.numpy() <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> masked_inputs.items()}</pre></div> <p>Next, we’ll apply this function to our test set and drop the unmasked columns so we can replace them with the masked ones. You can use whole word masking by replacing the <code>data_collator</code> above with the appropriate one, in which case you should remove the first line here:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>downsampled_dataset = downsampled_dataset.remove_columns([<span class="hljs-string">"word_ids"</span>]) eval_dataset = downsampled_dataset[<span class="hljs-string">"test"</span>].<span class="hljs-built_in">map</span>( insert_random_mask, batched=<span class="hljs-literal">True</span>, remove_columns=downsampled_dataset[<span class="hljs-string">"test"</span>].column_names, ) eval_dataset = eval_dataset.rename_columns( { <span class="hljs-string">"masked_input_ids"</span>: <span class="hljs-string">"input_ids"</span>, <span class="hljs-string">"masked_attention_mask"</span>: <span class="hljs-string">"attention_mask"</span>, <span class="hljs-string">"masked_labels"</span>: <span class="hljs-string">"labels"</span>, } )</pre></div> <p>We can then set up the dataloaders as usual, but we’ll use the <code>default_data_collator</code> from 🤗 Transformers for the evaluation set:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> default_data_collator batch_size = <span class="hljs-number">64</span> train_dataloader = DataLoader( downsampled_dataset[<span class="hljs-string">"train"</span>], shuffle=<span class="hljs-literal">True</span>, batch_size=batch_size, collate_fn=data_collator, ) eval_dataloader = DataLoader( eval_dataset, batch_size=batch_size, collate_fn=default_data_collator )</pre></div> <p>Form here, we follow the standard steps with 🤗 Accelerate. The first order of business is to load a fresh version of the pretrained model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model = <span class="hljs-module-access"><span class="hljs-module"><span class="hljs-identifier">AutoModelForMaskedLM</span>.</span></span>from<span class="hljs-constructor">_pretrained(<span class="hljs-params">model_checkpoint</span>)</span></pre></div> <p>Then we need to specify the optimizer; we’ll use the standard <code>AdamW</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.optim <span class="hljs-keyword">import</span> AdamW optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">5e-5</span>)</pre></div> <p>With these objects, we can now prepare everything for training with the <code>Accelerator</code> object:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )</pre></div> <p>Now that our model, optimizer, and dataloaders are configured, we can specify the learning rate scheduler as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler num_train_epochs = <span class="hljs-number">3</span> num_update_steps_per_epoch = <span class="hljs-built_in">len</span>(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( <span class="hljs-string">"linear"</span>, optimizer=optimizer, num_warmup_steps=<span class="hljs-number">0</span>, num_training_steps=num_training_steps, )</pre></div> <p>There is just one last thing to do before training: create a model repository on the Hugging Face Hub! We can use the 🤗 Hub library to first generate the full name of our repo:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> get_full_repo_name model_name = <span class="hljs-string">"distilbert-base-uncased-finetuned-imdb-accelerate"</span> repo_name = get_full_repo_name(model_name) repo_name</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'lewtun/distilbert-base-uncased-finetuned-imdb-accelerate'</span></pre></div> <p>then create and clone the repository using the <code>Repository</code> class from 🤗 Hub:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> Repository output_dir = model_name repo = Repository(output_dir, clone_from=repo_name)</pre></div> <p>With that done, it’s just a simple matter of writing out the full training and evaluation loop:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm <span class="hljs-keyword">import</span> torch <span class="hljs-keyword">import</span> math progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps)) <span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_train_epochs): <span class="hljs-comment"># Training</span> model.train() <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(<span class="hljs-number">1</span>) <span class="hljs-comment"># Evaluation</span> model.<span class="hljs-built_in">eval</span>() losses = [] <span class="hljs-keyword">for</span> step, batch <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(eval_dataloader): <span class="hljs-keyword">with</span> torch.no_grad(): outputs = model(**batch) loss = outputs.loss losses.append(accelerator.gather(loss.repeat(batch_size))) losses = torch.cat(losses) losses = losses[: <span class="hljs-built_in">len</span>(eval_dataset)] <span class="hljs-keyword">try</span>: perplexity = math.exp(torch.mean(losses)) <span class="hljs-keyword">except</span> OverflowError: perplexity = <span class="hljs-built_in">float</span>(<span class="hljs-string">"inf"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"&gt;&gt;&gt; Epoch <span class="hljs-subst">{epoch}</span>: Perplexity: <span class="hljs-subst">{perplexity}</span>"</span>) <span class="hljs-comment"># Save and upload</span> accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) <span class="hljs-keyword">if</span> accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=<span class="hljs-string">f"Training in progress epoch <span class="hljs-subst">{epoch}</span>"</span>, blocking=<span class="hljs-literal">False</span> )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>Epoch <span class="hljs-number">0</span>: Perplexity: <span class="hljs-number">11.397545307900472</span> <span class="hljs-meta">&gt;&gt;&gt; </span>Epoch <span class="hljs-number">1</span>: Perplexity: <span class="hljs-number">10.904909330983092</span> <span class="hljs-meta">&gt;&gt;&gt; </span>Epoch <span class="hljs-number">2</span>: Perplexity: <span class="hljs-number">10.729503505340409</span></pre></div> <p>Cool, we’ve been able to evaluate perplexity with each epoch and ensure that multiple training runs are reproducible!</p> <h2 class="relative group"><a id="using-our-fine-tuned-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-our-fine-tuned-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using our fine-tuned model</span></h2> <p>You can interact with your fine-tuned model either by using its widget on the Hub or locally with the <code>pipeline</code> from 🤗 Transformers. Let’s use the latter to download our model using the <code>fill-mask</code> pipeline:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline mask_filler = pipeline( <span class="hljs-string">"fill-mask"</span>, model=<span class="hljs-string">"huggingface-course/distilbert-base-uncased-finetuned-imdb"</span> )</pre></div> <p>We can then feed the pipeline our sample text of “This is a great [MASK]” and see what the top 5 predictions are:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>preds = mask_filler(text) <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> preds: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"&gt;&gt;&gt; <span class="hljs-subst">{pred[<span class="hljs-string">'sequence'</span>]}</span>"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt;&gt; this is a great movie.'</span> <span class="hljs-string">'&gt;&gt;&gt; this is a great film.'</span> <span class="hljs-string">'&gt;&gt;&gt; this is a great story.'</span> <span class="hljs-string">'&gt;&gt;&gt; this is a great movies.'</span> <span class="hljs-string">'&gt;&gt;&gt; this is a great character.'</span></pre></div> <p>Neat — our model has clearly adapted its weights to predict words that are more strongly associated with movies!</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/0Oxphw4Q9fo" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>This wraps up our first experiment with training a language model. In <a href="/course/en/chapter7/section6">section 6</a> you’ll learn how to train an auto-regressive model like GPT-2 from scratch; head over there if you’d like to see how you can pretrain your very own Transformer model!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> To quantify the benefits of domain adaptation, fine-tune a classifier on the IMDb labels for both the pretrained and fine-tuned DistilBERT checkpoints. If you need a refresher on text classification, check out <a href="/course/chapter3">Chapter 3</a>.</p></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter7/2?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Token classification</a> <a href="/learn/nlp-course/chapter7/4?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Translation<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;fine-tuning-a-masked-language-model&quot;,&quot;url&quot;:&quot;#fine-tuning-a-masked-language-model&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Picking a pretrained model for masked language modeling&quot;,&quot;id&quot;:&quot;picking-a-pretrained-model-for-masked-language-modeling&quot;,&quot;url&quot;:&quot;#picking-a-pretrained-model-for-masked-language-modeling&quot;},{&quot;title&quot;:&quot;The dataset&quot;,&quot;id&quot;:&quot;the-dataset&quot;,&quot;url&quot;:&quot;#the-dataset&quot;},{&quot;title&quot;:&quot;Preprocessing the data&quot;,&quot;id&quot;:&quot;preprocessing-the-data&quot;,&quot;url&quot;:&quot;#preprocessing-the-data&quot;},{&quot;title&quot;:&quot;Fine-tuning DistilBERT with the `Trainer` API&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;fine-tuning-distilbert-with-the-trainer-api&quot;,&quot;url&quot;:&quot;#fine-tuning-distilbert-with-the-trainer-api&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Perplexity for language models&quot;,&quot;id&quot;:&quot;perplexity-for-language-models&quot;,&quot;url&quot;:&quot;#perplexity-for-language-models&quot;}]},{&quot;title&quot;:&quot;Fine-tuning DistilBERT with 🤗 Accelerate&quot;,&quot;id&quot;:&quot;fine-tuning-distilbert-with-accelerate&quot;,&quot;url&quot;:&quot;#fine-tuning-distilbert-with-accelerate&quot;},{&quot;title&quot;:&quot;Using our fine-tuned model&quot;,&quot;id&quot;:&quot;using-our-fine-tuned-model&quot;,&quot;url&quot;:&quot;#using-our-fine-tuned-model&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#fine-tuning-a-masked-language-model" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-a-masked-language-model"><wbr>Fine-tuning a masked language model</a> <a href="#picking-a-pretrained-model-for-masked-language-modeling" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-picking-a-pretrained-model-for-masked-language-modeling"><wbr>Picking a pretrained model for masked language modeling</a> <a href="#the-dataset" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-dataset"><wbr>The dataset</a> <a href="#preprocessing-the-data" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preprocessing-the-data"><wbr>Preprocessing the data</a> <a href="#fine-tuning-distilbert-with-the-trainer-api" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-distilbert-with-the-trainer-api"><wbr>Fine-tuning <wbr>DistilBER<wbr>T with the `<wbr>Trainer` API</a> <a href="#perplexity-for-language-models" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-perplexity-for-language-models"><wbr>Perplexity for language models</a> <a href="#fine-tuning-distilbert-with-accelerate" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-distilbert-with-accelerate"><wbr>Fine-tuning <wbr>DistilBER<wbr>T with 🤗 <wbr>Accelerate</a> <a href="#using-our-fine-tuned-model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-our-fine-tuned-model"><wbr>Using our fine-tuned model</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter7/3" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter7/3"); } </script> <iframe name="__privateStripeMetricsController9850" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter7%2F3%3Ffw%3Dpt&amp;title=Fine-tuning%20a%20masked%20language%20model%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:28.507Z
Translation - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter7/4?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#translation)Translation [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-7-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section4_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section4_pt.ipynb) Let’s now dive into translation. This is another [sequence-to-sequence task](/course/chapter1/7), which means it’s a problem that can be formulated as going from one sequence to another. In that sense the problem is pretty close to [summarization](/course/chapter7/6), and you could adapt what we will see here to other sequence-to-sequence problems such as: - **Style transfer**: Creating a model that _translates_ texts written in a certain style to another (e.g., formal to casual or Shakespearean English to modern English) - **Generative question answering**: Creating a model that generates answers to questions, given a context If you have a big enough corpus of texts in two (or more) languages, you can train a new translation model from scratch like we will in the section on [causal language modeling](/course/chapter7/6). It will be faster, however, to fine-tune an existing translation model, be it a multilingual one like mT5 or mBART that you want to fine-tune to a specific language pair, or even a model specialized for translation from one language to another that you want to fine-tune to your specific corpus. In this section, we will fine-tune a Marian model pretrained to translate from English to French (since a lot of Hugging Face employees speak both those languages) on the [KDE4 dataset](https://huggingface.co/datasets/kde4), which is a dataset of localized files for the [KDE apps](https://apps.kde.org/). The model we will use has been pretrained on a large corpus of French and English texts taken from the [Opus dataset](https://opus.nlpl.eu/), which actually contains the KDE4 dataset. But even if the pretrained model we use has seen that data during its pretraining, we will see that we can get a better version of it after fine-tuning. Once we’re finished, we will have a model able to make predictions like this one: [![One-hot encoded labels for question answering.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/modeleval-marian-finetuned-kde4-en-to-fr.png) ![One-hot encoded labels for question answering.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/modeleval-marian-finetuned-kde4-en-to-fr-dark.png)](/huggingface-course/marian-finetuned-kde4-en-to-fr) As in the previous sections, you can find the actual model that we’ll train and upload to the Hub using the code below and double-check its predictions [here](https://huggingface.co/huggingface-course/marian-finetuned-kde4-en-to-fr?text=This+plugin+allows+you+to+automatically+translate+web+pages+between+several+languages.). ## [](#preparing-the-data)Preparing the data To fine-tune or train a translation model from scratch, we will need a dataset suitable for the task. As mentioned previously, we’ll use the [KDE4 dataset](https://huggingface.co/datasets/kde4) in this section, but you can adapt the code to use your own data quite easily, as long as you have pairs of sentences in the two languages you want to translate from and into. Refer back to [Chapter 5](/course/chapter5) if you need a reminder of how to load your custom data in a `Dataset`. ### [](#the-kde4-dataset)The KDE4 dataset As usual, we download our dataset using the `load_dataset()` function: ``` from datasets import load_dataset raw_datasets = load_dataset("kde4", lang1="en", lang2="fr")``` If you want to work with a different pair of languages, you can specify them by their codes. A total of 92 languages are available for this dataset; you can see them all by expanding the language tags on its [dataset card](https://huggingface.co/datasets/kde4). ![Language available for the KDE4 dataset.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/language_tags.png) Let’s have a look at the dataset: ``` DatasetDict({ train: Dataset({ features: ['id', 'translation'], num_rows: 210173 }) })``` We have 210,173 pairs of sentences, but in one single split, so we will need to create our own validation set. As we saw in [Chapter 5](/course/chapter5), a `Dataset` has a `train_test_split()` method that can help us. We’ll provide a seed for reproducibility: ``` split_datasets = raw_datasets["train"].train_test_split(train_size=0.9, seed=20) split_datasets``` ``` DatasetDict({ train: Dataset({ features: ['id', 'translation'], num_rows: 189155 }) test: Dataset({ features: ['id', 'translation'], num_rows: 21018 }) })``` We can rename the `"test"` key to `"validation"` like this: ``` split_datasets["validation"] = split_datasets.pop("test")``` Now let’s take a look at one element of the dataset: ``` split_datasets["train"][1]["translation"]``` ``` {'en': 'Default to expanded threads', 'fr': 'Par défaut, développer les fils de discussion'}``` We get a dictionary with two sentences in the pair of languages we requested. One particularity of this dataset full of technical computer science terms is that they are all fully translated in French. However, French engineers are often lazy and leave most computer science-specific words in English when they talk. Here, for instance, the word “threads” might well appear in a French sentence, especially in a technical conversation; but in this dataset it has been translated into the more correct “fils de discussion.” The pretrained model we use, which has been pretrained on a larger corpus of French and English sentences, takes the easier option of leaving the word as is: ``` from transformers import pipeline model_checkpoint = "Helsinki-NLP/opus-mt-en-fr" translator = pipeline("translation", model=model_checkpoint) translator("Default to expanded threads")``` ``` [{'translation_text': 'Par défaut pour les threads élargis'}]``` Another example of this behavior can be seen with the word “plugin,” which isn’t officially a French word but which most native speakers will understand and not bother to translate. In the KDE4 dataset this word has been translated in French into the more official “module d’extension”: ``` split_datasets["train"][172]["translation"]``` ``` {'en': 'Unable to import %1 using the OFX importer plugin. This file is not the correct format.', 'fr': "Impossible d'importer %1 en utilisant le module d'extension d'importation OFX. Ce fichier n'a pas un format correct."}``` Our pretrained model, however, sticks with the compact and familiar English word: ``` translator( "Unable to import %1 using the OFX importer plugin. This file is not the correct format." )``` ``` [{'translation_text': "Impossible d'importer %1 en utilisant le plugin d'importateur OFX. Ce fichier n'est pas le bon format."}]``` It will be interesting to see if our fine-tuned model picks up on those particularities of the dataset (spoiler alert: it will). ✏️ **Your turn!** Another English word that is often used in French is “email.” Find the first sample in the training dataset that uses this word. How is it translated? How does the pretrained model translate the same English sentence? ### [](#processing-the-data)Processing the data You should know the drill by now: the texts all need to be converted into sets of token IDs so the model can make sense of them. For this task, we’ll need to tokenize both the inputs and the targets. Our first task is to create our `tokenizer` object. As noted earlier, we’ll be using a Marian English to French pretrained model. If you are trying this code with another pair of languages, make sure to adapt the model checkpoint. The [Helsinki-NLP](https://huggingface.co/Helsinki-NLP) organization provides more than a thousand models in multiple languages. ``` from transformers import AutoTokenizer model_checkpoint = "Helsinki-NLP/opus-mt-en-fr" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint, return_tensors="pt")``` You can also replace the `model_checkpoint` with any other model you prefer from the [Hub](https://huggingface.co/models), or a local folder where you’ve saved a pretrained model and a tokenizer. 💡 If you are using a multilingual tokenizer such as mBART, mBART-50, or M2M100, you will need to set the language codes of your inputs and targets in the tokenizer by setting `tokenizer.src_lang` and `tokenizer.tgt_lang` to the right values. The preparation of our data is pretty straightforward. There’s just one thing to remember; you need to ensure that the tokenizer processes the targets in the output language (here, French). You can do this by passing the targets to the `text_targets` argument of the tokenizer’s `__call__` method. To see how this works, let’s process one sample of each language in the training set: ``` en_sentence = split_datasets["train"][1]["translation"]["en"] fr_sentence = split_datasets["train"][1]["translation"]["fr"] inputs = tokenizer(en_sentence, text_target=fr_sentence) inputs``` ``` {'input_ids': [47591, 12, 9842, 19634, 9, 0], 'attention_mask': [1, 1, 1, 1, 1, 1], 'labels': [577, 5891, 2, 3184, 16, 2542, 5, 1710, 0]}``` As we can see, the output contains the input IDs associated with the English sentence, while the IDs associated with the French one are stored in the `labels` field. If you forget to indicate that you are tokenizing labels, they will be tokenized by the input tokenizer, which in the case of a Marian model is not going to go well at all: ``` wrong_targets = tokenizer(fr_sentence) print(tokenizer.convert_ids_to_tokens(wrong_targets["input_ids"])) print(tokenizer.convert_ids_to_tokens(inputs["labels"]))``` ``` ['▁Par', '▁dé', 'f', 'aut', ',', '▁dé', 've', 'lop', 'per', '▁les', '▁fil', 's', '▁de', '▁discussion', '</s>'] ['▁Par', '▁défaut', ',', '▁développer', '▁les', '▁fils', '▁de', '▁discussion', '</s>']``` As we can see, using the English tokenizer to preprocess a French sentence results in a lot more tokens, since the tokenizer doesn’t know any French words (except those that also appear in the English language, like “discussion”). Since `inputs` is a dictionary with our usual keys (input IDs, attention mask, etc.), the last step is to define the preprocessing function we will apply on the datasets: ``` max_length = 128 def preprocess_function(examples): inputs = [ex["en"] for ex in examples["translation"]] targets = [ex["fr"] for ex in examples["translation"]] model_inputs = tokenizer( inputs, text_target=targets, max_length=max_length, truncation=True ) return model_inputs``` Note that we set the same maximum length for our inputs and outputs. Since the texts we’re dealing with seem pretty short, we use 128. 💡 If you are using a T5 model (more specifically, one of the `t5-xxx` checkpoints), the model will expect the text inputs to have a prefix indicating the task at hand, such as `translate: English to French:`. ⚠️ We don’t pay attention to the attention mask of the targets, as the model won’t expect it. Instead, the labels corresponding to a padding token should be set to `-100` so they are ignored in the loss computation. This will be done by our data collator later on since we are applying dynamic padding, but if you use padding here, you should adapt the preprocessing function to set all labels that correspond to the padding token to `-100`. We can now apply that preprocessing in one go on all the splits of our dataset: ``` tokenized_datasets = split_datasets.map( preprocess_function, batched=True, remove_columns=split_datasets["train"].column_names, )``` Now that the data has been preprocessed, we are ready to fine-tune our pretrained model! ## [](#fine-tuning-the-model-with-the-trainer-api)Fine-tuning the model with the `Trainer` API The actual code using the `Trainer` will be the same as before, with just one little change: we use a [`Seq2SeqTrainer`](https://huggingface.co/transformers/main_classes/trainer.html#seq2seqtrainer) here, which is a subclass of `Trainer` that will allow us to properly deal with the evaluation, using the `generate()` method to predict outputs from the inputs. We’ll dive into that in more detail when we talk about the metric computation. First things first, we need an actual model to fine-tune. We’ll use the usual `AutoModel` API: ``` from transformers import AutoModelForSeq2SeqLM model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)``` Note that this time we are using a model that was trained on a translation task and can actually be used already, so there is no warning about missing weights or newly initialized ones. ### [](#data-collation)Data collation We’ll need a data collator to deal with the padding for dynamic batching. We can’t just use a `DataCollatorWithPadding` like in [Chapter 3](/course/chapter3) in this case, because that only pads the inputs (input IDs, attention mask, and token type IDs). Our labels should also be padded to the maximum length encountered in the labels. And, as mentioned previously, the padding value used to pad the labels should be `-100` and not the padding token of the tokenizer, to make sure those padded values are ignored in the loss computation. This is all done by a [`DataCollatorForSeq2Seq`](https://huggingface.co/transformers/main_classes/data_collator.html#datacollatorforseq2seq). Like the `DataCollatorWithPadding`, it takes the `tokenizer` used to preprocess the inputs, but it also takes the `model`. This is because this data collator will also be responsible for preparing the decoder input IDs, which are shifted versions of the labels with a special token at the beginning. Since this shift is done slightly differently for different architectures, the `DataCollatorForSeq2Seq` needs to know the `model` object: ``` from transformers import DataCollatorForSeq2Seq data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)``` To test this on a few samples, we just call it on a list of examples from our tokenized training set: ``` batch = data_collator([tokenized_datasets["train"][i] for i in range(1, 3)]) batch.keys()``` ``` dict_keys(['attention_mask', 'input_ids', 'labels', 'decoder_input_ids'])``` We can check our labels have been padded to the maximum length of the batch, using `-100`: ``` tensor([[ 577, 5891, 2, 3184, 16, 2542, 5, 1710, 0, -100, -100, -100, -100, -100, -100, -100], [ 1211, 3, 49, 9409, 1211, 3, 29140, 817, 3124, 817, 550, 7032, 5821, 7907, 12649, 0]])``` And we can also have a look at the decoder input IDs, to see that they are shifted versions of the labels: ``` batch["decoder_input_ids"]``` ``` tensor([[59513, 577, 5891, 2, 3184, 16, 2542, 5, 1710, 0, 59513, 59513, 59513, 59513, 59513, 59513], [59513, 1211, 3, 49, 9409, 1211, 3, 29140, 817, 3124, 817, 550, 7032, 5821, 7907, 12649]])``` Here are the labels for the first and second elements in our dataset: ``` for i in range(1, 3): print(tokenized_datasets["train"][i]["labels"])``` ``` [577, 5891, 2, 3184, 16, 2542, 5, 1710, 0] [1211, 3, 49, 9409, 1211, 3, 29140, 817, 3124, 817, 550, 7032, 5821, 7907, 12649, 0]``` We will pass this `data_collator` along to the `Seq2SeqTrainer`. Next, let’s have a look at the metric. ### [](#metrics)Metrics The feature that `Seq2SeqTrainer` adds to its superclass `Trainer` is the ability to use the `generate()` method during evaluation or prediction. During training, the model will use the `decoder_input_ids` with an attention mask ensuring it does not use the tokens after the token it’s trying to predict, to speed up training. During inference we won’t be able to use those since we won’t have labels, so it’s a good idea to evaluate our model with the same setup. As we saw in [Chapter 1](/course/chapter1/6), the decoder performs inference by predicting tokens one by one — something that’s implemented behind the scenes in 🤗 Transformers by the `generate()` method. The `Seq2SeqTrainer` will let us use that method for evaluation if we set `predict_with_generate=True`. The traditional metric used for translation is the [BLEU score](https://en.wikipedia.org/wiki/BLEU), introduced in [a 2002 article](https://aclanthology.org/P02-1040.pdf) by Kishore Papineni et al. The BLEU score evaluates how close the translations are to their labels. It does not measure the intelligibility or grammatical correctness of the model’s generated outputs, but uses statistical rules to ensure that all the words in the generated outputs also appear in the targets. In addition, there are rules that penalize repetitions of the same words if they are not also repeated in the targets (to avoid the model outputting sentences like `"the the the the the"`) and output sentences that are shorter than those in the targets (to avoid the model outputting sentences like `"the"`). One weakness with BLEU is that it expects the text to already be tokenized, which makes it difficult to compare scores between models that use different tokenizers. So instead, the most commonly used metric for benchmarking translation models today is [SacreBLEU](https://github.com/mjpost/sacrebleu), which addresses this weakness (and others) by standardizing the tokenization step. To use this metric, we first need to install the SacreBLEU library: We can then load it via `evaluate.load()` like we did in [Chapter 3](/course/chapter3): ``` import evaluate metric = evaluate.load("sacrebleu")``` This metric will take texts as inputs and targets. It is designed to accept several acceptable targets, as there are often multiple acceptable translations of the same sentence — the dataset we’re using only provides one, but it’s not uncommon in NLP to find datasets that give several sentences as labels. So, the predictions should be a list of sentences, but the references should be a list of lists of sentences. Let’s try an example: ``` predictions = [ "This plugin lets you translate web pages between several languages automatically." ] references = [ [ "This plugin allows you to automatically translate web pages between several languages." ] ] metric.compute(predictions=predictions, references=references)``` ``` {'score': 46.750469682990165, 'counts': [11, 6, 4, 3], 'totals': [12, 11, 10, 9], 'precisions': [91.67, 54.54, 40.0, 33.33], 'bp': 0.9200444146293233, 'sys_len': 12, 'ref_len': 13}``` This gets a BLEU score of 46.75, which is rather good — for reference, the original Transformer model in the [“Attention Is All You Need” paper](https://arxiv.org/pdf/1706.03762.pdf) achieved a BLEU score of 41.8 on a similar translation task between English and French! (For more information about the individual metrics, like `counts` and `bp`, see the [SacreBLEU repository](https://github.com/mjpost/sacrebleu/blob/078c440168c6adc89ba75fe6d63f0d922d42bcfe/sacrebleu/metrics/bleu.py#L74).) On the other hand, if we try with the two bad types of predictions (lots of repetitions or too short) that often come out of translation models, we will get rather bad BLEU scores: ``` predictions = ["This This This This"] references = [ [ "This plugin allows you to automatically translate web pages between several languages." ] ] metric.compute(predictions=predictions, references=references)``` ``` {'score': 1.683602693167689, 'counts': [1, 0, 0, 0], 'totals': [4, 3, 2, 1], 'precisions': [25.0, 16.67, 12.5, 12.5], 'bp': 0.10539922456186433, 'sys_len': 4, 'ref_len': 13}``` ``` predictions = ["This plugin"] references = [ [ "This plugin allows you to automatically translate web pages between several languages." ] ] metric.compute(predictions=predictions, references=references)``` ``` {'score': 0.0, 'counts': [2, 1, 0, 0], 'totals': [2, 1, 0, 0], 'precisions': [100.0, 100.0, 0.0, 0.0], 'bp': 0.004086771438464067, 'sys_len': 2, 'ref_len': 13}``` The score can go from 0 to 100, and higher is better. To get from the model outputs to texts the metric can use, we will use the `tokenizer.batch_decode()` method. We just have to clean up all the `-100`s in the labels (the tokenizer will automatically do the same for the padding token): ``` import numpy as np def compute_metrics(eval_preds): preds, labels = eval_preds if isinstance(preds, tuple): preds = preds[0] decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True) labels = np.where(labels != -100, labels, tokenizer.pad_token_id) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) decoded_preds = [pred.strip() for pred in decoded_preds] decoded_labels = [[label.strip()] for label in decoded_labels] result = metric.compute(predictions=decoded_preds, references=decoded_labels) return {"bleu": result["score"]}``` Now that this is done, we are ready to fine-tune our model! ### [](#fine-tuning-the-model)Fine-tuning the model The first step is to log in to Hugging Face, so you’re able to upload your results to the Model Hub. There’s a convenience function to help you with this in a notebook: ``` from huggingface_hub import notebook_login notebook_login()``` This will display a widget where you can enter your Hugging Face login credentials. If you aren’t working in a notebook, just type the following line in your terminal: Once this is done, we can define our `Seq2SeqTrainingArguments`. Like for the `Trainer`, we use a subclass of `TrainingArguments` that contains a few more fields: ``` from transformers import Seq2SeqTrainingArguments args = Seq2SeqTrainingArguments( f"marian-finetuned-kde4-en-to-fr", evaluation_strategy="no", save_strategy="epoch", learning_rate=2e-5, per_device_train_batch_size=32, per_device_eval_batch_size=64, weight_decay=0.01, save_total_limit=3, num_train_epochs=3, predict_with_generate=True, fp16=True, push_to_hub=True, )``` Apart from the usual hyperparameters (like learning rate, number of epochs, batch size, and some weight decay), here are a few changes compared to what we saw in the previous sections: - We don’t set any regular evaluation, as evaluation takes a while; we will just evaluate our model once before training and after. - We set `fp16=True`, which speeds up training on modern GPUs. - We set `predict_with_generate=True`, as discussed above. - We use `push_to_hub=True` to upload the model to the Hub at the end of each epoch. Note that you can specify the full name of the repository you want to push to with the `hub_model_id` argument (in particular, you will have to use this argument to push to an organization). For instance, when we pushed the model to the [`huggingface-course` organization](https://huggingface.co/huggingface-course), we added `hub_model_id="huggingface-course/marian-finetuned-kde4-en-to-fr"` to `Seq2SeqTrainingArguments`. By default, the repository used will be in your namespace and named after the output directory you set, so in our case it will be `"sgugger/marian-finetuned-kde4-en-to-fr"` (which is the model we linked to at the beginning of this section). 💡 If the output directory you are using already exists, it needs to be a local clone of the repository you want to push to. If it isn’t, you’ll get an error when defining your `Seq2SeqTrainer` and will need to set a new name. Finally, we just pass everything to the `Seq2SeqTrainer`: ``` from transformers import Seq2SeqTrainer trainer = Seq2SeqTrainer( model, args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=data_collator, tokenizer=tokenizer, compute_metrics=compute_metrics, )``` Before training, we’ll first look at the score our model gets, to double-check that we’re not making things worse with our fine-tuning. This command will take a bit of time, so you can grab a coffee while it executes: ``` trainer.evaluate(max_length=max_length)``` ``` {'eval_loss': 1.6964408159255981, 'eval_bleu': 39.26865061007616, 'eval_runtime': 965.8884, 'eval_samples_per_second': 21.76, 'eval_steps_per_second': 0.341}``` A BLEU score of 39 is not too bad, which reflects the fact that our model is already good at translating English sentences to French ones. Next is the training, which will also take a bit of time: Note that while the training happens, each time the model is saved (here, every epoch) it is uploaded to the Hub in the background. This way, you will be able to to resume your training on another machine if necessary. Once training is done, we evaluate our model again — hopefully we will see some amelioration in the BLEU score! ``` trainer.evaluate(max_length=max_length)``` ``` {'eval_loss': 0.8558505773544312, 'eval_bleu': 52.94161337775576, 'eval_runtime': 714.2576, 'eval_samples_per_second': 29.426, 'eval_steps_per_second': 0.461, 'epoch': 3.0}``` That’s a nearly 14-point improvement, which is great. Finally, we use the `push_to_hub()` method to make sure we upload the latest version of the model. The `Trainer` also drafts a model card with all the evaluation results and uploads it. This model card contains metadata that helps the Model Hub pick the widget for the inference demo. Usually, there is no need to say anything as it can infer the right widget from the model class, but in this case, the same model class can be used for all kinds of sequence-to-sequence problems, so we specify it’s a translation model: ``` trainer.push_to_hub(tags="translation", commit_message="Training complete")``` This command returns the URL of the commit it just did, if you want to inspect it: ``` 'https://huggingface.co/sgugger/marian-finetuned-kde4-en-to-fr/commit/3601d621e3baae2bc63d3311452535f8f58f6ef3'``` At this stage, you can use the inference widget on the Model Hub to test your model and share it with your friends. You have successfully fine-tuned a model on a translation task — congratulations! If you want to dive a bit more deeply into the training loop, we will now show you how to do the same thing using 🤗 Accelerate. ## [](#a-custom-training-loop)A custom training loop Let’s now take a look at the full training loop, so you can easily customize the parts you need. It will look a lot like what we did in [section 2](/course/chapter7/2) and [Chapter 3](/course/chapter3/4). ### [](#preparing-everything-for-training)Preparing everything for training You’ve seen all of this a few times now, so we’ll go through the code quite quickly. First we’ll build the `DataLoader`s from our datasets, after setting the datasets to the `"torch"` format so we get PyTorch tensors: ``` from torch.utils.data import DataLoader tokenized_datasets.set_format("torch") train_dataloader = DataLoader( tokenized_datasets["train"], shuffle=True, collate_fn=data_collator, batch_size=8, ) eval_dataloader = DataLoader( tokenized_datasets["validation"], collate_fn=data_collator, batch_size=8 )``` Next we reinstantiate our model, to make sure we’re not continuing the fine-tuning from before but starting from the pretrained model again: ``` model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)``` Then we will need an optimizer: ``` from transformers import AdamW optimizer = AdamW(model.parameters(), lr=2e-5)``` Once we have all those objects, we can send them to the `accelerator.prepare()` method. Remember that if you want to train on TPUs in a Colab notebook, you will need to move all of this code into a training function, and that shouldn’t execute any cell that instantiates an `Accelerator`. ``` from accelerate import Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )``` Now that we have sent our `train_dataloader` to `accelerator.prepare()`, we can use its length to compute the number of training steps. Remember we should always do this after preparing the dataloader, as that method will change the length of the `DataLoader`. We use a classic linear schedule from the learning rate to 0: ``` from transformers import get_scheduler num_train_epochs = 3 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, )``` Lastly, to push our model to the Hub, we will need to create a `Repository` object in a working folder. First log in to the Hugging Face Hub, if you’re not logged in already. We’ll determine the repository name from the model ID we want to give our model (feel free to replace the `repo_name` with your own choice; it just needs to contain your username, which is what the function `get_full_repo_name()` does): ``` from huggingface_hub import Repository, get_full_repo_name model_name = "marian-finetuned-kde4-en-to-fr-accelerate" repo_name = get_full_repo_name(model_name) repo_name``` ``` 'sgugger/marian-finetuned-kde4-en-to-fr-accelerate'``` Then we can clone that repository in a local folder. If it already exists, this local folder should be a clone of the repository we are working with: ``` output_dir = "marian-finetuned-kde4-en-to-fr-accelerate" repo = Repository(output_dir, clone_from=repo_name)``` We can now upload anything we save in `output_dir` by calling the `repo.push_to_hub()` method. This will help us upload the intermediate models at the end of each epoch. ### [](#training-loop)Training loop We are now ready to write the full training loop. To simplify its evaluation part, we define this `postprocess()` function that takes predictions and labels and converts them to the lists of strings our `metric` object will expect: ``` def postprocess(predictions, labels): predictions = predictions.cpu().numpy() labels = labels.cpu().numpy() decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True) labels = np.where(labels != -100, labels, tokenizer.pad_token_id) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) decoded_preds = [pred.strip() for pred in decoded_preds] decoded_labels = [[label.strip()] for label in decoded_labels] return decoded_preds, decoded_labels``` The training loop looks a lot like the ones in [section 2](/course/chapter7/2) and [Chapter 3](/course/chapter3), with a few differences in the evaluation part — so let’s focus on that! The first thing to note is that we use the `generate()` method to compute predictions, but this is a method on our base model, not the wrapped model 🤗 Accelerate created in the `prepare()` method. That’s why we unwrap the model first, then call this method. The second thing is that, like with [token classification](/course/chapter7/2), two processes may have padded the inputs and labels to different shapes, so we use `accelerator.pad_across_processes()` to make the predictions and labels the same shape before calling the `gather()` method. If we don’t do this, the evaluation will either error out or hang forever. ``` from tqdm.auto import tqdm import torch progress_bar = tqdm(range(num_training_steps)) for epoch in range(num_train_epochs): model.train() for batch in train_dataloader: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) model.eval() for batch in tqdm(eval_dataloader): with torch.no_grad(): generated_tokens = accelerator.unwrap_model(model).generate( batch["input_ids"], attention_mask=batch["attention_mask"], max_length=128, ) labels = batch["labels"] generated_tokens = accelerator.pad_across_processes( generated_tokens, dim=1, pad_index=tokenizer.pad_token_id ) labels = accelerator.pad_across_processes(labels, dim=1, pad_index=-100) predictions_gathered = accelerator.gather(generated_tokens) labels_gathered = accelerator.gather(labels) decoded_preds, decoded_labels = postprocess(predictions_gathered, labels_gathered) metric.add_batch(predictions=decoded_preds, references=decoded_labels) results = metric.compute() print(f"epoch {epoch}, BLEU score: {results['score']:.2f}") accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=f"Training in progress epoch {epoch}", blocking=False )``` ``` epoch 0, BLEU score: 53.47 epoch 1, BLEU score: 54.24 epoch 2, BLEU score: 54.44``` Once this is done, you should have a model that has results pretty similar to the one trained with the `Seq2SeqTrainer`. You can check the one we trained using this code at [_huggingface-course/marian-finetuned-kde4-en-to-fr-accelerate_](https://huggingface.co/huggingface-course/marian-finetuned-kde4-en-to-fr-accelerate). And if you want to test out any tweaks to the training loop, you can directly implement them by editing the code shown above! ## [](#using-the-fine-tuned-model)Using the fine-tuned model We’ve already shown you how you can use the model we fine-tuned on the Model Hub with the inference widget. To use it locally in a `pipeline`, we just have to specify the proper model identifier: ``` from transformers import pipeline model_checkpoint = "huggingface-course/marian-finetuned-kde4-en-to-fr" translator = pipeline("translation", model=model_checkpoint) translator("Default to expanded threads")``` ``` [{'translation_text': 'Par défaut, développer les fils de discussion'}]``` As expected, our pretrained model adapted its knowledge to the corpus we fine-tuned it on, and instead of leaving the English word “threads” alone, it now translates it to the French official version. It’s the same for “plugin”: ``` translator( "Unable to import %1 using the OFX importer plugin. This file is not the correct format." )``` ``` [{'translation_text': "Impossible d'importer %1 en utilisant le module externe d'importation OFX. Ce fichier n'est pas le bon format."}]``` Another great example of domain adaptation! ✏️ **Your turn!** What does the model return on the sample with the word “email” you identified earlier?
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Translation - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter7/4"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Translation - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;translation&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;preparing-the-data&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;the-kde4-dataset&quot;,&quot;title&quot;:&quot;The KDE4 dataset&quot;},{&quot;local&quot;:&quot;processing-the-data&quot;,&quot;title&quot;:&quot;Processing the data&quot;}],&quot;title&quot;:&quot;Preparing the data&quot;},{&quot;local&quot;:&quot;fine-tuning-the-model-with-the-trainer-api&quot;,&quot;title&quot;:&quot;Fine-tuning the model with the `Trainer` API&quot;},{&quot;local&quot;:&quot;fine-tuning-the-model-with-keras&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;data-collation&quot;,&quot;title&quot;:&quot;Data collation&quot;},{&quot;local&quot;:&quot;metrics&quot;,&quot;title&quot;:&quot;Metrics&quot;},{&quot;local&quot;:&quot;fine-tuning-the-model&quot;,&quot;title&quot;:&quot;Fine-tuning the model&quot;}],&quot;title&quot;:&quot;Fine-tuning the model with Keras&quot;},{&quot;local&quot;:&quot;a-custom-training-loop&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;preparing-everything-for-training&quot;,&quot;title&quot;:&quot;Preparing everything for training&quot;},{&quot;local&quot;:&quot;training-loop&quot;,&quot;title&quot;:&quot;Training loop&quot;}],&quot;title&quot;:&quot;A custom training loop&quot;},{&quot;local&quot;:&quot;using-the-fine-tuned-model&quot;,&quot;title&quot;:&quot;Using the fine-tuned model&quot;}],&quot;title&quot;:&quot;Translation&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter7/4&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Translation&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Translation</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/2?fw=pt">Token classification </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/3?fw=pt">Fine-tuning a masked language model </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter7/4?fw=pt">Translation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/5?fw=pt">Summarization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/6?fw=pt">Training a causal language model from scratch </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/7?fw=pt">Question answering </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/8?fw=pt">Mastering NLP </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="translation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#translation"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Translation</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-7-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section4_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section4_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>Let’s now dive into translation. This is another <a href="/course/chapter1/7">sequence-to-sequence task</a>, which means it’s a problem that can be formulated as going from one sequence to another. In that sense the problem is pretty close to <a href="/course/chapter7/6">summarization</a>, and you could adapt what we will see here to other sequence-to-sequence problems such as:</p> <ul><li><strong>Style transfer</strong>: Creating a model that <em>translates</em> texts written in a certain style to another (e.g., formal to casual or Shakespearean English to modern English)</li> <li><strong>Generative question answering</strong>: Creating a model that generates answers to questions, given a context</li></ul> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/1JvfrvZgi6c" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>If you have a big enough corpus of texts in two (or more) languages, you can train a new translation model from scratch like we will in the section on <a href="/course/chapter7/6">causal language modeling</a>. It will be faster, however, to fine-tune an existing translation model, be it a multilingual one like mT5 or mBART that you want to fine-tune to a specific language pair, or even a model specialized for translation from one language to another that you want to fine-tune to your specific corpus.</p> <p>In this section, we will fine-tune a Marian model pretrained to translate from English to French (since a lot of Hugging Face employees speak both those languages) on the <a href="https://huggingface.co/datasets/kde4" rel="nofollow">KDE4 dataset</a>, which is a dataset of localized files for the <a href="https://apps.kde.org/" rel="nofollow">KDE apps</a>. The model we will use has been pretrained on a large corpus of French and English texts taken from the <a href="https://opus.nlpl.eu/" rel="nofollow">Opus dataset</a>, which actually contains the KDE4 dataset. But even if the pretrained model we use has seen that data during its pretraining, we will see that we can get a better version of it after fine-tuning.</p> <p>Once we’re finished, we will have a model able to make predictions like this one:</p> <iframe src="https://course-demos-marian-finetuned-kde4-en-to-fr.hf.space" frameborder="0" height="350" title="Gradio app" class="block dark:hidden container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <a class="flex justify-center" href="/huggingface-course/marian-finetuned-kde4-en-to-fr"><img class="block dark:hidden lg:w-3/5" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/modeleval-marian-finetuned-kde4-en-to-fr.png" alt="One-hot encoded labels for question answering."> <img class="hidden dark:block lg:w-3/5" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/modeleval-marian-finetuned-kde4-en-to-fr-dark.png" alt="One-hot encoded labels for question answering."></a> <p>As in the previous sections, you can find the actual model that we’ll train and upload to the Hub using the code below and double-check its predictions <a href="https://huggingface.co/huggingface-course/marian-finetuned-kde4-en-to-fr?text=This+plugin+allows+you+to+automatically+translate+web+pages+between+several+languages." rel="nofollow">here</a>.</p> <h2 class="relative group"><a id="preparing-the-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preparing-the-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preparing the data</span></h2> <p>To fine-tune or train a translation model from scratch, we will need a dataset suitable for the task. As mentioned previously, we’ll use the <a href="https://huggingface.co/datasets/kde4" rel="nofollow">KDE4 dataset</a> in this section, but you can adapt the code to use your own data quite easily, as long as you have pairs of sentences in the two languages you want to translate from and into. Refer back to <a href="/course/chapter5">Chapter 5</a> if you need a reminder of how to load your custom data in a <code>Dataset</code>.</p> <h3 class="relative group"><a id="the-kde4-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-kde4-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The KDE4 dataset</span></h3> <p>As usual, we download our dataset using the <code>load_dataset()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset raw_datasets = load_dataset(<span class="hljs-string">"kde4"</span>, lang1=<span class="hljs-string">"en"</span>, lang2=<span class="hljs-string">"fr"</span>)</pre></div> <p>If you want to work with a different pair of languages, you can specify them by their codes. A total of 92 languages are available for this dataset; you can see them all by expanding the language tags on its <a href="https://huggingface.co/datasets/kde4" rel="nofollow">dataset card</a>.</p> <img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/language_tags.png" alt="Language available for the KDE4 dataset." width="100%"> <p>Let’s have a look at the dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>raw_datasets</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'id'</span>, <span class="hljs-string">'translation'</span>], num_rows: <span class="hljs-number">210173</span> }) })</pre></div> <p>We have 210,173 pairs of sentences, but in one single split, so we will need to create our own validation set. As we saw in <a href="/course/chapter5">Chapter 5</a>, a <code>Dataset</code> has a <code>train_test_split()</code> method that can help us. We’ll provide a seed for reproducibility:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>split_datasets = raw_datasets[<span class="hljs-string">"train"</span>].train_test_split(train_size=<span class="hljs-number">0.9</span>, seed=<span class="hljs-number">20</span>) split_datasets</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'id'</span>, <span class="hljs-string">'translation'</span>], num_rows: <span class="hljs-number">189155</span> }) test: Dataset({ features: [<span class="hljs-string">'id'</span>, <span class="hljs-string">'translation'</span>], num_rows: <span class="hljs-number">21018</span> }) })</pre></div> <p>We can rename the <code>"test"</code> key to <code>"validation"</code> like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>split_datasets[<span class="hljs-string">"validation"</span>] = split_datasets.pop(<span class="hljs-string">"test"</span>)</pre></div> <p>Now let’s take a look at one element of the dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>split_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">1</span>][<span class="hljs-string">"translation"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'en'</span>: <span class="hljs-string">'Default to expanded threads'</span>, <span class="hljs-string">'fr'</span>: <span class="hljs-string">'Par défaut, développer les fils de discussion'</span>}</pre></div> <p>We get a dictionary with two sentences in the pair of languages we requested. One particularity of this dataset full of technical computer science terms is that they are all fully translated in French. However, French engineers are often lazy and leave most computer science-specific words in English when they talk. Here, for instance, the word “threads” might well appear in a French sentence, especially in a technical conversation; but in this dataset it has been translated into the more correct “fils de discussion.” The pretrained model we use, which has been pretrained on a larger corpus of French and English sentences, takes the easier option of leaving the word as is:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline model_checkpoint = <span class="hljs-string">"Helsinki-NLP/opus-mt-en-fr"</span> translator = pipeline(<span class="hljs-string">"translation"</span>, model=model_checkpoint) translator(<span class="hljs-string">"Default to expanded threads"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'translation_text'</span>: <span class="hljs-string">'Par défaut pour les threads élargis'</span>}]</pre></div> <p>Another example of this behavior can be seen with the word “plugin,” which isn’t officially a French word but which most native speakers will understand and not bother to translate. In the KDE4 dataset this word has been translated in French into the more official “module d’extension”:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>split_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">172</span>][<span class="hljs-string">"translation"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'en'</span>: <span class="hljs-string">'Unable to import %1 using the OFX importer plugin. This file is not the correct format.'</span>, <span class="hljs-string">'fr'</span>: <span class="hljs-string">"Impossible d'importer %1 en utilisant le module d'extension d'importation OFX. Ce fichier n'a pas un format correct."</span>}</pre></div> <p>Our pretrained model, however, sticks with the compact and familiar English word:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>translator( <span class="hljs-string">"Unable to import %1 using the OFX importer plugin. This file is not the correct format."</span> )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'translation_text'</span>: <span class="hljs-string">"Impossible d'importer %1 en utilisant le plugin d'importateur OFX. Ce fichier n'est pas le bon format."</span>}]</pre></div> <p>It will be interesting to see if our fine-tuned model picks up on those particularities of the dataset (spoiler alert: it will).</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/0Oxphw4Q9fo" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Your turn!</strong> Another English word that is often used in French is “email.” Find the first sample in the training dataset that uses this word. How is it translated? How does the pretrained model translate the same English sentence?</p></div> <h3 class="relative group"><a id="processing-the-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#processing-the-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Processing the data</span></h3> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/XAR8jnZZuUs" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>You should know the drill by now: the texts all need to be converted into sets of token IDs so the model can make sense of them. For this task, we’ll need to tokenize both the inputs and the targets. Our first task is to create our <code>tokenizer</code> object. As noted earlier, we’ll be using a Marian English to French pretrained model. If you are trying this code with another pair of languages, make sure to adapt the model checkpoint. The <a href="https://huggingface.co/Helsinki-NLP" rel="nofollow">Helsinki-NLP</a> organization provides more than a thousand models in multiple languages.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer model_checkpoint = <span class="hljs-string">"Helsinki-NLP/opus-mt-en-fr"</span> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint, return_tensors=<span class="hljs-string">"pt"</span>)</pre></div> <p>You can also replace the <code>model_checkpoint</code> with any other model you prefer from the <a href="https://huggingface.co/models" rel="nofollow">Hub</a>, or a local folder where you’ve saved a pretrained model and a tokenizer.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 If you are using a multilingual tokenizer such as mBART, mBART-50, or M2M100, you will need to set the language codes of your inputs and targets in the tokenizer by setting <code>tokenizer.src_lang</code> and <code>tokenizer.tgt_lang</code> to the right values.</p></div> <p>The preparation of our data is pretty straightforward. There’s just one thing to remember; you need to ensure that the tokenizer processes the targets in the output language (here, French). You can do this by passing the targets to the <code>text_targets</code> argument of the tokenizer’s <code>__call__</code> method.</p> <p>To see how this works, let’s process one sample of each language in the training set:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>en_sentence = split_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">1</span>][<span class="hljs-string">"translation"</span>][<span class="hljs-string">"en"</span>] fr_sentence = split_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">1</span>][<span class="hljs-string">"translation"</span>][<span class="hljs-string">"fr"</span>] inputs = tokenizer(en_sentence, text_target=fr_sentence) inputs</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'input_ids'</span>: [<span class="hljs-number">47591</span>, <span class="hljs-number">12</span>, <span class="hljs-number">9842</span>, <span class="hljs-number">19634</span>, <span class="hljs-number">9</span>, <span class="hljs-number">0</span>], <span class="hljs-string">'attention_mask'</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>], <span class="hljs-string">'labels'</span>: [<span class="hljs-number">577</span>, <span class="hljs-number">5891</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3184</span>, <span class="hljs-number">16</span>, <span class="hljs-number">2542</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1710</span>, <span class="hljs-number">0</span>]}</pre></div> <p>As we can see, the output contains the input IDs associated with the English sentence, while the IDs associated with the French one are stored in the <code>labels</code> field. If you forget to indicate that you are tokenizing labels, they will be tokenized by the input tokenizer, which in the case of a Marian model is not going to go well at all:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>wrong_targets = tokenizer(fr_sentence) <span class="hljs-built_in">print</span>(tokenizer.convert_ids_to_tokens(wrong_targets[<span class="hljs-string">"input_ids"</span>])) <span class="hljs-built_in">print</span>(tokenizer.convert_ids_to_tokens(inputs[<span class="hljs-string">"labels"</span>]))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'▁Par'</span>, <span class="hljs-string">'▁dé'</span>, <span class="hljs-string">'f'</span>, <span class="hljs-string">'aut'</span>, <span class="hljs-string">','</span>, <span class="hljs-string">'▁dé'</span>, <span class="hljs-string">'ve'</span>, <span class="hljs-string">'lop'</span>, <span class="hljs-string">'per'</span>, <span class="hljs-string">'▁les'</span>, <span class="hljs-string">'▁fil'</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'▁de'</span>, <span class="hljs-string">'▁discussion'</span>, <span class="hljs-string">'&lt;/s&gt;'</span>] [<span class="hljs-string">'▁Par'</span>, <span class="hljs-string">'▁défaut'</span>, <span class="hljs-string">','</span>, <span class="hljs-string">'▁développer'</span>, <span class="hljs-string">'▁les'</span>, <span class="hljs-string">'▁fils'</span>, <span class="hljs-string">'▁de'</span>, <span class="hljs-string">'▁discussion'</span>, <span class="hljs-string">'&lt;/s&gt;'</span>]</pre></div> <p>As we can see, using the English tokenizer to preprocess a French sentence results in a lot more tokens, since the tokenizer doesn’t know any French words (except those that also appear in the English language, like “discussion”).</p> <p>Since <code>inputs</code> is a dictionary with our usual keys (input IDs, attention mask, etc.), the last step is to define the preprocessing function we will apply on the datasets:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>max_length = <span class="hljs-number">128</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_function</span>(<span class="hljs-params">examples</span>): inputs = [ex[<span class="hljs-string">"en"</span>] <span class="hljs-keyword">for</span> ex <span class="hljs-keyword">in</span> examples[<span class="hljs-string">"translation"</span>]] targets = [ex[<span class="hljs-string">"fr"</span>] <span class="hljs-keyword">for</span> ex <span class="hljs-keyword">in</span> examples[<span class="hljs-string">"translation"</span>]] model_inputs = tokenizer( inputs, text_target=targets, max_length=max_length, truncation=<span class="hljs-literal">True</span> ) <span class="hljs-keyword">return</span> model_inputs</pre></div> <p>Note that we set the same maximum length for our inputs and outputs. Since the texts we’re dealing with seem pretty short, we use 128.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 If you are using a T5 model (more specifically, one of the <code>t5-xxx</code> checkpoints), the model will expect the text inputs to have a prefix indicating the task at hand, such as <code>translate: English to French:</code>.</p></div> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>⚠️ We don’t pay attention to the attention mask of the targets, as the model won’t expect it. Instead, the labels corresponding to a padding token should be set to <code>-100</code> so they are ignored in the loss computation. This will be done by our data collator later on since we are applying dynamic padding, but if you use padding here, you should adapt the preprocessing function to set all labels that correspond to the padding token to <code>-100</code>.</p></div> <p>We can now apply that preprocessing in one go on all the splits of our dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_datasets = split_datasets.<span class="hljs-built_in">map</span>( preprocess_function, batched=<span class="hljs-literal">True</span>, remove_columns=split_datasets[<span class="hljs-string">"train"</span>].column_names, )</pre></div> <p>Now that the data has been preprocessed, we are ready to fine-tune our pretrained model!</p> <h2 class="relative group"><a id="fine-tuning-the-model-with-the-trainer-api" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-the-model-with-the-trainer-api"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning the model with the <code>Trainer</code> API</span></h2> <p>The actual code using the <code>Trainer</code> will be the same as before, with just one little change: we use a <a href="https://huggingface.co/transformers/main_classes/trainer.html#seq2seqtrainer" rel="nofollow"><code>Seq2SeqTrainer</code></a> here, which is a subclass of <code>Trainer</code> that will allow us to properly deal with the evaluation, using the <code>generate()</code> method to predict outputs from the inputs. We’ll dive into that in more detail when we talk about the metric computation.</p> <p>First things first, we need an actual model to fine-tune. We’ll use the usual <code>AutoModel</code> API:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSeq2SeqLM model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)</pre></div> <p>Note that this time we are using a model that was trained on a translation task and can actually be used already, so there is no warning about missing weights or newly initialized ones.</p> <h3 class="relative group"><a id="data-collation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#data-collation"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Data collation</span></h3> <p>We’ll need a data collator to deal with the padding for dynamic batching. We can’t just use a <code>DataCollatorWithPadding</code> like in <a href="/course/chapter3">Chapter 3</a> in this case, because that only pads the inputs (input IDs, attention mask, and token type IDs). Our labels should also be padded to the maximum length encountered in the labels. And, as mentioned previously, the padding value used to pad the labels should be <code>-100</code> and not the padding token of the tokenizer, to make sure those padded values are ignored in the loss computation.</p> <p>This is all done by a <a href="https://huggingface.co/transformers/main_classes/data_collator.html#datacollatorforseq2seq" rel="nofollow"><code>DataCollatorForSeq2Seq</code></a>. Like the <code>DataCollatorWithPadding</code>, it takes the <code>tokenizer</code> used to preprocess the inputs, but it also takes the <code>model</code>. This is because this data collator will also be responsible for preparing the decoder input IDs, which are shifted versions of the labels with a special token at the beginning. Since this shift is done slightly differently for different architectures, the <code>DataCollatorForSeq2Seq</code> needs to know the <code>model</code> object:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForSeq2Seq data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)</pre></div> <p>To test this on a few samples, we just call it on a list of examples from our tokenized training set:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>batch = data_collator([tokenized_datasets[<span class="hljs-string">"train"</span>][i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>)]) batch.keys()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>dict_keys([<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'labels'</span>, <span class="hljs-string">'decoder_input_ids'</span>])</pre></div> <p>We can check our labels have been padded to the maximum length of the batch, using <code>-100</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>batch[<span class="hljs-string">"labels"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tensor([[ <span class="hljs-number">577</span>, <span class="hljs-number">5891</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3184</span>, <span class="hljs-number">16</span>, <span class="hljs-number">2542</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1710</span>, <span class="hljs-number">0</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>, -<span class="hljs-number">100</span>], [ <span class="hljs-number">1211</span>, <span class="hljs-number">3</span>, <span class="hljs-number">49</span>, <span class="hljs-number">9409</span>, <span class="hljs-number">1211</span>, <span class="hljs-number">3</span>, <span class="hljs-number">29140</span>, <span class="hljs-number">817</span>, <span class="hljs-number">3124</span>, <span class="hljs-number">817</span>, <span class="hljs-number">550</span>, <span class="hljs-number">7032</span>, <span class="hljs-number">5821</span>, <span class="hljs-number">7907</span>, <span class="hljs-number">12649</span>, <span class="hljs-number">0</span>]])</pre></div> <p>And we can also have a look at the decoder input IDs, to see that they are shifted versions of the labels:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>batch[<span class="hljs-string">"decoder_input_ids"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tensor([[<span class="hljs-number">59513</span>, <span class="hljs-number">577</span>, <span class="hljs-number">5891</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3184</span>, <span class="hljs-number">16</span>, <span class="hljs-number">2542</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1710</span>, <span class="hljs-number">0</span>, <span class="hljs-number">59513</span>, <span class="hljs-number">59513</span>, <span class="hljs-number">59513</span>, <span class="hljs-number">59513</span>, <span class="hljs-number">59513</span>, <span class="hljs-number">59513</span>], [<span class="hljs-number">59513</span>, <span class="hljs-number">1211</span>, <span class="hljs-number">3</span>, <span class="hljs-number">49</span>, <span class="hljs-number">9409</span>, <span class="hljs-number">1211</span>, <span class="hljs-number">3</span>, <span class="hljs-number">29140</span>, <span class="hljs-number">817</span>, <span class="hljs-number">3124</span>, <span class="hljs-number">817</span>, <span class="hljs-number">550</span>, <span class="hljs-number">7032</span>, <span class="hljs-number">5821</span>, <span class="hljs-number">7907</span>, <span class="hljs-number">12649</span>]])</pre></div> <p>Here are the labels for the first and second elements in our dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>): <span class="hljs-built_in">print</span>(tokenized_datasets[<span class="hljs-string">"train"</span>][i][<span class="hljs-string">"labels"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">577</span>, <span class="hljs-number">5891</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3184</span>, <span class="hljs-number">16</span>, <span class="hljs-number">2542</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1710</span>, <span class="hljs-number">0</span>] [<span class="hljs-number">1211</span>, <span class="hljs-number">3</span>, <span class="hljs-number">49</span>, <span class="hljs-number">9409</span>, <span class="hljs-number">1211</span>, <span class="hljs-number">3</span>, <span class="hljs-number">29140</span>, <span class="hljs-number">817</span>, <span class="hljs-number">3124</span>, <span class="hljs-number">817</span>, <span class="hljs-number">550</span>, <span class="hljs-number">7032</span>, <span class="hljs-number">5821</span>, <span class="hljs-number">7907</span>, <span class="hljs-number">12649</span>, <span class="hljs-number">0</span>]</pre></div> <p>We will pass this <code>data_collator</code> along to the <code>Seq2SeqTrainer</code>. Next, let’s have a look at the metric.</p> <h3 class="relative group"><a id="metrics" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#metrics"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Metrics</span></h3> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/M05L1DhFqcw" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The feature that <code>Seq2SeqTrainer</code> adds to its superclass <code>Trainer</code> is the ability to use the <code>generate()</code> method during evaluation or prediction. During training, the model will use the <code>decoder_input_ids</code> with an attention mask ensuring it does not use the tokens after the token it’s trying to predict, to speed up training. During inference we won’t be able to use those since we won’t have labels, so it’s a good idea to evaluate our model with the same setup.</p> <p>As we saw in <a href="/course/chapter1/6">Chapter 1</a>, the decoder performs inference by predicting tokens one by one — something that’s implemented behind the scenes in 🤗 Transformers by the <code>generate()</code> method. The <code>Seq2SeqTrainer</code> will let us use that method for evaluation if we set <code>predict_with_generate=True</code>.</p> <p>The traditional metric used for translation is the <a href="https://en.wikipedia.org/wiki/BLEU" rel="nofollow">BLEU score</a>, introduced in <a href="https://aclanthology.org/P02-1040.pdf" rel="nofollow">a 2002 article</a> by Kishore Papineni et al. The BLEU score evaluates how close the translations are to their labels. It does not measure the intelligibility or grammatical correctness of the model’s generated outputs, but uses statistical rules to ensure that all the words in the generated outputs also appear in the targets. In addition, there are rules that penalize repetitions of the same words if they are not also repeated in the targets (to avoid the model outputting sentences like <code>"the the the the the"</code>) and output sentences that are shorter than those in the targets (to avoid the model outputting sentences like <code>"the"</code>).</p> <p>One weakness with BLEU is that it expects the text to already be tokenized, which makes it difficult to compare scores between models that use different tokenizers. So instead, the most commonly used metric for benchmarking translation models today is <a href="https://github.com/mjpost/sacrebleu" rel="nofollow">SacreBLEU</a>, which addresses this weakness (and others) by standardizing the tokenization step. To use this metric, we first need to install the SacreBLEU library:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!pip install sacrebleu</pre></div> <p>We can then load it via <code>evaluate.load()</code> like we did in <a href="/course/chapter3">Chapter 3</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> evaluate metric = evaluate.load(<span class="hljs-string">"sacrebleu"</span>)</pre></div> <p>This metric will take texts as inputs and targets. It is designed to accept several acceptable targets, as there are often multiple acceptable translations of the same sentence — the dataset we’re using only provides one, but it’s not uncommon in NLP to find datasets that give several sentences as labels. So, the predictions should be a list of sentences, but the references should be a list of lists of sentences.</p> <p>Let’s try an example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>predictions = [ <span class="hljs-string">"This plugin lets you translate web pages between several languages automatically."</span> ] references = [ [ <span class="hljs-string">"This plugin allows you to automatically translate web pages between several languages."</span> ] ] metric.compute(predictions=predictions, references=references)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'score'</span>: <span class="hljs-number">46.750469682990165</span>, <span class="hljs-string">'counts'</span>: [<span class="hljs-number">11</span>, <span class="hljs-number">6</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>], <span class="hljs-string">'totals'</span>: [<span class="hljs-number">12</span>, <span class="hljs-number">11</span>, <span class="hljs-number">10</span>, <span class="hljs-number">9</span>], <span class="hljs-string">'precisions'</span>: [<span class="hljs-number">91.67</span>, <span class="hljs-number">54.54</span>, <span class="hljs-number">40.0</span>, <span class="hljs-number">33.33</span>], <span class="hljs-string">'bp'</span>: <span class="hljs-number">0.9200444146293233</span>, <span class="hljs-string">'sys_len'</span>: <span class="hljs-number">12</span>, <span class="hljs-string">'ref_len'</span>: <span class="hljs-number">13</span>}</pre></div> <p>This gets a BLEU score of 46.75, which is rather good — for reference, the original Transformer model in the <a href="https://arxiv.org/pdf/1706.03762.pdf" rel="nofollow">“Attention Is All You Need” paper</a> achieved a BLEU score of 41.8 on a similar translation task between English and French! (For more information about the individual metrics, like <code>counts</code> and <code>bp</code>, see the <a href="https://github.com/mjpost/sacrebleu/blob/078c440168c6adc89ba75fe6d63f0d922d42bcfe/sacrebleu/metrics/bleu.py#L74" rel="nofollow">SacreBLEU repository</a>.) On the other hand, if we try with the two bad types of predictions (lots of repetitions or too short) that often come out of translation models, we will get rather bad BLEU scores:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>predictions = [<span class="hljs-string">"This This This This"</span>] references = [ [ <span class="hljs-string">"This plugin allows you to automatically translate web pages between several languages."</span> ] ] metric.compute(predictions=predictions, references=references)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'score'</span>: <span class="hljs-number">1.683602693167689</span>, <span class="hljs-string">'counts'</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], <span class="hljs-string">'totals'</span>: [<span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>], <span class="hljs-string">'precisions'</span>: [<span class="hljs-number">25.0</span>, <span class="hljs-number">16.67</span>, <span class="hljs-number">12.5</span>, <span class="hljs-number">12.5</span>], <span class="hljs-string">'bp'</span>: <span class="hljs-number">0.10539922456186433</span>, <span class="hljs-string">'sys_len'</span>: <span class="hljs-number">4</span>, <span class="hljs-string">'ref_len'</span>: <span class="hljs-number">13</span>}</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>predictions = [<span class="hljs-string">"This plugin"</span>] references = [ [ <span class="hljs-string">"This plugin allows you to automatically translate web pages between several languages."</span> ] ] metric.compute(predictions=predictions, references=references)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'score'</span>: <span class="hljs-number">0.0</span>, <span class="hljs-string">'counts'</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], <span class="hljs-string">'totals'</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], <span class="hljs-string">'precisions'</span>: [<span class="hljs-number">100.0</span>, <span class="hljs-number">100.0</span>, <span class="hljs-number">0.0</span>, <span class="hljs-number">0.0</span>], <span class="hljs-string">'bp'</span>: <span class="hljs-number">0.004086771438464067</span>, <span class="hljs-string">'sys_len'</span>: <span class="hljs-number">2</span>, <span class="hljs-string">'ref_len'</span>: <span class="hljs-number">13</span>}</pre></div> <p>The score can go from 0 to 100, and higher is better.</p> <p>To get from the model outputs to texts the metric can use, we will use the <code>tokenizer.batch_decode()</code> method. We just have to clean up all the <code>-100</code>s in the labels (the tokenizer will automatically do the same for the padding token):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_preds</span>): preds, labels = eval_preds <span class="hljs-comment"># In case the model returns more than the prediction logits</span> <span class="hljs-keyword">if</span> <span class="hljs-built_in">isinstance</span>(preds, <span class="hljs-built_in">tuple</span>): preds = preds[<span class="hljs-number">0</span>] decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=<span class="hljs-literal">True</span>) <span class="hljs-comment"># Replace -100s in the labels as we can't decode them</span> labels = np.where(labels != -<span class="hljs-number">100</span>, labels, tokenizer.pad_token_id) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=<span class="hljs-literal">True</span>) <span class="hljs-comment"># Some simple post-processing</span> decoded_preds = [pred.strip() <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> decoded_preds] decoded_labels = [[label.strip()] <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> decoded_labels] result = metric.compute(predictions=decoded_preds, references=decoded_labels) <span class="hljs-keyword">return</span> {<span class="hljs-string">"bleu"</span>: result[<span class="hljs-string">"score"</span>]}</pre></div> <p>Now that this is done, we are ready to fine-tune our model!</p> <h3 class="relative group"><a id="fine-tuning-the-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-the-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning the model</span></h3> <p>The first step is to log in to Hugging Face, so you’re able to upload your results to the Model Hub. There’s a convenience function to help you with this in a notebook:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login notebook_login()</pre></div> <p>This will display a widget where you can enter your Hugging Face login credentials.</p> <p>If you aren’t working in a notebook, just type the following line in your terminal:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-cli login</pre></div> <p>Once this is done, we can define our <code>Seq2SeqTrainingArguments</code>. Like for the <code>Trainer</code>, we use a subclass of <code>TrainingArguments</code> that contains a few more fields:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Seq2SeqTrainingArguments args = Seq2SeqTrainingArguments( <span class="hljs-string">f"marian-finetuned-kde4-en-to-fr"</span>, evaluation_strategy=<span class="hljs-string">"no"</span>, save_strategy=<span class="hljs-string">"epoch"</span>, learning_rate=<span class="hljs-number">2e-5</span>, per_device_train_batch_size=<span class="hljs-number">32</span>, per_device_eval_batch_size=<span class="hljs-number">64</span>, weight_decay=<span class="hljs-number">0.01</span>, save_total_limit=<span class="hljs-number">3</span>, num_train_epochs=<span class="hljs-number">3</span>, predict_with_generate=<span class="hljs-literal">True</span>, fp16=<span class="hljs-literal">True</span>, push_to_hub=<span class="hljs-literal">True</span>, )</pre></div> <p>Apart from the usual hyperparameters (like learning rate, number of epochs, batch size, and some weight decay), here are a few changes compared to what we saw in the previous sections:</p> <ul><li>We don’t set any regular evaluation, as evaluation takes a while; we will just evaluate our model once before training and after.</li> <li>We set <code>fp16=True</code>, which speeds up training on modern GPUs.</li> <li>We set <code>predict_with_generate=True</code>, as discussed above.</li> <li>We use <code>push_to_hub=True</code> to upload the model to the Hub at the end of each epoch.</li></ul> <p>Note that you can specify the full name of the repository you want to push to with the <code>hub_model_id</code> argument (in particular, you will have to use this argument to push to an organization). For instance, when we pushed the model to the <a href="https://huggingface.co/huggingface-course" rel="nofollow"><code>huggingface-course</code> organization</a>, we added <code>hub_model_id="huggingface-course/marian-finetuned-kde4-en-to-fr"</code> to <code>Seq2SeqTrainingArguments</code>. By default, the repository used will be in your namespace and named after the output directory you set, so in our case it will be <code>"sgugger/marian-finetuned-kde4-en-to-fr"</code> (which is the model we linked to at the beginning of this section).</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 If the output directory you are using already exists, it needs to be a local clone of the repository you want to push to. If it isn’t, you’ll get an error when defining your <code>Seq2SeqTrainer</code> and will need to set a new name.</p></div> <p>Finally, we just pass everything to the <code>Seq2SeqTrainer</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Seq2SeqTrainer trainer = Seq2SeqTrainer( model, args, train_dataset=tokenized_datasets[<span class="hljs-string">"train"</span>], eval_dataset=tokenized_datasets[<span class="hljs-string">"validation"</span>], data_collator=data_collator, tokenizer=tokenizer, compute_metrics=compute_metrics, )</pre></div> <p>Before training, we’ll first look at the score our model gets, to double-check that we’re not making things worse with our fine-tuning. This command will take a bit of time, so you can grab a coffee while it executes:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.evaluate(max_length=max_length)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'eval_loss'</span>: <span class="hljs-number">1.6964408159255981</span>, <span class="hljs-string">'eval_bleu'</span>: <span class="hljs-number">39.26865061007616</span>, <span class="hljs-string">'eval_runtime'</span>: <span class="hljs-number">965.8884</span>, <span class="hljs-string">'eval_samples_per_second'</span>: <span class="hljs-number">21.76</span>, <span class="hljs-string">'eval_steps_per_second'</span>: <span class="hljs-number">0.341</span>}</pre></div> <p>A BLEU score of 39 is not too bad, which reflects the fact that our model is already good at translating English sentences to French ones.</p> <p>Next is the training, which will also take a bit of time:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.train()</pre></div> <p>Note that while the training happens, each time the model is saved (here, every epoch) it is uploaded to the Hub in the background. This way, you will be able to to resume your training on another machine if necessary.</p> <p>Once training is done, we evaluate our model again — hopefully we will see some amelioration in the BLEU score!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.evaluate(max_length=max_length)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'eval_loss'</span>: <span class="hljs-number">0.8558505773544312</span>, <span class="hljs-string">'eval_bleu'</span>: <span class="hljs-number">52.94161337775576</span>, <span class="hljs-string">'eval_runtime'</span>: <span class="hljs-number">714.2576</span>, <span class="hljs-string">'eval_samples_per_second'</span>: <span class="hljs-number">29.426</span>, <span class="hljs-string">'eval_steps_per_second'</span>: <span class="hljs-number">0.461</span>, <span class="hljs-string">'epoch'</span>: <span class="hljs-number">3.0</span>}</pre></div> <p>That’s a nearly 14-point improvement, which is great.</p> <p>Finally, we use the <code>push_to_hub()</code> method to make sure we upload the latest version of the model. The <code>Trainer</code> also drafts a model card with all the evaluation results and uploads it. This model card contains metadata that helps the Model Hub pick the widget for the inference demo. Usually, there is no need to say anything as it can infer the right widget from the model class, but in this case, the same model class can be used for all kinds of sequence-to-sequence problems, so we specify it’s a translation model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.push_to_hub(tags=<span class="hljs-string">"translation"</span>, commit_message=<span class="hljs-string">"Training complete"</span>)</pre></div> <p>This command returns the URL of the commit it just did, if you want to inspect it:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'https://huggingface.co/sgugger/marian-finetuned-kde4-en-to-fr/commit/3601d621e3baae2bc63d3311452535f8f58f6ef3'</span></pre></div> <p>At this stage, you can use the inference widget on the Model Hub to test your model and share it with your friends. You have successfully fine-tuned a model on a translation task — congratulations!</p> <p>If you want to dive a bit more deeply into the training loop, we will now show you how to do the same thing using 🤗 Accelerate.</p> <h2 class="relative group"><a id="a-custom-training-loop" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#a-custom-training-loop"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>A custom training loop</span></h2> <p>Let’s now take a look at the full training loop, so you can easily customize the parts you need. It will look a lot like what we did in <a href="/course/chapter7/2">section 2</a> and <a href="/course/chapter3/4">Chapter 3</a>.</p> <h3 class="relative group"><a id="preparing-everything-for-training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preparing-everything-for-training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preparing everything for training</span></h3> <p>You’ve seen all of this a few times now, so we’ll go through the code quite quickly. First we’ll build the <code>DataLoader</code>s from our datasets, after setting the datasets to the <code>"torch"</code> format so we get PyTorch tensors:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader tokenized_datasets.set_format(<span class="hljs-string">"torch"</span>) train_dataloader = DataLoader( tokenized_datasets[<span class="hljs-string">"train"</span>], shuffle=<span class="hljs-literal">True</span>, collate_fn=data_collator, batch_size=<span class="hljs-number">8</span>, ) eval_dataloader = DataLoader( tokenized_datasets[<span class="hljs-string">"validation"</span>], collate_fn=data_collator, batch_size=<span class="hljs-number">8</span> )</pre></div> <p>Next we reinstantiate our model, to make sure we’re not continuing the fine-tuning from before but starting from the pretrained model again:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)</pre></div> <p>Then we will need an optimizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AdamW optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">2e-5</span>)</pre></div> <p>Once we have all those objects, we can send them to the <code>accelerator.prepare()</code> method. Remember that if you want to train on TPUs in a Colab notebook, you will need to move all of this code into a training function, and that shouldn’t execute any cell that instantiates an <code>Accelerator</code>.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )</pre></div> <p>Now that we have sent our <code>train_dataloader</code> to <code>accelerator.prepare()</code>, we can use its length to compute the number of training steps. Remember we should always do this after preparing the dataloader, as that method will change the length of the <code>DataLoader</code>. We use a classic linear schedule from the learning rate to 0:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler num_train_epochs = <span class="hljs-number">3</span> num_update_steps_per_epoch = <span class="hljs-built_in">len</span>(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( <span class="hljs-string">"linear"</span>, optimizer=optimizer, num_warmup_steps=<span class="hljs-number">0</span>, num_training_steps=num_training_steps, )</pre></div> <p>Lastly, to push our model to the Hub, we will need to create a <code>Repository</code> object in a working folder. First log in to the Hugging Face Hub, if you’re not logged in already. We’ll determine the repository name from the model ID we want to give our model (feel free to replace the <code>repo_name</code> with your own choice; it just needs to contain your username, which is what the function <code>get_full_repo_name()</code> does):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> Repository, get_full_repo_name model_name = <span class="hljs-string">"marian-finetuned-kde4-en-to-fr-accelerate"</span> repo_name = get_full_repo_name(model_name) repo_name</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'sgugger/marian-finetuned-kde4-en-to-fr-accelerate'</span></pre></div> <p>Then we can clone that repository in a local folder. If it already exists, this local folder should be a clone of the repository we are working with:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>output_dir = <span class="hljs-string">"marian-finetuned-kde4-en-to-fr-accelerate"</span> repo = Repository(output_dir, clone_from=repo_name)</pre></div> <p>We can now upload anything we save in <code>output_dir</code> by calling the <code>repo.push_to_hub()</code> method. This will help us upload the intermediate models at the end of each epoch.</p> <h3 class="relative group"><a id="training-loop" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-loop"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training loop</span></h3> <p>We are now ready to write the full training loop. To simplify its evaluation part, we define this <code>postprocess()</code> function that takes predictions and labels and converts them to the lists of strings our <code>metric</code> object will expect:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">postprocess</span>(<span class="hljs-params">predictions, labels</span>): predictions = predictions.cpu().numpy() labels = labels.cpu().numpy() decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=<span class="hljs-literal">True</span>) <span class="hljs-comment"># Replace -100 in the labels as we can't decode them.</span> labels = np.where(labels != -<span class="hljs-number">100</span>, labels, tokenizer.pad_token_id) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=<span class="hljs-literal">True</span>) <span class="hljs-comment"># Some simple post-processing</span> decoded_preds = [pred.strip() <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> decoded_preds] decoded_labels = [[label.strip()] <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> decoded_labels] <span class="hljs-keyword">return</span> decoded_preds, decoded_labels</pre></div> <p>The training loop looks a lot like the ones in <a href="/course/chapter7/2">section 2</a> and <a href="/course/chapter3">Chapter 3</a>, with a few differences in the evaluation part — so let’s focus on that!</p> <p>The first thing to note is that we use the <code>generate()</code> method to compute predictions, but this is a method on our base model, not the wrapped model 🤗 Accelerate created in the <code>prepare()</code> method. That’s why we unwrap the model first, then call this method.</p> <p>The second thing is that, like with <a href="/course/chapter7/2">token classification</a>, two processes may have padded the inputs and labels to different shapes, so we use <code>accelerator.pad_across_processes()</code> to make the predictions and labels the same shape before calling the <code>gather()</code> method. If we don’t do this, the evaluation will either error out or hang forever.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm <span class="hljs-keyword">import</span> torch progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps)) <span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_train_epochs): <span class="hljs-comment"># Training</span> model.train() <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader: outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(<span class="hljs-number">1</span>) <span class="hljs-comment"># Evaluation</span> model.<span class="hljs-built_in">eval</span>() <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> tqdm(eval_dataloader): <span class="hljs-keyword">with</span> torch.no_grad(): generated_tokens = accelerator.unwrap_model(model).generate( batch[<span class="hljs-string">"input_ids"</span>], attention_mask=batch[<span class="hljs-string">"attention_mask"</span>], max_length=<span class="hljs-number">128</span>, ) labels = batch[<span class="hljs-string">"labels"</span>] <span class="hljs-comment"># Necessary to pad predictions and labels for being gathered</span> generated_tokens = accelerator.pad_across_processes( generated_tokens, dim=<span class="hljs-number">1</span>, pad_index=tokenizer.pad_token_id ) labels = accelerator.pad_across_processes(labels, dim=<span class="hljs-number">1</span>, pad_index=-<span class="hljs-number">100</span>) predictions_gathered = accelerator.gather(generated_tokens) labels_gathered = accelerator.gather(labels) decoded_preds, decoded_labels = postprocess(predictions_gathered, labels_gathered) metric.add_batch(predictions=decoded_preds, references=decoded_labels) results = metric.compute() <span class="hljs-built_in">print</span>(<span class="hljs-string">f"epoch <span class="hljs-subst">{epoch}</span>, BLEU score: <span class="hljs-subst">{results[<span class="hljs-string">'score'</span>]:<span class="hljs-number">.2</span>f}</span>"</span>) <span class="hljs-comment"># Save and upload</span> accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) <span class="hljs-keyword">if</span> accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=<span class="hljs-string">f"Training in progress epoch <span class="hljs-subst">{epoch}</span>"</span>, blocking=<span class="hljs-literal">False</span> )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>epoch <span class="hljs-number">0</span>, BLEU score: <span class="hljs-number">53.47</span> epoch <span class="hljs-number">1</span>, BLEU score: <span class="hljs-number">54.24</span> epoch <span class="hljs-number">2</span>, BLEU score: <span class="hljs-number">54.44</span></pre></div> <p>Once this is done, you should have a model that has results pretty similar to the one trained with the <code>Seq2SeqTrainer</code>. You can check the one we trained using this code at <a href="https://huggingface.co/huggingface-course/marian-finetuned-kde4-en-to-fr-accelerate" rel="nofollow"><em>huggingface-course/marian-finetuned-kde4-en-to-fr-accelerate</em></a>. And if you want to test out any tweaks to the training loop, you can directly implement them by editing the code shown above!</p> <h2 class="relative group"><a id="using-the-fine-tuned-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-the-fine-tuned-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using the fine-tuned model</span></h2> <p>We’ve already shown you how you can use the model we fine-tuned on the Model Hub with the inference widget. To use it locally in a <code>pipeline</code>, we just have to specify the proper model identifier:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline <span class="hljs-comment"># Replace this with your own checkpoint</span> model_checkpoint = <span class="hljs-string">"huggingface-course/marian-finetuned-kde4-en-to-fr"</span> translator = pipeline(<span class="hljs-string">"translation"</span>, model=model_checkpoint) translator(<span class="hljs-string">"Default to expanded threads"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'translation_text'</span>: <span class="hljs-string">'Par défaut, développer les fils de discussion'</span>}]</pre></div> <p>As expected, our pretrained model adapted its knowledge to the corpus we fine-tuned it on, and instead of leaving the English word “threads” alone, it now translates it to the French official version. It’s the same for “plugin”:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>translator( <span class="hljs-string">"Unable to import %1 using the OFX importer plugin. This file is not the correct format."</span> )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'translation_text'</span>: <span class="hljs-string">"Impossible d'importer %1 en utilisant le module externe d'importation OFX. Ce fichier n'est pas le bon format."</span>}]</pre></div> <p>Another great example of domain adaptation!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Your turn!</strong> What does the model return on the sample with the word “email” you identified earlier?</p></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter7/3?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Fine-tuning a masked language model</a> <a href="/learn/nlp-course/chapter7/5?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Summarization<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Translation&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;translation&quot;,&quot;url&quot;:&quot;#translation&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Preparing the data&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;preparing-the-data&quot;,&quot;url&quot;:&quot;#preparing-the-data&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;The KDE4 dataset&quot;,&quot;id&quot;:&quot;the-kde4-dataset&quot;,&quot;url&quot;:&quot;#the-kde4-dataset&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;processing-the-data&quot;,&quot;url&quot;:&quot;#processing-the-data&quot;}]},{&quot;title&quot;:&quot;Fine-tuning the model with the `Trainer` API&quot;,&quot;id&quot;:&quot;fine-tuning-the-model-with-the-trainer-api&quot;,&quot;url&quot;:&quot;#fine-tuning-the-model-with-the-trainer-api&quot;},{&quot;title&quot;:&quot;Fine-tuning the model with Keras&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;fine-tuning-the-model-with-keras&quot;,&quot;url&quot;:&quot;#fine-tuning-the-model-with-keras&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Data collation&quot;,&quot;id&quot;:&quot;data-collation&quot;,&quot;url&quot;:&quot;#data-collation&quot;},{&quot;title&quot;:&quot;Metrics&quot;,&quot;id&quot;:&quot;metrics&quot;,&quot;url&quot;:&quot;#metrics&quot;},{&quot;title&quot;:&quot;Fine-tuning the model&quot;,&quot;id&quot;:&quot;fine-tuning-the-model&quot;,&quot;url&quot;:&quot;#fine-tuning-the-model&quot;}]},{&quot;title&quot;:&quot;A custom training loop&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;a-custom-training-loop&quot;,&quot;url&quot;:&quot;#a-custom-training-loop&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Preparing everything for training&quot;,&quot;id&quot;:&quot;preparing-everything-for-training&quot;,&quot;url&quot;:&quot;#preparing-everything-for-training&quot;},{&quot;title&quot;:&quot;Training loop&quot;,&quot;id&quot;:&quot;training-loop&quot;,&quot;url&quot;:&quot;#training-loop&quot;}]},{&quot;title&quot;:&quot;Using the fine-tuned model&quot;,&quot;id&quot;:&quot;using-the-fine-tuned-model&quot;,&quot;url&quot;:&quot;#using-the-fine-tuned-model&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#translation" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-translation"><wbr>Translation</a> <a href="#preparing-the-data" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preparing-the-data"><wbr>Preparing the data</a> <a href="#the-kde4-dataset" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-kde4-dataset"><wbr>The KD<wbr>E4 dataset</a> <a href="#processing-the-data" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-processing-the-data"><wbr>Processing the data</a> <a href="#fine-tuning-the-model-with-the-trainer-api" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-the-model-with-the-trainer-api"><wbr>Fine-tuning the model with the `<wbr>Trainer` API</a> <a href="#data-collation" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-data-collation"><wbr>Data collation</a> <a href="#metrics" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-metrics"><wbr>Metrics</a> <a href="#fine-tuning-the-model" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-the-model"><wbr>Fine-tuning the model</a> <a href="#a-custom-training-loop" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-a-custom-training-loop"><wbr>A custom training loop</a> <a href="#preparing-everything-for-training" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preparing-everything-for-training"><wbr>Preparing everything for training</a> <a href="#training-loop" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-loop"><wbr>Training loop</a> <a href="#using-the-fine-tuned-model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-the-fine-tuned-model"><wbr>Using the fine-tuned model</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter7/4" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter7/4"); } </script> <iframe name="__privateStripeMetricsController5640" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter7%2F4%3Ffw%3Dpt&amp;title=Translation%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:28.863Z
Training a causal language model from scratch - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter7/6?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#training-a-causal-language-model-from-scratch)Training a causal language model from scratch [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-7-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section6_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section6_pt.ipynb) Up until now, we’ve mostly been using pretrained models and fine-tuning them for new use cases by reusing the weights from pretraining. As we saw in [Chapter 1](/course/chapter1), this is commonly referred to as _transfer learning_, and it’s a very successful strategy for applying Transformer models to most real-world use cases where labeled data is sparse. In this chapter, we’ll take a different approach and train a completely new model from scratch. This is a good approach to take if you have a lot of data and it is very different from the pretraining data used for the available models. However, it also requires considerably more compute resources to pretrain a language model than just to fine-tune an existing one. Examples where it can make sense to train a new model include for datasets consisting of musical notes, molecular sequences such as DNA, or programming languages. The latter have recently gained traction thanks to tools such as TabNine and GitHub’s Copilot, powered by OpenAI’s Codex model, that can generate long sequences of code. This task of text generation is best addressed with auto-regressive or causal language models such as GPT-2. In this section we will build a scaled-down version of a code generation model: we’ll focus on one-line completions instead of full functions or classes, using a subset of Python code. When working with data in Python you are in frequent contact with the Python data science stack, consisting of the `matplotlib`, `seaborn`, `pandas`, and `scikit-learn` libraries. When using those frameworks it’s common to need to look up specific commands, so it would be nice if we could use a model to complete these calls for us. In [Chapter 6](/course/chapter6) we created an efficient tokenizer to process Python source code, but what we still need is a large-scale dataset to pretrain a model on. Here, we’ll apply our tokenizer to a corpus of Python code derived from GitHub repositories. We will then use the `Trainer` API and 🤗 Accelerate to train the model. Let’s get to it! This is actually showcasing the model that was trained and uploaded to the Hub using the code shown in this section. You can find it [here](https://huggingface.co/huggingface-course/codeparrot-ds?text=plt.imshow%28). Note that since there is some randomization happening in the text generation, you will probably get a slightly different result. ## [](#gathering-the-data)Gathering the data Python code is abundantly available from code repositories such as GitHub, which we can use to create a dataset by scraping for every Python repository. This was the approach taken in the [Transformers textbook](https://learning.oreilly.com/library/view/natural-language-processing/9781098136789/) to pretrain a large GPT-2 model. Using a GitHub dump of about 180 GB containing roughly 20 million Python files called `codeparrot`, the authors built a dataset that they then shared on the [Hugging Face Hub](https://huggingface.co/datasets/transformersbook/codeparrot). However, training on the full corpus is time- and compute-consuming, and we only need the subset of the dataset concerned with the Python data science stack. So, let’s start by filtering the `codeparrot` dataset for all files that include any of the libraries in this stack. Because of the dataset’s size, we want to avoid downloading it; instead, we’ll use the streaming feature to filter it on the fly. To help us filter the code samples using the libraries we mentioned earlier, we’ll use the following function: ``` def any_keyword_in_string(string, keywords): for keyword in keywords: if keyword in string: return True return False``` Let’s test it on two examples: ``` filters = ["pandas", "sklearn", "matplotlib", "seaborn"] example_1 = "import numpy as np" example_2 = "import pandas as pd" print( any_keyword_in_string(example_1, filters), any_keyword_in_string(example_2, filters) )``` We can use this to create a function that will stream the dataset and filter the elements we want: ``` from collections import defaultdict from tqdm import tqdm from datasets import Dataset def filter_streaming_dataset(dataset, filters): filtered_dict = defaultdict(list) total = 0 for sample in tqdm(iter(dataset)): total += 1 if any_keyword_in_string(sample["content"], filters): for k, v in sample.items(): filtered_dict[k].append(v) print(f"{len(filtered_dict['content'])/total:.2%} of data after filtering.") return Dataset.from_dict(filtered_dict)``` Then we can simply apply this function to the streaming dataset: ``` from datasets import load_dataset split = "train" filters = ["pandas", "sklearn", "matplotlib", "seaborn"] data = load_dataset(f"transformersbook/codeparrot-{split}", split=split, streaming=True) filtered_data = filter_streaming_dataset(data, filters)``` ``` 3.26% of data after filtering.``` This leaves us with about 3% of the original dataset, which is still quite sizable — the resulting dataset is 6 GB and consists of 600,000 Python scripts! Filtering the full dataset can take 2-3h depending on your machine and bandwidth. If you don’t want to go through this lengthy process yourself, we provide the filtered dataset on the Hub for you to download: ``` from datasets import load_dataset, DatasetDict ds_train = load_dataset("huggingface-course/codeparrot-ds-train", split="train") ds_valid = load_dataset("huggingface-course/codeparrot-ds-valid", split="validation") raw_datasets = DatasetDict( { "train": ds_train, "valid": ds_valid, } ) raw_datasets``` ``` DatasetDict({ train: Dataset({ features: ['repo_name', 'path', 'copies', 'size', 'content', 'license'], num_rows: 606720 }) valid: Dataset({ features: ['repo_name', 'path', 'copies', 'size', 'content', 'license'], num_rows: 3322 }) })``` Pretraining the language model will take a while. We suggest that you first run the training loop on a sample of the data by uncommenting the two partial lines above, and make sure that the training successfully completes and the models are stored. Nothing is more frustrating than a training run failing at the last step because you forgot to create a folder or because there’s a typo at the end of the training loop! Let’s look at an example from the dataset. We’ll just show the first 200 characters of each field: ``` for key in raw_datasets["train"][0]: print(f"{key.upper()}: {raw_datasets['train'][0][key][:200]}")``` ``` 'REPO_NAME: kmike/scikit-learn' 'PATH: sklearn/utils/__init__.py' 'COPIES: 3' 'SIZE: 10094' '''CONTENT: """ The :mod:`sklearn.utils` module includes various utilites. """ from collections import Sequence import numpy as np from scipy.sparse import issparse import warnings from .murmurhash import murm LICENSE: bsd-3-clause'''``` We can see that the `content` field contains the code that we want our model to train on. Now that we have a dataset, we need to prepare the texts so they’re in a format suitable for pretraining. ## [](#preparing-the-dataset)Preparing the dataset The first step will be to tokenize the data, so we can use it for training. Since our goal is to mainly autocomplete short function calls, we can keep the context size relatively small. This has the benefit that we can train the model much faster and it requires significantly less memory. If it is important for your application to have more context (for example, if you want the model to write unit tests based on a file with the function definition), make sure you increase that number, but also keep in mind that this comes with a greater GPU memory footprint. For now, let’s fix the context size at 128 tokens, as opposed to the 1,024 or 2,048 used in GPT-2 or GPT-3, respectively. Most documents contain many more than 128 tokens, so simply truncating the inputs to the maximum length would eliminate a large fraction of our dataset. Instead, we’ll use the `return_overflowing_tokens` option to tokenize the whole input and split it into several chunks, as we did in [Chapter 6](/course/chapter6/4). We’ll also use the `return_length` option to return the length of each created chunk automatically. Often the last chunk will be smaller than the context size, and we’ll get rid of these pieces to avoid padding issues; we don’t really need them as we have plenty of data anyway. ![Chunking a large texts in several pieces.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/chunking_texts.svg) ![Chunking a large texts in several pieces.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/chunking_texts-dark.svg) Let’s see exactly how this works by looking at the first two examples: ``` from transformers import AutoTokenizer context_length = 128 tokenizer = AutoTokenizer.from_pretrained("huggingface-course/code-search-net-tokenizer") outputs = tokenizer( raw_datasets["train"][:2]["content"], truncation=True, max_length=context_length, return_overflowing_tokens=True, return_length=True, ) print(f"Input IDs length: {len(outputs['input_ids'])}") print(f"Input chunk lengths: {(outputs['length'])}") print(f"Chunk mapping: {outputs['overflow_to_sample_mapping']}")``` ``` Input IDs length: 34 Input chunk lengths: [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 117, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 41] Chunk mapping: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]``` We can see that we get 34 segments in total from those two examples. Looking at the chunk lengths, we can see that the chunks at the ends of both documents have less than 128 tokens (117 and 41, respectively). These represent just a small fraction of the total chunks that we have, so we can safely throw them away. With the `overflow_to_sample_mapping` field, we can also reconstruct which chunks belonged to which input samples. With this operation we’re using a handy feature of the `Dataset.map()` function in 🤗 Datasets, which is that it does not require one-to-one maps; as we saw in [section 3](/course/chapter7/3), we can create batches with more or fewer elements than the input batch. This is useful when doing operations like data augmentation or data filtering that change the number of elements. In our case, when tokenizing each element into chunks of the specified context size, we create many samples from each document. We just need to make sure to delete the existing columns, since they have a conflicting size. If we wanted to keep them, we could repeat them appropriately and return them within the `Dataset.map()` call: ``` def tokenize(element): outputs = tokenizer( element["content"], truncation=True, max_length=context_length, return_overflowing_tokens=True, return_length=True, ) input_batch = [] for length, input_ids in zip(outputs["length"], outputs["input_ids"]): if length == context_length: input_batch.append(input_ids) return {"input_ids": input_batch} tokenized_datasets = raw_datasets.map( tokenize, batched=True, remove_columns=raw_datasets["train"].column_names ) tokenized_datasets``` ``` DatasetDict({ train: Dataset({ features: ['input_ids'], num_rows: 16702061 }) valid: Dataset({ features: ['input_ids'], num_rows: 93164 }) })``` We now have 16.7 million examples with 128 tokens each, which corresponds to about 2.1 billion tokens in total. For reference, OpenAI’s GPT-3 and Codex models are trained on 300 and 100 billion tokens, respectively, where the Codex models are initialized from the GPT-3 checkpoints. Our goal in this section is not to compete with these models, which can generate long, coherent texts, but to create a scaled-down version providing a quick autocomplete function for data scientists. Now that we have the dataset ready, let’s set up the model! ✏️ **Try it out!** Getting rid of all the chunks that are smaller than the context size wasn’t a big issue here because we’re using small context windows. As you increase the context size (or if you have a corpus of short documents), the fraction of chunks that are thrown away will also grow. A more efficient way to prepare the data is to join all the tokenized samples in a batch with an `eos_token_id` token in between, and then perform the chunking on the concatenated sequences. As an exercise, modify the `tokenize()` function to make use of that approach. Note that you’ll want to set `truncation=False` and remove the other arguments from the tokenizer to get the full sequence of token IDs. ## [](#initializing-a-new-model)Initializing a new model Our first step is to freshly initialize a GPT-2 model. We’ll use the same configuration for our model as for the small GPT-2 model, so we load the pretrained configuration, make sure that the tokenizer size matches the model vocabulary size and pass the `bos` and `eos` (beginning and end of sequence) token IDs: ``` from transformers import AutoTokenizer, GPT2LMHeadModel, AutoConfig config = AutoConfig.from_pretrained( "gpt2", vocab_size=len(tokenizer), n_ctx=context_length, bos_token_id=tokenizer.bos_token_id, eos_token_id=tokenizer.eos_token_id, )``` With that configuration, we can load a new model. Note that this is the first time we don’t use the `from_pretrained()` function, since we’re actually initializing a model ourself: ``` model = GPT2LMHeadModel(config) model_size = sum(t.numel() for t in model.parameters()) print(f"GPT-2 size: {model_size/1000**2:.1f}M parameters")``` ``` GPT-2 size: 124.2M parameters``` Our model has 124M parameters that we’ll have to tune. Before we can start training, we need to set up a data collator that will take care of creating the batches. We can use the `DataCollatorForLanguageModeling` collator, which is designed specifically for language modeling (as the name subtly suggests). Besides stacking and padding batches, it also takes care of creating the language model labels — in causal language modeling the inputs serve as labels too (just shifted by one element), and this data collator creates them on the fly during training so we don’t need to duplicate the `input_ids`. Note that `DataCollatorForLanguageModeling` supports both masked language modeling (MLM) and causal language modeling (CLM). By default it prepares data for MLM, but we can switch to CLM by setting the argument `mlm=False`: ``` from transformers import DataCollatorForLanguageModeling tokenizer.pad_token = tokenizer.eos_token data_collator = DataCollatorForLanguageModeling(tokenizer, mlm=False)``` Let’s have a look at an example: ``` out = data_collator([tokenized_datasets["train"][i] for i in range(5)]) for key in out: print(f"{key} shape: {out[key].shape}")``` ``` input_ids shape: torch.Size([5, 128]) attention_mask shape: torch.Size([5, 128]) labels shape: torch.Size([5, 128])``` We can see that the examples have been stacked and all the tensors have the same shape. ⚠️ Shifting the inputs and labels to align them happens inside the model, so the data collator just copies the inputs to create the labels. Now we have everything in place to actually train our model — that wasn’t so much work after all! Before we start training we should log in to Hugging Face. If you’re working in a notebook, you can do so with the following utility function: ``` from huggingface_hub import notebook_login notebook_login()``` This will display a widget where you can enter your Hugging Face login credentials. If you aren’t working in a notebook, just type the following line in your terminal: All that’s left to do is configure the training arguments and fire up the `Trainer`. We’ll use a cosine learning rate schedule with some warmup and an effective batch size of 256 (`per_device_train_batch_size` \* `gradient_accumulation_steps`). Gradient accumulation is used when a single batch does not fit into memory, and incrementally builds up the gradient through several forward/backward passes. We’ll see this in action when we create the training loop with 🤗 Accelerate. ``` from transformers import Trainer, TrainingArguments args = TrainingArguments( output_dir="codeparrot-ds", per_device_train_batch_size=32, per_device_eval_batch_size=32, evaluation_strategy="steps", eval_steps=5_000, logging_steps=5_000, gradient_accumulation_steps=8, num_train_epochs=1, weight_decay=0.1, warmup_steps=1_000, lr_scheduler_type="cosine", learning_rate=5e-4, save_steps=5_000, fp16=True, push_to_hub=True, ) trainer = Trainer( model=model, tokenizer=tokenizer, args=args, data_collator=data_collator, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["valid"], )``` Now we can just start the `Trainer` and wait for training to finish. Depending on whether you run it on the full or a subset of the training set this will take 20 or 2 hours, respectively, so grab a few coffees and a good book to read! After training completes, we can push the model and tokenizer to the Hub: ✏️ **Try it out!** It only took us about 30 lines of code in addition to the `TrainingArguments` to get from raw texts to training GPT-2. Try it out with your own dataset and see if you can get good results! 💡 If you have access to a machine with multiple GPUs, try to run the code there. The `Trainer` automatically manages multiple machines, and this can speed up training tremendously. ## [](#code-generation-with-a-pipeline)Code generation with a pipeline Now is the moment of truth: let’s see how well the trained model actually works! We can see in the logs that the loss went down steadily, but to put the model to the test let’s take a look at how well it works on some prompts. To do that we’ll wrap the model in a text generation `pipeline`, and we’ll put it on the GPU for fast generations if there is one available: ``` import torch from transformers import pipeline device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") pipe = pipeline( "text-generation", model="huggingface-course/codeparrot-ds", device=device )``` Let’s start with the simple task of creating a scatter plot: ``` txt = """\ # create some data x = np.random.randn(100) y = np.random.randn(100) # create scatter plot with x, y """ print(pipe(txt, num_return_sequences=1)[0]["generated_text"])``` ``` x = np.random.randn(100) y = np.random.randn(100) plt.scatter(x, y) ``` The result looks correct. Does it also work for a `pandas` operation? Let’s see if we can create a `DataFrame` from two arrays: ``` txt = """\ # create some data x = np.random.randn(100) y = np.random.randn(100) # create dataframe from x and y """ print(pipe(txt, num_return_sequences=1)[0]["generated_text"])``` ``` x = np.random.randn(100) y = np.random.randn(100) df = pd.DataFrame({'x': x, 'y': y}) df.insert(0,'x', x) for``` Nice, that’s the correct answer — although it then inserts the column `x` again. Since the number of generated tokens is limited, the following `for` loop is cut off. Let’s see if we can do something a bit more complex and have the model help us use the `groupby` operation: ``` txt = """\ # dataframe with profession, income and name df = pd.DataFrame({'profession': x, 'income':y, 'name': z}) # calculate the mean income per profession """ print(pipe(txt, num_return_sequences=1)[0]["generated_text"])``` ``` df = pd.DataFrame({'profession': x, 'income':y, 'name': z}) profession = df.groupby(['profession']).mean() ``` Not bad; that’s the right way to do it. Finally, let’s see if we can also use it for `scikit-learn` and set up a Random Forest model: ``` txt = """ # import random forest regressor from scikit-learn from sklearn.ensemble import RandomForestRegressor # fit random forest model with 300 estimators on X, y: """ print(pipe(txt, num_return_sequences=1)[0]["generated_text"])``` ``` from sklearn.ensemble import RandomForestRegressor rf = RandomForestRegressor(n_estimators=300, random_state=random_state, max_depth=3) rf.fit(X, y) rf``` Looking at these few examples, it seems that the model has learned some of the syntax of the Python data science stack (of course, we would need to evaluate it more thoroughly before deploying the model in the real world). Sometimes it requires more customization of the model training to achieve the necessary performance for a given use case, however. For example, what if we would like to dynamically update the batch size or have a conditional training loop that skips bad examples on the fly? One option would be to subclass the `Trainer` and add the necessary changes, but sometimes it’s simpler to write the training loop from scratch. That’s where 🤗 Accelerate comes in. ## [](#training-with-accelerate)Training with 🤗 Accelerate We’ve seen how to train a model with the `Trainer`, which can allow for some customization. However, sometimes we want full control over the training loop, or we want to make some exotic changes. In this case 🤗 Accelerate is a great choice, and in this section we’ll go through the steps to use it to train our model. To make things more interesting, we’ll also add a twist to the training loop. Since we are mainly interested in sensible autocompletion for the the data science libraries, it makes sense to give more weight to training samples that make more use of these libraries. We can easily identify these examples through the use of keywords such as `plt`, `pd`, `sk`, `fit`, and `predict`, which are the most frequent import names for `matplotlib.pyplot`, `pandas`, and `sklearn` as well as the fit/predict pattern of the latter. If these are each represented as a single token, we can easily check if they occur in the input sequence. Tokens can have a whitespace prefix, so we’ll also check for those versions in the tokenizer vocabulary. To verify that it works, we’ll add one test token which should be split into multiple tokens: ``` keytoken_ids = [] for keyword in [ "plt", "pd", "sk", "fit", "predict", " plt", " pd", " sk", " fit", " predict", "testtest", ]: ids = tokenizer([keyword]).input_ids[0] if len(ids) == 1: keytoken_ids.append(ids[0]) else: print(f"Keyword has not single token: {keyword}")``` ``` 'Keyword has not single token: testtest'``` Great, that seems to work nicely! We can now write a custom loss function that takes the input sequence, the logits, and the key tokens we just selected as inputs. First we need to align the logits and inputs: the input sequence shifted by one to the right forms the labels, since the next token is the label for the current token. We can achieve this by starting the labels from the second token of the input sequence, since the model does not make a prediction for the first token anyway. Then we cut off the last logit, as we don’t have a label for the token that follows the full input sequence. With that we can compute the loss per sample and count the occurrences of all keywords in each sample. Finally, we calculate the weighted average over all samples using the occurrences as weights. Since we don’t want to throw away all the samples that have no keywords, we add 1 to the weights: ``` from torch.nn import CrossEntropyLoss import torch def keytoken_weighted_loss(inputs, logits, keytoken_ids, alpha=1.0): shift_labels = inputs[..., 1:].contiguous() shift_logits = logits[..., :-1, :].contiguous() loss_fct = CrossEntropyLoss(reduce=False) loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1)) loss_per_sample = loss.view(shift_logits.size(0), shift_logits.size(1)).mean(axis=1) weights = torch.stack([(inputs == kt).float() for kt in keytoken_ids]).sum( axis=[0, 2] ) weights = alpha * (1.0 + weights) weighted_loss = (loss_per_sample * weights).mean() return weighted_loss``` Before we can start training with this awesome new loss function, we need to prepare a few things: - We need dataloaders to load the data in batches. - We need to set up weight decay parameters. - From time to time we want to evaluate, so it makes sense to wrap the evaluation code in a function. Let’s start with the dataloaders. We only need to set the dataset’s format to `"torch"`, and then we can pass it to a PyTorch `DataLoader` with the appropriate batch size: ``` from torch.utils.data.dataloader import DataLoader tokenized_dataset.set_format("torch") train_dataloader = DataLoader(tokenized_dataset["train"], batch_size=32, shuffle=True) eval_dataloader = DataLoader(tokenized_dataset["valid"], batch_size=32)``` Next, we group the parameters so that the optimizer knows which ones will get an additional weight decay. Usually, all bias and LayerNorm weights terms are exempt from this; here’s how we can do this: ``` weight_decay = 0.1 def get_grouped_params(model, no_decay=["bias", "LayerNorm.weight"]): params_with_wd, params_without_wd = [], [] for n, p in model.named_parameters(): if any(nd in n for nd in no_decay): params_without_wd.append(p) else: params_with_wd.append(p) return [ {"params": params_with_wd, "weight_decay": weight_decay}, {"params": params_without_wd, "weight_decay": 0.0}, ]``` Since we want to evaluate the model regularly on the validation set during training, let’s write a function for that as well. It just runs through the evaluation dataloader and gathers all the losses across processes: ``` def evaluate(): model.eval() losses = [] for step, batch in enumerate(eval_dataloader): with torch.no_grad(): outputs = model(batch["input_ids"], labels=batch["input_ids"]) losses.append(accelerator.gather(outputs.loss)) loss = torch.mean(torch.cat(losses)) try: perplexity = torch.exp(loss) except OverflowError: perplexity = float("inf") return loss.item(), perplexity.item()``` With the `evaluate()` function we can report loss and [perplexity](/course/chapter7/3) at regular intervals. Next, we redefine our model to make sure we train from scratch again: ``` model = GPT2LMHeadModel(config)``` We can then define our optimizer, using the function from before to split the parameters for weight decay: ``` from torch.optim import AdamW optimizer = AdamW(get_grouped_params(model), lr=5e-4)``` Now let’s prepare the model, optimizer, and dataloaders so we can start training: ``` from accelerate import Accelerator accelerator = Accelerator(fp16=True) model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )``` 🚨 If you’re training on a TPU, you’ll need to move all the code starting at the cell above into a dedicated training function. See [Chapter 3](/course/chapter3) for more details. Now that we have sent our `train_dataloader` to `accelerator.prepare()`, we can use its length to compute the number of training steps. Remember that we should always do this after preparing the dataloader, as that method will change its length. We use a classic linear schedule from the learning rate to 0: ``` from transformers import get_scheduler num_train_epochs = 1 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( name="linear", optimizer=optimizer, num_warmup_steps=1_000, num_training_steps=num_training_steps, )``` Lastly, to push our model to the Hub, we will need to create a `Repository` object in a working folder. First log in to the Hugging Face Hub, if you aren’t logged in already. We’ll determine the repository name from the model ID we want to give our model (feel free to replace the `repo_name` with your own choice; it just needs to contain your username, which is what the function `get_full_repo_name()` does): ``` from huggingface_hub import Repository, get_full_repo_name model_name = "codeparrot-ds-accelerate" repo_name = get_full_repo_name(model_name) repo_name``` ``` 'sgugger/codeparrot-ds-accelerate'``` Then we can clone that repository in a local folder. If it already exists, this local folder should be an existing clone of the repository we are working with: ``` output_dir = "codeparrot-ds-accelerate" repo = Repository(output_dir, clone_from=repo_name)``` We can now upload anything we save in `output_dir` by calling the `repo.push_to_hub()` method. This will help us upload the intermediate models at the end of each epoch. Before we train, let’s run a quick test to see if the evaluation function works properly: ``` (10.934126853942871, 56057.14453125)``` Those are very high values for loss and perplexity, but that’s not surprising as we haven’t trained the model yet. With that, we have everything prepared to write the core part of the training script: the training loop. In the training loop we iterate over the dataloader and pass the batches to the model. With the logits, we can then evaluate our custom loss function. We scale the loss by the number of gradient accumulation steps so as not to create larger losses when aggregating more steps. Before we optimize, we also clip the gradients for better convergence. Finally, every few steps we evaluate the model on the evaluation set with our new `evaluate()` function: ``` from tqdm.notebook import tqdm gradient_accumulation_steps = 8 eval_steps = 5_000 model.train() completed_steps = 0 for epoch in range(num_train_epochs): for step, batch in tqdm( enumerate(train_dataloader, start=1), total=num_training_steps ): logits = model(batch["input_ids"]).logits loss = keytoken_weighted_loss(batch["input_ids"], logits, keytoken_ids) if step % 100 == 0: accelerator.print( { "lr": get_lr(), "samples": step * samples_per_step, "steps": completed_steps, "loss/train": loss.item() * gradient_accumulation_steps, } ) loss = loss / gradient_accumulation_steps accelerator.backward(loss) if step % gradient_accumulation_steps == 0: accelerator.clip_grad_norm_(model.parameters(), 1.0) optimizer.step() lr_scheduler.step() optimizer.zero_grad() completed_steps += 1 if (step % (eval_steps * gradient_accumulation_steps)) == 0: eval_loss, perplexity = evaluate() accelerator.print({"loss/eval": eval_loss, "perplexity": perplexity}) model.train() accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=f"Training in progress step {step}", blocking=False )``` And that’s it — you now have your own custom training loop for causal language models such as GPT-2 that you can further customize to your needs. ✏️ **Try it out!** Either create your own custom loss function tailored to your use case, or add another custom step into the training loop. ✏️ **Try it out!** When running long training experiments it’s a good idea to log important metrics using tools such as TensorBoard or Weights & Biases. Add proper logging to the training loop so you can always check how the training is going.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Training a causal language model from scratch - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter7/6"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Training a causal language model from scratch - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;training-a-causal-language-model-from-scratch&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;gathering-the-data&quot;,&quot;title&quot;:&quot;Gathering the data&quot;},{&quot;local&quot;:&quot;preparing-the-dataset&quot;,&quot;title&quot;:&quot;Preparing the dataset&quot;},{&quot;local&quot;:&quot;initializing-a-new-model&quot;,&quot;title&quot;:&quot;Initializing a new model&quot;},{&quot;local&quot;:&quot;code-generation-with-a-pipeline&quot;,&quot;title&quot;:&quot;Code generation with a pipeline&quot;},{&quot;local&quot;:&quot;training-with-accelerate&quot;,&quot;title&quot;:&quot;Training with 🤗 Accelerate&quot;}],&quot;title&quot;:&quot;Training a causal language model from scratch&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter7/6&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Training a causal language model from scratch&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Training a causal language model from scratch</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/2?fw=pt">Token classification </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/3?fw=pt">Fine-tuning a masked language model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/4?fw=pt">Translation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/5?fw=pt">Summarization </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter7/6?fw=pt">Training a causal language model from scratch </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/7?fw=pt">Question answering </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/8?fw=pt">Mastering NLP </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="training-a-causal-language-model-from-scratch" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-a-causal-language-model-from-scratch"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training a causal language model from scratch</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-7-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section6_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section6_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>Up until now, we’ve mostly been using pretrained models and fine-tuning them for new use cases by reusing the weights from pretraining. As we saw in <a href="/course/chapter1">Chapter 1</a>, this is commonly referred to as <em>transfer learning</em>, and it’s a very successful strategy for applying Transformer models to most real-world use cases where labeled data is sparse. In this chapter, we’ll take a different approach and train a completely new model from scratch. This is a good approach to take if you have a lot of data and it is very different from the pretraining data used for the available models. However, it also requires considerably more compute resources to pretrain a language model than just to fine-tune an existing one. Examples where it can make sense to train a new model include for datasets consisting of musical notes, molecular sequences such as DNA, or programming languages. The latter have recently gained traction thanks to tools such as TabNine and GitHub’s Copilot, powered by OpenAI’s Codex model, that can generate long sequences of code. This task of text generation is best addressed with auto-regressive or causal language models such as GPT-2.</p> <p>In this section we will build a scaled-down version of a code generation model: we’ll focus on one-line completions instead of full functions or classes, using a subset of Python code. When working with data in Python you are in frequent contact with the Python data science stack, consisting of the <code>matplotlib</code>, <code>seaborn</code>, <code>pandas</code>, and <code>scikit-learn</code> libraries. When using those frameworks it’s common to need to look up specific commands, so it would be nice if we could use a model to complete these calls for us.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/Vpjb1lu0MDk" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>In <a href="/course/chapter6">Chapter 6</a> we created an efficient tokenizer to process Python source code, but what we still need is a large-scale dataset to pretrain a model on. Here, we’ll apply our tokenizer to a corpus of Python code derived from GitHub repositories. We will then use the <code>Trainer</code> API and 🤗 Accelerate to train the model. Let’s get to it!</p> <iframe src="https://course-demos-codeparrot-ds.hf.space" frameborder="0" height="300" title="Gradio app" class="block dark:hidden container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>This is actually showcasing the model that was trained and uploaded to the Hub using the code shown in this section. You can find it <a href="https://huggingface.co/huggingface-course/codeparrot-ds?text=plt.imshow%28" rel="nofollow">here</a>. Note that since there is some randomization happening in the text generation, you will probably get a slightly different result.</p> <h2 class="relative group"><a id="gathering-the-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#gathering-the-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Gathering the data</span></h2> <p>Python code is abundantly available from code repositories such as GitHub, which we can use to create a dataset by scraping for every Python repository. This was the approach taken in the <a href="https://learning.oreilly.com/library/view/natural-language-processing/9781098136789/" rel="nofollow">Transformers textbook</a> to pretrain a large GPT-2 model. Using a GitHub dump of about 180 GB containing roughly 20 million Python files called <code>codeparrot</code>, the authors built a dataset that they then shared on the <a href="https://huggingface.co/datasets/transformersbook/codeparrot" rel="nofollow">Hugging Face Hub</a>.</p> <p>However, training on the full corpus is time- and compute-consuming, and we only need the subset of the dataset concerned with the Python data science stack. So, let’s start by filtering the <code>codeparrot</code> dataset for all files that include any of the libraries in this stack. Because of the dataset’s size, we want to avoid downloading it; instead, we’ll use the streaming feature to filter it on the fly. To help us filter the code samples using the libraries we mentioned earlier, we’ll use the following function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">any_keyword_in_string</span>(<span class="hljs-params">string, keywords</span>): <span class="hljs-keyword">for</span> keyword <span class="hljs-keyword">in</span> keywords: <span class="hljs-keyword">if</span> keyword <span class="hljs-keyword">in</span> string: <span class="hljs-keyword">return</span> <span class="hljs-literal">True</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">False</span></pre></div> <p>Let’s test it on two examples:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>filters = [<span class="hljs-string">"pandas"</span>, <span class="hljs-string">"sklearn"</span>, <span class="hljs-string">"matplotlib"</span>, <span class="hljs-string">"seaborn"</span>] example_1 = <span class="hljs-string">"import numpy as np"</span> example_2 = <span class="hljs-string">"import pandas as pd"</span> <span class="hljs-built_in">print</span>( any_keyword_in_string(example_1, filters), any_keyword_in_string(example_2, filters) )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-literal">False</span> <span class="hljs-literal">True</span></pre></div> <p>We can use this to create a function that will stream the dataset and filter the elements we want:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> defaultdict <span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm <span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset <span class="hljs-keyword">def</span> <span class="hljs-title function_">filter_streaming_dataset</span>(<span class="hljs-params">dataset, filters</span>): filtered_dict = defaultdict(<span class="hljs-built_in">list</span>) total = <span class="hljs-number">0</span> <span class="hljs-keyword">for</span> sample <span class="hljs-keyword">in</span> tqdm(<span class="hljs-built_in">iter</span>(dataset)): total += <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> any_keyword_in_string(sample[<span class="hljs-string">"content"</span>], filters): <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> sample.items(): filtered_dict[k].append(v) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"<span class="hljs-subst">{<span class="hljs-built_in">len</span>(filtered_dict[<span class="hljs-string">'content'</span>])/total:<span class="hljs-number">.2</span>%}</span> of data after filtering."</span>) <span class="hljs-keyword">return</span> Dataset.from_dict(filtered_dict)</pre></div> <p>Then we can simply apply this function to the streaming dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># This cell will take a very long time to execute, so you should skip it and go to</span> <span class="hljs-comment"># the next one!</span> <span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset split = <span class="hljs-string">"train"</span> <span class="hljs-comment"># "valid"</span> filters = [<span class="hljs-string">"pandas"</span>, <span class="hljs-string">"sklearn"</span>, <span class="hljs-string">"matplotlib"</span>, <span class="hljs-string">"seaborn"</span>] data = load_dataset(<span class="hljs-string">f"transformersbook/codeparrot-<span class="hljs-subst">{split}</span>"</span>, split=split, streaming=<span class="hljs-literal">True</span>) filtered_data = filter_streaming_dataset(data, filters)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">3.26</span>% of data after filtering.</pre></div> <p>This leaves us with about 3% of the original dataset, which is still quite sizable — the resulting dataset is 6 GB and consists of 600,000 Python scripts!</p> <p>Filtering the full dataset can take 2-3h depending on your machine and bandwidth. If you don’t want to go through this lengthy process yourself, we provide the filtered dataset on the Hub for you to download:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset, DatasetDict ds_train = load_dataset(<span class="hljs-string">"huggingface-course/codeparrot-ds-train"</span>, split=<span class="hljs-string">"train"</span>) ds_valid = load_dataset(<span class="hljs-string">"huggingface-course/codeparrot-ds-valid"</span>, split=<span class="hljs-string">"validation"</span>) raw_datasets = DatasetDict( { <span class="hljs-string">"train"</span>: ds_train, <span class="hljs-comment"># .shuffle().select(range(50000)),</span> <span class="hljs-string">"valid"</span>: ds_valid, <span class="hljs-comment"># .shuffle().select(range(500))</span> } ) raw_datasets</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'repo_name'</span>, <span class="hljs-string">'path'</span>, <span class="hljs-string">'copies'</span>, <span class="hljs-string">'size'</span>, <span class="hljs-string">'content'</span>, <span class="hljs-string">'license'</span>], num_rows: <span class="hljs-number">606720</span> }) valid: Dataset({ features: [<span class="hljs-string">'repo_name'</span>, <span class="hljs-string">'path'</span>, <span class="hljs-string">'copies'</span>, <span class="hljs-string">'size'</span>, <span class="hljs-string">'content'</span>, <span class="hljs-string">'license'</span>], num_rows: <span class="hljs-number">3322</span> }) })</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>Pretraining the language model will take a while. We suggest that you first run the training loop on a sample of the data by uncommenting the two partial lines above, and make sure that the training successfully completes and the models are stored. Nothing is more frustrating than a training run failing at the last step because you forgot to create a folder or because there’s a typo at the end of the training loop!</p></div> <p>Let’s look at an example from the dataset. We’ll just show the first 200 characters of each field:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>]: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"<span class="hljs-subst">{key.upper()}</span>: <span class="hljs-subst">{raw_datasets[<span class="hljs-string">'train'</span>][<span class="hljs-number">0</span>][key][:<span class="hljs-number">200</span>]}</span>"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'REPO_NAME: kmike/scikit-learn'</span> <span class="hljs-string">'PATH: sklearn/utils/__init__.py'</span> <span class="hljs-string">'COPIES: 3'</span> <span class="hljs-string">'SIZE: 10094'</span> <span class="hljs-string">'''CONTENT: """ The :mod:`sklearn.utils` module includes various utilites. """ from collections import Sequence import numpy as np from scipy.sparse import issparse import warnings from .murmurhash import murm LICENSE: bsd-3-clause'''</span></pre></div> <p>We can see that the <code>content</code> field contains the code that we want our model to train on. Now that we have a dataset, we need to prepare the texts so they’re in a format suitable for pretraining.</p> <h2 class="relative group"><a id="preparing-the-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preparing-the-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preparing the dataset</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/ma1TrR7gE7I" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The first step will be to tokenize the data, so we can use it for training. Since our goal is to mainly autocomplete short function calls, we can keep the context size relatively small. This has the benefit that we can train the model much faster and it requires significantly less memory. If it is important for your application to have more context (for example, if you want the model to write unit tests based on a file with the function definition), make sure you increase that number, but also keep in mind that this comes with a greater GPU memory footprint. For now, let’s fix the context size at 128 tokens, as opposed to the 1,024 or 2,048 used in GPT-2 or GPT-3, respectively.</p> <p>Most documents contain many more than 128 tokens, so simply truncating the inputs to the maximum length would eliminate a large fraction of our dataset. Instead, we’ll use the <code>return_overflowing_tokens</code> option to tokenize the whole input and split it into several chunks, as we did in <a href="/course/chapter6/4">Chapter 6</a>. We’ll also use the <code>return_length</code> option to return the length of each created chunk automatically. Often the last chunk will be smaller than the context size, and we’ll get rid of these pieces to avoid padding issues; we don’t really need them as we have plenty of data anyway.</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/chunking_texts.svg" alt="Chunking a large texts in several pieces."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/chunking_texts-dark.svg" alt="Chunking a large texts in several pieces."></div> <p>Let’s see exactly how this works by looking at the first two examples:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer context_length = <span class="hljs-number">128</span> tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"huggingface-course/code-search-net-tokenizer"</span>) outputs = tokenizer( raw_datasets[<span class="hljs-string">"train"</span>][:<span class="hljs-number">2</span>][<span class="hljs-string">"content"</span>], truncation=<span class="hljs-literal">True</span>, max_length=context_length, return_overflowing_tokens=<span class="hljs-literal">True</span>, return_length=<span class="hljs-literal">True</span>, ) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Input IDs length: <span class="hljs-subst">{<span class="hljs-built_in">len</span>(outputs[<span class="hljs-string">'input_ids'</span>])}</span>"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Input chunk lengths: <span class="hljs-subst">{(outputs[<span class="hljs-string">'length'</span>])}</span>"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Chunk mapping: <span class="hljs-subst">{outputs[<span class="hljs-string">'overflow_to_sample_mapping'</span>]}</span>"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Input IDs length: <span class="hljs-number">34</span> Input chunk lengths: [<span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">117</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">128</span>, <span class="hljs-number">41</span>] Chunk mapping: [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]</pre></div> <p>We can see that we get 34 segments in total from those two examples. Looking at the chunk lengths, we can see that the chunks at the ends of both documents have less than 128 tokens (117 and 41, respectively). These represent just a small fraction of the total chunks that we have, so we can safely throw them away. With the <code>overflow_to_sample_mapping</code> field, we can also reconstruct which chunks belonged to which input samples.</p> <p>With this operation we’re using a handy feature of the <code>Dataset.map()</code> function in 🤗 Datasets, which is that it does not require one-to-one maps; as we saw in <a href="/course/chapter7/3">section 3</a>, we can create batches with more or fewer elements than the input batch. This is useful when doing operations like data augmentation or data filtering that change the number of elements. In our case, when tokenizing each element into chunks of the specified context size, we create many samples from each document. We just need to make sure to delete the existing columns, since they have a conflicting size. If we wanted to keep them, we could repeat them appropriately and return them within the <code>Dataset.map()</code> call:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize</span>(<span class="hljs-params">element</span>): outputs = tokenizer( element[<span class="hljs-string">"content"</span>], truncation=<span class="hljs-literal">True</span>, max_length=context_length, return_overflowing_tokens=<span class="hljs-literal">True</span>, return_length=<span class="hljs-literal">True</span>, ) input_batch = [] <span class="hljs-keyword">for</span> length, input_ids <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(outputs[<span class="hljs-string">"length"</span>], outputs[<span class="hljs-string">"input_ids"</span>]): <span class="hljs-keyword">if</span> length == context_length: input_batch.append(input_ids) <span class="hljs-keyword">return</span> {<span class="hljs-string">"input_ids"</span>: input_batch} tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>( tokenize, batched=<span class="hljs-literal">True</span>, remove_columns=raw_datasets[<span class="hljs-string">"train"</span>].column_names ) tokenized_datasets</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'input_ids'</span>], num_rows: <span class="hljs-number">16702061</span> }) valid: Dataset({ features: [<span class="hljs-string">'input_ids'</span>], num_rows: <span class="hljs-number">93164</span> }) })</pre></div> <p>We now have 16.7 million examples with 128 tokens each, which corresponds to about 2.1 billion tokens in total. For reference, OpenAI’s GPT-3 and Codex models are trained on 300 and 100 billion tokens, respectively, where the Codex models are initialized from the GPT-3 checkpoints. Our goal in this section is not to compete with these models, which can generate long, coherent texts, but to create a scaled-down version providing a quick autocomplete function for data scientists.</p> <p>Now that we have the dataset ready, let’s set up the model!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Getting rid of all the chunks that are smaller than the context size wasn’t a big issue here because we’re using small context windows. As you increase the context size (or if you have a corpus of short documents), the fraction of chunks that are thrown away will also grow. A more efficient way to prepare the data is to join all the tokenized samples in a batch with an <code>eos_token_id</code> token in between, and then perform the chunking on the concatenated sequences. As an exercise, modify the <code>tokenize()</code> function to make use of that approach. Note that you’ll want to set <code>truncation=False</code> and remove the other arguments from the tokenizer to get the full sequence of token IDs.</p></div> <h2 class="relative group"><a id="initializing-a-new-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#initializing-a-new-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Initializing a new model</span></h2> <p>Our first step is to freshly initialize a GPT-2 model. We’ll use the same configuration for our model as for the small GPT-2 model, so we load the pretrained configuration, make sure that the tokenizer size matches the model vocabulary size and pass the <code>bos</code> and <code>eos</code> (beginning and end of sequence) token IDs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, GPT2LMHeadModel, AutoConfig config = AutoConfig.from_pretrained( <span class="hljs-string">"gpt2"</span>, vocab_size=<span class="hljs-built_in">len</span>(tokenizer), n_ctx=context_length, bos_token_id=tokenizer.bos_token_id, eos_token_id=tokenizer.eos_token_id, )</pre></div> <p>With that configuration, we can load a new model. Note that this is the first time we don’t use the <code>from_pretrained()</code> function, since we’re actually initializing a model ourself:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model = GPT2LMHeadModel(config) model_size = <span class="hljs-built_in">sum</span>(t.numel() <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> model.parameters()) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"GPT-2 size: <span class="hljs-subst">{model_size/<span class="hljs-number">1000</span>**<span class="hljs-number">2</span>:<span class="hljs-number">.1</span>f}</span>M parameters"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>GPT-<span class="hljs-number">2</span> size: <span class="hljs-number">124.2</span>M parameters</pre></div> <p>Our model has 124M parameters that we’ll have to tune. Before we can start training, we need to set up a data collator that will take care of creating the batches. We can use the <code>DataCollatorForLanguageModeling</code> collator, which is designed specifically for language modeling (as the name subtly suggests). Besides stacking and padding batches, it also takes care of creating the language model labels — in causal language modeling the inputs serve as labels too (just shifted by one element), and this data collator creates them on the fly during training so we don’t need to duplicate the <code>input_ids</code>.</p> <p>Note that <code>DataCollatorForLanguageModeling</code> supports both masked language modeling (MLM) and causal language modeling (CLM). By default it prepares data for MLM, but we can switch to CLM by setting the argument <code>mlm=False</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForLanguageModeling tokenizer.pad_token = tokenizer.eos_token data_collator = DataCollatorForLanguageModeling(tokenizer, mlm=<span class="hljs-literal">False</span>)</pre></div> <p>Let’s have a look at an example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>out = data_collator([tokenized_datasets[<span class="hljs-string">"train"</span>][i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>)]) <span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> out: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"<span class="hljs-subst">{key}</span> shape: <span class="hljs-subst">{out[key].shape}</span>"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>input_ids shape: torch.Size([<span class="hljs-number">5</span>, <span class="hljs-number">128</span>]) attention_mask shape: torch.Size([<span class="hljs-number">5</span>, <span class="hljs-number">128</span>]) labels shape: torch.Size([<span class="hljs-number">5</span>, <span class="hljs-number">128</span>])</pre></div> <p>We can see that the examples have been stacked and all the tensors have the same shape.</p> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>⚠️ Shifting the inputs and labels to align them happens inside the model, so the data collator just copies the inputs to create the labels.</p></div> <p>Now we have everything in place to actually train our model — that wasn’t so much work after all! Before we start training we should log in to Hugging Face. If you’re working in a notebook, you can do so with the following utility function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login notebook_login()</pre></div> <p>This will display a widget where you can enter your Hugging Face login credentials.</p> <p>If you aren’t working in a notebook, just type the following line in your terminal:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-cli login</pre></div> <p>All that’s left to do is configure the training arguments and fire up the <code>Trainer</code>. We’ll use a cosine learning rate schedule with some warmup and an effective batch size of 256 (<code>per_device_train_batch_size</code> * <code>gradient_accumulation_steps</code>). Gradient accumulation is used when a single batch does not fit into memory, and incrementally builds up the gradient through several forward/backward passes. We’ll see this in action when we create the training loop with 🤗 Accelerate.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Trainer, TrainingArguments args = TrainingArguments( output_dir=<span class="hljs-string">"codeparrot-ds"</span>, per_device_train_batch_size=<span class="hljs-number">32</span>, per_device_eval_batch_size=<span class="hljs-number">32</span>, evaluation_strategy=<span class="hljs-string">"steps"</span>, eval_steps=<span class="hljs-number">5_000</span>, logging_steps=<span class="hljs-number">5_000</span>, gradient_accumulation_steps=<span class="hljs-number">8</span>, num_train_epochs=<span class="hljs-number">1</span>, weight_decay=<span class="hljs-number">0.1</span>, warmup_steps=<span class="hljs-number">1_000</span>, lr_scheduler_type=<span class="hljs-string">"cosine"</span>, learning_rate=<span class="hljs-number">5e-4</span>, save_steps=<span class="hljs-number">5_000</span>, fp16=<span class="hljs-literal">True</span>, push_to_hub=<span class="hljs-literal">True</span>, ) trainer = Trainer( model=model, tokenizer=tokenizer, args=args, data_collator=data_collator, train_dataset=tokenized_datasets[<span class="hljs-string">"train"</span>], eval_dataset=tokenized_datasets[<span class="hljs-string">"valid"</span>], )</pre></div> <p>Now we can just start the <code>Trainer</code> and wait for training to finish. Depending on whether you run it on the full or a subset of the training set this will take 20 or 2 hours, respectively, so grab a few coffees and a good book to read!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.train()</pre></div> <p>After training completes, we can push the model and tokenizer to the Hub:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.push_to_hub()</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> It only took us about 30 lines of code in addition to the <code>TrainingArguments</code> to get from raw texts to training GPT-2. Try it out with your own dataset and see if you can get good results!</p></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 If you have access to a machine with multiple GPUs, try to run the code there. The <code>Trainer</code> automatically manages multiple machines, and this can speed up training tremendously.</p></div> <h2 class="relative group"><a id="code-generation-with-a-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#code-generation-with-a-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Code generation with a pipeline</span></h2> <p>Now is the moment of truth: let’s see how well the trained model actually works! We can see in the logs that the loss went down steadily, but to put the model to the test let’s take a look at how well it works on some prompts. To do that we’ll wrap the model in a text generation <code>pipeline</code>, and we’ll put it on the GPU for fast generations if there is one available:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline device = torch.device(<span class="hljs-string">"cuda"</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">"cpu"</span>) pipe = pipeline( <span class="hljs-string">"text-generation"</span>, model=<span class="hljs-string">"huggingface-course/codeparrot-ds"</span>, device=device )</pre></div> <p>Let’s start with the simple task of creating a scatter plot:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>txt = <span class="hljs-string">"""\ # create some data x = np.random.randn(100) y = np.random.randn(100) # create scatter plot with x, y """</span> <span class="hljs-built_in">print</span>(pipe(txt, num_return_sequences=<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>][<span class="hljs-string">"generated_text"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># create some data</span> x = np.random.randn(<span class="hljs-number">100</span>) y = np.random.randn(<span class="hljs-number">100</span>) <span class="hljs-comment"># create scatter plot with x, y</span> plt.scatter(x, y) <span class="hljs-comment"># create scatter</span></pre></div> <p>The result looks correct. Does it also work for a <code>pandas</code> operation? Let’s see if we can create a <code>DataFrame</code> from two arrays:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>txt = <span class="hljs-string">"""\ # create some data x = np.random.randn(100) y = np.random.randn(100) # create dataframe from x and y """</span> <span class="hljs-built_in">print</span>(pipe(txt, num_return_sequences=<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>][<span class="hljs-string">"generated_text"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># create some data</span> x = np.random.randn(<span class="hljs-number">100</span>) y = np.random.randn(<span class="hljs-number">100</span>) <span class="hljs-comment"># create dataframe from x and y</span> df = pd.DataFrame({<span class="hljs-string">'x'</span>: x, <span class="hljs-string">'y'</span>: y}) df.insert(<span class="hljs-number">0</span>,<span class="hljs-string">'x'</span>, x) <span class="hljs-keyword">for</span></pre></div> <p>Nice, that’s the correct answer — although it then inserts the column <code>x</code> again. Since the number of generated tokens is limited, the following <code>for</code> loop is cut off. Let’s see if we can do something a bit more complex and have the model help us use the <code>groupby</code> operation:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>txt = <span class="hljs-string">"""\ # dataframe with profession, income and name df = pd.DataFrame({'profession': x, 'income':y, 'name': z}) # calculate the mean income per profession """</span> <span class="hljs-built_in">print</span>(pipe(txt, num_return_sequences=<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>][<span class="hljs-string">"generated_text"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># dataframe with profession, income and name</span> df = pd.DataFrame({<span class="hljs-string">'profession'</span>: x, <span class="hljs-string">'income'</span>:y, <span class="hljs-string">'name'</span>: z}) <span class="hljs-comment"># calculate the mean income per profession</span> profession = df.groupby([<span class="hljs-string">'profession'</span>]).mean() <span class="hljs-comment"># compute the</span></pre></div> <p>Not bad; that’s the right way to do it. Finally, let’s see if we can also use it for <code>scikit-learn</code> and set up a Random Forest model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>txt = <span class="hljs-string">""" # import random forest regressor from scikit-learn from sklearn.ensemble import RandomForestRegressor # fit random forest model with 300 estimators on X, y: """</span> <span class="hljs-built_in">print</span>(pipe(txt, num_return_sequences=<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>][<span class="hljs-string">"generated_text"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># import random forest regressor from scikit-learn</span> <span class="hljs-keyword">from</span> sklearn.ensemble <span class="hljs-keyword">import</span> RandomForestRegressor <span class="hljs-comment"># fit random forest model with 300 estimators on X, y:</span> rf = RandomForestRegressor(n_estimators=<span class="hljs-number">300</span>, random_state=random_state, max_depth=<span class="hljs-number">3</span>) rf.fit(X, y) rf</pre></div> <p>Looking at these few examples, it seems that the model has learned some of the syntax of the Python data science stack (of course, we would need to evaluate it more thoroughly before deploying the model in the real world). Sometimes it requires more customization of the model training to achieve the necessary performance for a given use case, however. For example, what if we would like to dynamically update the batch size or have a conditional training loop that skips bad examples on the fly? One option would be to subclass the <code>Trainer</code> and add the necessary changes, but sometimes it’s simpler to write the training loop from scratch. That’s where 🤗 Accelerate comes in.</p> <h2 class="relative group"><a id="training-with-accelerate" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-with-accelerate"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training with 🤗 Accelerate</span></h2> <p>We’ve seen how to train a model with the <code>Trainer</code>, which can allow for some customization. However, sometimes we want full control over the training loop, or we want to make some exotic changes. In this case 🤗 Accelerate is a great choice, and in this section we’ll go through the steps to use it to train our model. To make things more interesting, we’ll also add a twist to the training loop.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/Hm8_PgVTFuc" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Since we are mainly interested in sensible autocompletion for the the data science libraries, it makes sense to give more weight to training samples that make more use of these libraries. We can easily identify these examples through the use of keywords such as <code>plt</code>, <code>pd</code>, <code>sk</code>, <code>fit</code>, and <code>predict</code>, which are the most frequent import names for <code>matplotlib.pyplot</code>, <code>pandas</code>, and <code>sklearn</code> as well as the fit/predict pattern of the latter. If these are each represented as a single token, we can easily check if they occur in the input sequence. Tokens can have a whitespace prefix, so we’ll also check for those versions in the tokenizer vocabulary. To verify that it works, we’ll add one test token which should be split into multiple tokens:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>keytoken_ids = [] <span class="hljs-keyword">for</span> keyword <span class="hljs-keyword">in</span> [ <span class="hljs-string">"plt"</span>, <span class="hljs-string">"pd"</span>, <span class="hljs-string">"sk"</span>, <span class="hljs-string">"fit"</span>, <span class="hljs-string">"predict"</span>, <span class="hljs-string">" plt"</span>, <span class="hljs-string">" pd"</span>, <span class="hljs-string">" sk"</span>, <span class="hljs-string">" fit"</span>, <span class="hljs-string">" predict"</span>, <span class="hljs-string">"testtest"</span>, ]: ids = tokenizer([keyword]).input_ids[<span class="hljs-number">0</span>] <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(ids) == <span class="hljs-number">1</span>: keytoken_ids.append(ids[<span class="hljs-number">0</span>]) <span class="hljs-keyword">else</span>: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Keyword has not single token: <span class="hljs-subst">{keyword}</span>"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'Keyword has not single token: testtest'</span></pre></div> <p>Great, that seems to work nicely! We can now write a custom loss function that takes the input sequence, the logits, and the key tokens we just selected as inputs. First we need to align the logits and inputs: the input sequence shifted by one to the right forms the labels, since the next token is the label for the current token. We can achieve this by starting the labels from the second token of the input sequence, since the model does not make a prediction for the first token anyway. Then we cut off the last logit, as we don’t have a label for the token that follows the full input sequence. With that we can compute the loss per sample and count the occurrences of all keywords in each sample. Finally, we calculate the weighted average over all samples using the occurrences as weights. Since we don’t want to throw away all the samples that have no keywords, we add 1 to the weights:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.nn <span class="hljs-keyword">import</span> CrossEntropyLoss <span class="hljs-keyword">import</span> torch <span class="hljs-keyword">def</span> <span class="hljs-title function_">keytoken_weighted_loss</span>(<span class="hljs-params">inputs, logits, keytoken_ids, alpha=<span class="hljs-number">1.0</span></span>): <span class="hljs-comment"># Shift so that tokens &lt; n predict n</span> shift_labels = inputs[..., <span class="hljs-number">1</span>:].contiguous() shift_logits = logits[..., :-<span class="hljs-number">1</span>, :].contiguous() <span class="hljs-comment"># Calculate per-token loss</span> loss_fct = CrossEntropyLoss(reduce=<span class="hljs-literal">False</span>) loss = loss_fct(shift_logits.view(-<span class="hljs-number">1</span>, shift_logits.size(-<span class="hljs-number">1</span>)), shift_labels.view(-<span class="hljs-number">1</span>)) <span class="hljs-comment"># Resize and average loss per sample</span> loss_per_sample = loss.view(shift_logits.size(<span class="hljs-number">0</span>), shift_logits.size(<span class="hljs-number">1</span>)).mean(axis=<span class="hljs-number">1</span>) <span class="hljs-comment"># Calculate and scale weighting</span> weights = torch.stack([(inputs == kt).<span class="hljs-built_in">float</span>() <span class="hljs-keyword">for</span> kt <span class="hljs-keyword">in</span> keytoken_ids]).<span class="hljs-built_in">sum</span>( axis=[<span class="hljs-number">0</span>, <span class="hljs-number">2</span>] ) weights = alpha * (<span class="hljs-number">1.0</span> + weights) <span class="hljs-comment"># Calculate weighted average</span> weighted_loss = (loss_per_sample * weights).mean() <span class="hljs-keyword">return</span> weighted_loss</pre></div> <p>Before we can start training with this awesome new loss function, we need to prepare a few things:</p> <ul><li>We need dataloaders to load the data in batches.</li> <li>We need to set up weight decay parameters.</li> <li>From time to time we want to evaluate, so it makes sense to wrap the evaluation code in a function.</li></ul> <p>Let’s start with the dataloaders. We only need to set the dataset’s format to <code>"torch"</code>, and then we can pass it to a PyTorch <code>DataLoader</code> with the appropriate batch size:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.utils.data.dataloader <span class="hljs-keyword">import</span> DataLoader tokenized_dataset.set_format(<span class="hljs-string">"torch"</span>) train_dataloader = DataLoader(tokenized_dataset[<span class="hljs-string">"train"</span>], batch_size=<span class="hljs-number">32</span>, shuffle=<span class="hljs-literal">True</span>) eval_dataloader = DataLoader(tokenized_dataset[<span class="hljs-string">"valid"</span>], batch_size=<span class="hljs-number">32</span>)</pre></div> <p>Next, we group the parameters so that the optimizer knows which ones will get an additional weight decay. Usually, all bias and LayerNorm weights terms are exempt from this; here’s how we can do this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>weight_decay = <span class="hljs-number">0.1</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">get_grouped_params</span>(<span class="hljs-params">model, no_decay=[<span class="hljs-string">"bias"</span>, <span class="hljs-string">"LayerNorm.weight"</span>]</span>): params_with_wd, params_without_wd = [], [] <span class="hljs-keyword">for</span> n, p <span class="hljs-keyword">in</span> model.named_parameters(): <span class="hljs-keyword">if</span> <span class="hljs-built_in">any</span>(nd <span class="hljs-keyword">in</span> n <span class="hljs-keyword">for</span> nd <span class="hljs-keyword">in</span> no_decay): params_without_wd.append(p) <span class="hljs-keyword">else</span>: params_with_wd.append(p) <span class="hljs-keyword">return</span> [ {<span class="hljs-string">"params"</span>: params_with_wd, <span class="hljs-string">"weight_decay"</span>: weight_decay}, {<span class="hljs-string">"params"</span>: params_without_wd, <span class="hljs-string">"weight_decay"</span>: <span class="hljs-number">0.0</span>}, ]</pre></div> <p>Since we want to evaluate the model regularly on the validation set during training, let’s write a function for that as well. It just runs through the evaluation dataloader and gathers all the losses across processes:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">evaluate</span>(): model.<span class="hljs-built_in">eval</span>() losses = [] <span class="hljs-keyword">for</span> step, batch <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(eval_dataloader): <span class="hljs-keyword">with</span> torch.no_grad(): outputs = model(batch[<span class="hljs-string">"input_ids"</span>], labels=batch[<span class="hljs-string">"input_ids"</span>]) losses.append(accelerator.gather(outputs.loss)) loss = torch.mean(torch.cat(losses)) <span class="hljs-keyword">try</span>: perplexity = torch.exp(loss) <span class="hljs-keyword">except</span> OverflowError: perplexity = <span class="hljs-built_in">float</span>(<span class="hljs-string">"inf"</span>) <span class="hljs-keyword">return</span> loss.item(), perplexity.item()</pre></div> <p>With the <code>evaluate()</code> function we can report loss and <a href="/course/chapter7/3">perplexity</a> at regular intervals. Next, we redefine our model to make sure we train from scratch again:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model = GPT2LMHeadModel(config)</pre></div> <p>We can then define our optimizer, using the function from before to split the parameters for weight decay:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.optim <span class="hljs-keyword">import</span> AdamW optimizer = AdamW(get_grouped_params(model), lr=<span class="hljs-number">5e-4</span>)</pre></div> <p>Now let’s prepare the model, optimizer, and dataloaders so we can start training:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator accelerator = Accelerator(fp16=<span class="hljs-literal">True</span>) model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>🚨 If you’re training on a TPU, you’ll need to move all the code starting at the cell above into a dedicated training function. See <a href="/course/chapter3">Chapter 3</a> for more details.</p></div> <p>Now that we have sent our <code>train_dataloader</code> to <code>accelerator.prepare()</code>, we can use its length to compute the number of training steps. Remember that we should always do this after preparing the dataloader, as that method will change its length. We use a classic linear schedule from the learning rate to 0:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler num_train_epochs = <span class="hljs-number">1</span> num_update_steps_per_epoch = <span class="hljs-built_in">len</span>(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( name=<span class="hljs-string">"linear"</span>, optimizer=optimizer, num_warmup_steps=<span class="hljs-number">1_000</span>, num_training_steps=num_training_steps, )</pre></div> <p>Lastly, to push our model to the Hub, we will need to create a <code>Repository</code> object in a working folder. First log in to the Hugging Face Hub, if you aren’t logged in already. We’ll determine the repository name from the model ID we want to give our model (feel free to replace the <code>repo_name</code> with your own choice; it just needs to contain your username, which is what the function <code>get_full_repo_name()</code> does):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> Repository, get_full_repo_name model_name = <span class="hljs-string">"codeparrot-ds-accelerate"</span> repo_name = get_full_repo_name(model_name) repo_name</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'sgugger/codeparrot-ds-accelerate'</span></pre></div> <p>Then we can clone that repository in a local folder. If it already exists, this local folder should be an existing clone of the repository we are working with:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>output_dir = <span class="hljs-string">"codeparrot-ds-accelerate"</span> repo = Repository(output_dir, clone_from=repo_name)</pre></div> <p>We can now upload anything we save in <code>output_dir</code> by calling the <code>repo.push_to_hub()</code> method. This will help us upload the intermediate models at the end of each epoch.</p> <p>Before we train, let’s run a quick test to see if the evaluation function works properly:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>evaluate()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-number">10.934126853942871</span>, <span class="hljs-number">56057.14453125</span>)</pre></div> <p>Those are very high values for loss and perplexity, but that’s not surprising as we haven’t trained the model yet. With that, we have everything prepared to write the core part of the training script: the training loop. In the training loop we iterate over the dataloader and pass the batches to the model. With the logits, we can then evaluate our custom loss function. We scale the loss by the number of gradient accumulation steps so as not to create larger losses when aggregating more steps. Before we optimize, we also clip the gradients for better convergence. Finally, every few steps we evaluate the model on the evaluation set with our new <code>evaluate()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> tqdm.notebook <span class="hljs-keyword">import</span> tqdm gradient_accumulation_steps = <span class="hljs-number">8</span> eval_steps = <span class="hljs-number">5_000</span> model.train() completed_steps = <span class="hljs-number">0</span> <span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_train_epochs): <span class="hljs-keyword">for</span> step, batch <span class="hljs-keyword">in</span> tqdm( <span class="hljs-built_in">enumerate</span>(train_dataloader, start=<span class="hljs-number">1</span>), total=num_training_steps ): logits = model(batch[<span class="hljs-string">"input_ids"</span>]).logits loss = keytoken_weighted_loss(batch[<span class="hljs-string">"input_ids"</span>], logits, keytoken_ids) <span class="hljs-keyword">if</span> step % <span class="hljs-number">100</span> == <span class="hljs-number">0</span>: accelerator.<span class="hljs-built_in">print</span>( { <span class="hljs-string">"lr"</span>: get_lr(), <span class="hljs-string">"samples"</span>: step * samples_per_step, <span class="hljs-string">"steps"</span>: completed_steps, <span class="hljs-string">"loss/train"</span>: loss.item() * gradient_accumulation_steps, } ) loss = loss / gradient_accumulation_steps accelerator.backward(loss) <span class="hljs-keyword">if</span> step % gradient_accumulation_steps == <span class="hljs-number">0</span>: accelerator.clip_grad_norm_(model.parameters(), <span class="hljs-number">1.0</span>) optimizer.step() lr_scheduler.step() optimizer.zero_grad() completed_steps += <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> (step % (eval_steps * gradient_accumulation_steps)) == <span class="hljs-number">0</span>: eval_loss, perplexity = evaluate() accelerator.<span class="hljs-built_in">print</span>({<span class="hljs-string">"loss/eval"</span>: eval_loss, <span class="hljs-string">"perplexity"</span>: perplexity}) model.train() accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) <span class="hljs-keyword">if</span> accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=<span class="hljs-string">f"Training in progress step <span class="hljs-subst">{step}</span>"</span>, blocking=<span class="hljs-literal">False</span> )</pre></div> <p>And that’s it — you now have your own custom training loop for causal language models such as GPT-2 that you can further customize to your needs.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Either create your own custom loss function tailored to your use case, or add another custom step into the training loop.</p></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> When running long training experiments it’s a good idea to log important metrics using tools such as TensorBoard or Weights &amp; Biases. Add proper logging to the training loop so you can always check how the training is going.</p></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter7/5?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Summarization</a> <a href="/learn/nlp-course/chapter7/7?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Question answering<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;training-a-causal-language-model-from-scratch&quot;,&quot;url&quot;:&quot;#training-a-causal-language-model-from-scratch&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Gathering the data&quot;,&quot;id&quot;:&quot;gathering-the-data&quot;,&quot;url&quot;:&quot;#gathering-the-data&quot;},{&quot;title&quot;:&quot;Preparing the dataset&quot;,&quot;id&quot;:&quot;preparing-the-dataset&quot;,&quot;url&quot;:&quot;#preparing-the-dataset&quot;},{&quot;title&quot;:&quot;Initializing a new model&quot;,&quot;id&quot;:&quot;initializing-a-new-model&quot;,&quot;url&quot;:&quot;#initializing-a-new-model&quot;},{&quot;title&quot;:&quot;Code generation with a pipeline&quot;,&quot;id&quot;:&quot;code-generation-with-a-pipeline&quot;,&quot;url&quot;:&quot;#code-generation-with-a-pipeline&quot;},{&quot;title&quot;:&quot;Training with 🤗 Accelerate&quot;,&quot;id&quot;:&quot;training-with-accelerate&quot;,&quot;url&quot;:&quot;#training-with-accelerate&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#training-a-causal-language-model-from-scratch" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-a-causal-language-model-from-scratch"><wbr>Training a causal language model from scratch</a> <a href="#gathering-the-data" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-gathering-the-data"><wbr>Gathering the data</a> <a href="#preparing-the-dataset" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preparing-the-dataset"><wbr>Preparing the dataset</a> <a href="#initializing-a-new-model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-initializing-a-new-model"><wbr>Initializing a new model</a> <a href="#code-generation-with-a-pipeline" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-code-generation-with-a-pipeline"><wbr>Code generation with a pipeline</a> <a href="#training-with-accelerate" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-with-accelerate"><wbr>Training with 🤗 <wbr>Accelerate</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter7/6" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter7/6"); } </script> <iframe name="__privateStripeMetricsController0540" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter7%2F6%3Ffw%3Dpt&amp;title=Training%20a%20causal%20language%20model%20from%20scratch%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:30.427Z
Summarization - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter7/5?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#summarization)Summarization [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-7-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section5_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section5_pt.ipynb) In this section we’ll take a look at how Transformer models can be used to condense long documents into summaries, a task known as _text summarization_. This is one of the most challenging NLP tasks as it requires a range of abilities, such as understanding long passages and generating coherent text that captures the main topics in a document. However, when done well, text summarization is a powerful tool that can speed up various business processes by relieving the burden of domain experts to read long documents in detail. Although there already exist various fine-tuned models for summarization on the [Hugging Face Hub](https://huggingface.co/models?pipeline_tag=summarization&sort=downloads), almost all of these are only suitable for English documents. So, to add a twist in this section, we’ll train a bilingual model for English and Spanish. By the end of this section, you’ll have a [model](https://huggingface.co/huggingface-course/mt5-small-finetuned-amazon-en-es) that can summarize customer reviews like the one shown here: As we’ll see, these summaries are concise because they’re learned from the titles that customers provide in their product reviews. Let’s start by putting together a suitable bilingual corpus for this task. ## [](#preparing-a-multilingual-corpus)Preparing a multilingual corpus We’ll use the [Multilingual Amazon Reviews Corpus](https://huggingface.co/datasets/amazon_reviews_multi) to create our bilingual summarizer. This corpus consists of Amazon product reviews in six languages and is typically used to benchmark multilingual classifiers. However, since each review is accompanied by a short title, we can use the titles as the target summaries for our model to learn from! To get started, let’s download the English and Spanish subsets from the Hugging Face Hub: ``` from datasets import load_dataset spanish_dataset = load_dataset("amazon_reviews_multi", "es") english_dataset = load_dataset("amazon_reviews_multi", "en") english_dataset``` ``` DatasetDict({ train: Dataset({ features: ['review_id', 'product_id', 'reviewer_id', 'stars', 'review_body', 'review_title', 'language', 'product_category'], num_rows: 200000 }) validation: Dataset({ features: ['review_id', 'product_id', 'reviewer_id', 'stars', 'review_body', 'review_title', 'language', 'product_category'], num_rows: 5000 }) test: Dataset({ features: ['review_id', 'product_id', 'reviewer_id', 'stars', 'review_body', 'review_title', 'language', 'product_category'], num_rows: 5000 }) })``` As you can see, for each language there are 200,000 reviews for the `train` split, and 5,000 reviews for each of the `validation` and `test` splits. The review information we are interested in is contained in the `review_body` and `review_title` columns. Let’s take a look at a few examples by creating a simple function that takes a random sample from the training set with the techniques we learned in [Chapter 5](/course/chapter5): ``` def show_samples(dataset, num_samples=3, seed=42): sample = dataset["train"].shuffle(seed=seed).select(range(num_samples)) for example in sample: print(f"\n'>> Title: {example['review_title']}'") print(f"'>> Review: {example['review_body']}'") show_samples(english_dataset)``` ``` '>> Title: Worked in front position, not rear' '>> Review: 3 stars because these are not rear brakes as stated in the item description. At least the mount adapter only worked on the front fork of the bike that I got it for.' '>> Title: meh' '>> Review: Does it’s job and it’s gorgeous but mine is falling apart, I had to basically put it together again with hot glue' '>> Title: Can\'t beat these for the money' '>> Review: Bought this for handling miscellaneous aircraft parts and hanger "stuff" that I needed to organize; it really fit the bill. The unit arrived quickly, was well packaged and arrived intact (always a good sign). There are five wall mounts-- three on the top and two on the bottom. I wanted to mount it on the wall, so all I had to do was to remove the top two layers of plastic drawers, as well as the bottom corner drawers, place it when I wanted and mark it; I then used some of the new plastic screw in wall anchors (the 50 pound variety) and it easily mounted to the wall. Some have remarked that they wanted dividers for the drawers, and that they made those. Good idea. My application was that I needed something that I can see the contents at about eye level, so I wanted the fuller-sized drawers. I also like that these are the new plastic that doesn\'t get brittle and split like my older plastic drawers did. I like the all-plastic construction. It\'s heavy duty enough to hold metal parts, but being made of plastic it\'s not as heavy as a metal frame, so you can easily mount it to the wall and still load it up with heavy stuff, or light stuff. No problem there. For the money, you can\'t beat it. Best one of these I\'ve bought to date-- and I\'ve been using some version of these for over forty years.'``` ✏️ **Try it out!** Change the random seed in the `Dataset.shuffle()` command to explore other reviews in the corpus. If you’re a Spanish speaker, take a look at some of the reviews in `spanish_dataset` to see if the titles also seem like reasonable summaries. This sample shows the diversity of reviews one typically finds online, ranging from positive to negative (and everything in between!). Although the example with the “meh” title is not very informative, the other titles look like decent summaries of the reviews themselves. Training a summarization model on all 400,000 reviews would take far too long on a single GPU, so instead we’ll focus on generating summaries for a single domain of products. To get a feel for what domains we can choose from, let’s convert `english_dataset` to a `pandas.DataFrame` and compute the number of reviews per product category: ``` english_dataset.set_format("pandas") english_df = english_dataset["train"][:] english_df["product_category"].value_counts()[:20]``` ``` home 17679 apparel 15951 wireless 15717 other 13418 beauty 12091 drugstore 11730 kitchen 10382 toy 8745 sports 8277 automotive 7506 lawn_and_garden 7327 home_improvement 7136 pet_products 7082 digital_ebook_purchase 6749 pc 6401 electronics 6186 office_product 5521 shoes 5197 grocery 4730 book 3756 Name: product_category, dtype: int64``` The most popular products in the English dataset are about household items, clothing, and wireless electronics. To stick with the Amazon theme, though, let’s focus on summarizing book reviews — after all, this is what the company was founded on! We can see two product categories that fit the bill (`book` and `digital_ebook_purchase`), so let’s filter the datasets in both languages for just these products. As we saw in [Chapter 5](/course/chapter5), the `Dataset.filter()` function allows us to slice a dataset very efficiently, so we can define a simple function to do this: ``` def filter_books(example): return ( example["product_category"] == "book" or example["product_category"] == "digital_ebook_purchase" )``` Now when we apply this function to `english_dataset` and `spanish_dataset`, the result will contain just those rows involving the book categories. Before applying the filter, let’s switch the format of `english_dataset` from `"pandas"` back to `"arrow"`: ``` english_dataset.reset_format()``` We can then apply the filter function, and as a sanity check let’s inspect a sample of reviews to see if they are indeed about books: ``` spanish_books = spanish_dataset.filter(filter_books) english_books = english_dataset.filter(filter_books) show_samples(english_books)``` ``` '>> Title: I\'m dissapointed.' '>> Review: I guess I had higher expectations for this book from the reviews. I really thought I\'d at least like it. The plot idea was great. I loved Ash but, it just didnt go anywhere. Most of the book was about their radio show and talking to callers. I wanted the author to dig deeper so we could really get to know the characters. All we know about Grace is that she is attractive looking, Latino and is kind of a brat. I\'m dissapointed.' '>> Title: Good art, good price, poor design' '>> Review: I had gotten the DC Vintage calendar the past two years, but it was on backorder forever this year and I saw they had shrunk the dimensions for no good reason. This one has good art choices but the design has the fold going through the picture, so it\'s less aesthetically pleasing, especially if you want to keep a picture to hang. For the price, a good calendar' '>> Title: Helpful' '>> Review: Nearly all the tips useful and. I consider myself an intermediate to advanced user of OneNote. I would highly recommend.'``` Okay, we can see that the reviews are not strictly about books and might refer to things like calendars and electronic applications such as OneNote. Nevertheless, the domain seems about right to train a summarization model on. Before we look at various models that are suitable for this task, we have one last bit of data preparation to do: combining the English and Spanish reviews as a single `DatasetDict` object. 🤗 Datasets provides a handy `concatenate_datasets()` function that (as the name suggests) will stack two `Dataset` objects on top of each other. So, to create our bilingual dataset, we’ll loop over each split, concatenate the datasets for that split, and shuffle the result to ensure our model doesn’t overfit to a single language: ``` from datasets import concatenate_datasets, DatasetDict books_dataset = DatasetDict() for split in english_books.keys(): books_dataset[split] = concatenate_datasets( [english_books[split], spanish_books[split]] ) books_dataset[split] = books_dataset[split].shuffle(seed=42) show_samples(books_dataset)``` ``` '>> Title: Easy to follow!!!!' '>> Review: I loved The dash diet weight loss Solution. Never hungry. I would recommend this diet. Also the menus are well rounded. Try it. Has lots of the information need thanks.' '>> Title: PARCIALMENTE DAÑADO' '>> Review: Me llegó el día que tocaba, junto a otros libros que pedí, pero la caja llegó en mal estado lo cual dañó las esquinas de los libros porque venían sin protección (forro).' '>> Title: no lo he podido descargar' '>> Review: igual que el anterior'``` This certainly looks like a mix of English and Spanish reviews! Now that we have a training corpus, one final thing to check is the distribution of words in the reviews and their titles. This is especially important for summarization tasks, where short reference summaries in the data can bias the model to only output one or two words in the generated summaries. The plots below show the word distributions, and we can see that the titles are heavily skewed toward just 1-2 words: ![Word count distributions for the review titles and texts.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/review-lengths.svg) ![Word count distributions for the review titles and texts.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/review-lengths-dark.svg) To deal with this, we’ll filter out the examples with very short titles so that our model can produce more interesting summaries. Since we’re dealing with English and Spanish texts, we can use a rough heuristic to split the titles on whitespace and then use our trusty `Dataset.filter()` method as follows: ``` books_dataset = books_dataset.filter(lambda x: len(x["review_title"].split()) > 2)``` Now that we’ve prepared our corpus, let’s take a look at a few possible Transformer models that one might fine-tune on it! ## [](#models-for-text-summarization)Models for text summarization If you think about it, text summarization is a similar sort of task to machine translation: we have a body of text like a review that we’d like to “translate” into a shorter version that captures the salient features of the input. Accordingly, most Transformer models for summarization adopt the encoder-decoder architecture that we first encountered in [Chapter 1](/course/chapter1), although there are some exceptions like the GPT family of models which can also be used for summarization in few-shot settings. The following table lists some popular pretrained models that can be fine-tuned for summarization. | Transformer model | Description | Multilingual? | | --- | --- | --- | | [GPT-2](https://huggingface.co/gpt2-xl) | Although trained as an auto-regressive language model, you can make GPT-2 generate summaries by appending “TL;DR” at the end of the input text. | ❌ | | [PEGASUS](https://huggingface.co/google/pegasus-large) | Uses a pretraining objective to predict masked sentences in multi-sentence texts. This pretraining objective is closer to summarization than vanilla language modeling and scores highly on popular benchmarks. | ❌ | | [T5](https://huggingface.co/t5-base) | A universal Transformer architecture that formulates all tasks in a text-to-text framework; e.g., the input format for the model to summarize a document is `summarize: ARTICLE`. | ❌ | | [mT5](https://huggingface.co/google/mt5-base) | A multilingual version of T5, pretrained on the multilingual Common Crawl corpus (mC4), covering 101 languages. | ✅ | | [BART](https://huggingface.co/facebook/bart-base) | A novel Transformer architecture with both an encoder and a decoder stack trained to reconstruct corrupted input that combines the pretraining schemes of BERT and GPT-2. | ❌ | | [mBART-50](https://huggingface.co/facebook/mbart-large-50) | A multilingual version of BART, pretrained on 50 languages. | ✅ | As you can see from this table, the majority of Transformer models for summarization (and indeed most NLP tasks) are monolingual. This is great if your task is in a “high-resource” language like English or German, but less so for the thousands of other languages in use across the world. Fortunately, there is a class of multilingual Transformer models, like mT5 and mBART, that come to the rescue. These models are pretrained using language modeling, but with a twist: instead of training on a corpus of one language, they are trained jointly on texts in over 50 languages at once! We’ll focus on mT5, an interesting architecture based on T5 that was pretrained in a text-to-text framework. In T5, every NLP task is formulated in terms of a prompt prefix like `summarize:` which conditions the model to adapt the generated text to the prompt. As shown in the figure below, this makes T5 extremely versatile, as you can solve many tasks with a single model! ![Different tasks performed by the T5 architecture.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/t5.svg) ![Different tasks performed by the T5 architecture.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/t5-dark.svg) mT5 doesn’t use prefixes, but shares much of the versatility of T5 and has the advantage of being multilingual. Now that we’ve picked a model, let’s take a look at preparing our data for training. ✏️ **Try it out!** Once you’ve worked through this section, see how well mT5 compares to mBART by fine-tuning the latter with the same techniques. For bonus points, you can also try fine-tuning T5 on just the English reviews. Since T5 has a special prefix prompt, you’ll need to prepend `summarize:` to the input examples in the preprocessing steps below. ## [](#preprocessing-the-data)Preprocessing the data Our next task is to tokenize and encode our reviews and their titles. As usual, we begin by loading the tokenizer associated with the pretrained model checkpoint. We’ll use `mt5-small` as our checkpoint so we can fine-tune the model in a reasonable amount of time: ``` from transformers import AutoTokenizer model_checkpoint = "google/mt5-small" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)``` 💡 In the early stages of your NLP projects, a good practice is to train a class of “small” models on a small sample of data. This allows you to debug and iterate faster toward an end-to-end workflow. Once you are confident in the results, you can always scale up the model by simply changing the model checkpoint! Let’s test out the mT5 tokenizer on a small example: ``` inputs = tokenizer("I loved reading the Hunger Games!") inputs``` ``` {'input_ids': [336, 259, 28387, 11807, 287, 62893, 295, 12507, 1], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1]}``` Here we can see the familiar `input_ids` and `attention_mask` that we encountered in our first fine-tuning experiments back in [Chapter 3](/course/chapter3). Let’s decode these input IDs with the tokenizer’s `convert_ids_to_tokens()` function to see what kind of tokenizer we’re dealing with: ``` tokenizer.convert_ids_to_tokens(inputs.input_ids)``` ``` ['▁I', '▁', 'loved', '▁reading', '▁the', '▁Hung', 'er', '▁Games', '</s>']``` The special Unicode character `▁` and end-of-sequence token `</s>` indicate that we’re dealing with the SentencePiece tokenizer, which is based on the Unigram segmentation algorithm discussed in [Chapter 6](/course/chapter6). Unigram is especially useful for multilingual corpora since it allows SentencePiece to be agnostic about accents, punctuation, and the fact that many languages, like Japanese, do not have whitespace characters. To tokenize our corpus, we have to deal with a subtlety associated with summarization: because our labels are also text, it is possible that they exceed the model’s maximum context size. This means we need to apply truncation to both the reviews and their titles to ensure we don’t pass excessively long inputs to our model. The tokenizers in 🤗 Transformers provide a nifty `text_target` argument that allows you to tokenize the labels in parallel to the inputs. Here is an example of how the inputs and targets are processed for mT5: ``` max_input_length = 512 max_target_length = 30 def preprocess_function(examples): model_inputs = tokenizer( examples["review_body"], max_length=max_input_length, truncation=True, ) labels = tokenizer( examples["review_title"], max_length=max_target_length, truncation=True ) model_inputs["labels"] = labels["input_ids"] return model_inputs``` Let’s walk through this code to understand what’s happening. The first thing we’ve done is define values for `max_input_length` and `max_target_length`, which set the upper limits for how long our reviews and titles can be. Since the review body is typically much larger than the title, we’ve scaled these values accordingly. With `preprocess_function()`, it is then a simple matter to tokenize the whole corpus using the handy `Dataset.map()` function we’ve used extensively throughout this course: ``` tokenized_datasets = books_dataset.map(preprocess_function, batched=True)``` Now that the corpus has been preprocessed, let’s take a look at some metrics that are commonly used for summarization. As we’ll see, there is no silver bullet when it comes to measuring the quality of machine-generated text. 💡 You may have noticed that we used `batched=True` in our `Dataset.map()` function above. This encodes the examples in batches of 1,000 (the default) and allows you to make use of the multithreading capabilities of the fast tokenizers in 🤗 Transformers. Where possible, try using `batched=True` to get the most out of your preprocessing! ## [](#metrics-for-text-summarization)Metrics for text summarization In comparison to most of the other tasks we’ve covered in this course, measuring the performance of text generation tasks like summarization or translation is not as straightforward. For example, given a review like “I loved reading the Hunger Games”, there are multiple valid summaries, like “I loved the Hunger Games” or “Hunger Games is a great read”. Clearly, applying some sort of exact match between the generated summary and the label is not a good solution — even humans would fare poorly under such a metric, because we all have our own writing style. For summarization, one of the most commonly used metrics is the [ROUGE score](https://en.wikipedia.org/wiki/ROUGE_(metric)) (short for Recall-Oriented Understudy for Gisting Evaluation). The basic idea behind this metric is to compare a generated summary against a set of reference summaries that are typically created by humans. To make this more precise, suppose we want to compare the following two summaries: ``` generated_summary = "I absolutely loved reading the Hunger Games" reference_summary = "I loved reading the Hunger Games"``` One way to compare them could be to count the number of overlapping words, which in this case would be 6. However, this is a bit crude, so instead ROUGE is based on computing the _precision_ and _recall_ scores for the overlap. 🙋 Don’t worry if this is the first time you’ve heard of precision and recall — we’ll go through some explicit examples together to make it all clear. These metrics are usually encountered in classification tasks, so if you want to understand how precision and recall are defined in that context, we recommend checking out the `scikit-learn` [guides](https://scikit-learn.org/stable/auto_examples/model_selection/plot_precision_recall.html). For ROUGE, recall measures how much of the reference summary is captured by the generated one. If we are just comparing words, recall can be calculated according to the following formula: Recall\=Number of overlapping wordsTotal number of words in reference summary \\mathrm{Recall} = \\frac{\\mathrm{Number\\,of\\,overlapping\\, words}}{\\mathrm{Total\\, number\\, of\\, words\\, in\\, reference\\, summary}} For our simple example above, this formula gives a perfect recall of 6/6 = 1; i.e., all the words in the reference summary have been produced by the model. This may sound great, but imagine if our generated summary had been “I really really loved reading the Hunger Games all night”. This would also have perfect recall, but is arguably a worse summary since it is verbose. To deal with these scenarios we also compute the precision, which in the ROUGE context measures how much of the generated summary was relevant: Precision\=Number of overlapping wordsTotal number of words in generated summary \\mathrm{Precision} = \\frac{\\mathrm{Number\\,of\\,overlapping\\, words}}{\\mathrm{Total\\, number\\, of\\, words\\, in\\, generated\\, summary}} Applying this to our verbose summary gives a precision of 6/10 = 0.6, which is considerably worse than the precision of 6/7 = 0.86 obtained by our shorter one. In practice, both precision and recall are usually computed, and then the F1-score (the harmonic mean of precision and recall) is reported. We can do this easily in 🤗 Datasets by first installing the `rouge_score` package: and then loading the ROUGE metric as follows: ``` import evaluate rouge_score = evaluate.load("rouge")``` Then we can use the `rouge_score.compute()` function to calculate all the metrics at once: ``` scores = rouge_score.compute( predictions=[generated_summary], references=[reference_summary] ) scores``` ``` {'rouge1': AggregateScore(low=Score(precision=0.86, recall=1.0, fmeasure=0.92), mid=Score(precision=0.86, recall=1.0, fmeasure=0.92), high=Score(precision=0.86, recall=1.0, fmeasure=0.92)), 'rouge2': AggregateScore(low=Score(precision=0.67, recall=0.8, fmeasure=0.73), mid=Score(precision=0.67, recall=0.8, fmeasure=0.73), high=Score(precision=0.67, recall=0.8, fmeasure=0.73)), 'rougeL': AggregateScore(low=Score(precision=0.86, recall=1.0, fmeasure=0.92), mid=Score(precision=0.86, recall=1.0, fmeasure=0.92), high=Score(precision=0.86, recall=1.0, fmeasure=0.92)), 'rougeLsum': AggregateScore(low=Score(precision=0.86, recall=1.0, fmeasure=0.92), mid=Score(precision=0.86, recall=1.0, fmeasure=0.92), high=Score(precision=0.86, recall=1.0, fmeasure=0.92))}``` Whoa, there’s a lot of information in that output — what does it all mean? First, 🤗 Datasets actually computes confidence intervals for precision, recall, and F1-score; these are the `low`, `mid`, and `high` attributes you can see here. Moreover, 🤗 Datasets computes a variety of ROUGE scores which are based on different types of text granularity when comparing the generated and reference summaries. The `rouge1` variant is the overlap of unigrams — this is just a fancy way of saying the overlap of words and is exactly the metric we’ve discussed above. To verify this, let’s pull out the `mid` value of our scores: ``` Score(precision=0.86, recall=1.0, fmeasure=0.92)``` Great, the precision and recall numbers match up! Now what about those other ROUGE scores? `rouge2` measures the overlap between bigrams (think the overlap of pairs of words), while `rougeL` and `rougeLsum` measure the longest matching sequences of words by looking for the longest common substrings in the generated and reference summaries. The “sum” in `rougeLsum` refers to the fact that this metric is computed over a whole summary, while `rougeL` is computed as the average over individual sentences. ✏️ **Try it out!** Create your own example of a generated and reference summary and see if the resulting ROUGE scores agree with a manual calculation based on the formulas for precision and recall. For bonus points, split the text into bigrams and compare the precision and recall for the `rouge2` metric. We’ll use these ROUGE scores to track the performance of our model, but before doing that let’s do something every good NLP practitioner should do: create a strong, yet simple baseline! ### [](#creating-a-strong-baseline)Creating a strong baseline A common baseline for text summarization is to simply take the first three sentences of an article, often called the _lead-3_ baseline. We could use full stops to track the sentence boundaries, but this will fail on acronyms like “U.S.” or “U.N.” — so instead we’ll use the `nltk` library, which includes a better algorithm to handle these cases. You can install the package using `pip` as follows: and then download the punctuation rules: ``` import nltk nltk.download("punkt")``` Next, we import the sentence tokenizer from `nltk` and create a simple function to extract the first three sentences in a review. The convention in text summarization is to separate each summary with a newline, so let’s also include this and test it on a training example: ``` from nltk.tokenize import sent_tokenize def three_sentence_summary(text): return "\n".join(sent_tokenize(text)[:3]) print(three_sentence_summary(books_dataset["train"][1]["review_body"]))``` ``` 'I grew up reading Koontz, and years ago, I stopped,convinced i had "outgrown" him.' 'Still,when a friend was looking for something suspenseful too read, I suggested Koontz.' 'She found Strangers.'``` This seems to work, so let’s now implement a function that extracts these “summaries” from a dataset and computes the ROUGE scores for the baseline: ``` def evaluate_baseline(dataset, metric): summaries = [three_sentence_summary(text) for text in dataset["review_body"]] return metric.compute(predictions=summaries, references=dataset["review_title"])``` We can then use this function to compute the ROUGE scores over the validation set and prettify them a bit using Pandas: ``` import pandas as pd score = evaluate_baseline(books_dataset["validation"], rouge_score) rouge_names = ["rouge1", "rouge2", "rougeL", "rougeLsum"] rouge_dict = dict((rn, round(score[rn].mid.fmeasure * 100, 2)) for rn in rouge_names) rouge_dict``` ``` {'rouge1': 16.74, 'rouge2': 8.83, 'rougeL': 15.6, 'rougeLsum': 15.96}``` We can see that the `rouge2` score is significantly lower than the rest; this likely reflects the fact that review titles are typically concise and so the lead-3 baseline is too verbose. Now that we have a good baseline to work from, let’s turn our attention toward fine-tuning mT5! ## [](#fine-tuning-mt5-with-the-trainer-api)Fine-tuning mT5 with the `Trainer` API Fine-tuning a model for summarization is very similar to the other tasks we’ve covered in this chapter. The first thing we need to do is load the pretrained model from the `mt5-small` checkpoint. Since summarization is a sequence-to-sequence task, we can load the model with the `AutoModelForSeq2SeqLM` class, which will automatically download and cache the weights: ``` from transformers import AutoModelForSeq2SeqLM model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)``` 💡 If you’re wondering why you don’t see any warnings about fine-tuning the model on a downstream task, that’s because for sequence-to-sequence tasks we keep all the weights of the network. Compare this to our text classification model in [Chapter 3](/course/chapter3), where the head of the pretrained model was replaced with a randomly initialized network. The next thing we need to do is log in to the Hugging Face Hub. If you’re running this code in a notebook, you can do so with the following utility function: ``` from huggingface_hub import notebook_login notebook_login()``` which will display a widget where you can enter your credentials. Alternatively, you can run this command in your terminal and log in there: We’ll need to generate summaries in order to compute ROUGE scores during training. Fortunately, 🤗 Transformers provides dedicated `Seq2SeqTrainingArguments` and `Seq2SeqTrainer` classes that can do this for us automatically! To see how this works, let’s first define the hyperparameters and other arguments for our experiments: ``` from transformers import Seq2SeqTrainingArguments batch_size = 8 num_train_epochs = 8 logging_steps = len(tokenized_datasets["train"]) // batch_size model_name = model_checkpoint.split("/")[-1] args = Seq2SeqTrainingArguments( output_dir=f"{model_name}-finetuned-amazon-en-es", evaluation_strategy="epoch", learning_rate=5.6e-5, per_device_train_batch_size=batch_size, per_device_eval_batch_size=batch_size, weight_decay=0.01, save_total_limit=3, num_train_epochs=num_train_epochs, predict_with_generate=True, logging_steps=logging_steps, push_to_hub=True, )``` Here, the `predict_with_generate` argument has been set to indicate that we should generate summaries during evaluation so that we can compute ROUGE scores for each epoch. As discussed in [Chapter 1](/course/chapter1), the decoder performs inference by predicting tokens one by one, and this is implemented by the model’s `generate()` method. Setting `predict_with_generate=True` tells the `Seq2SeqTrainer` to use that method for evaluation. We’ve also adjusted some of the default hyperparameters, like the learning rate, number of epochs, and weight decay, and we’ve set the `save_total_limit` option to only save up to 3 checkpoints during training — this is because even the “small” version of mT5 uses around a GB of hard drive space, and we can save a bit of room by limiting the number of copies we save. The `push_to_hub=True` argument will allow us to push the model to the Hub after training; you’ll find the repository under your user profile in the location defined by `output_dir`. Note that you can specify the name of the repository you want to push to with the `hub_model_id` argument (in particular, you will have to use this argument to push to an organization). For instance, when we pushed the model to the [`huggingface-course` organization](https://huggingface.co/huggingface-course), we added `hub_model_id="huggingface-course/mt5-finetuned-amazon-en-es"` to `Seq2SeqTrainingArguments`. The next thing we need to do is provide the trainer with a `compute_metrics()` function so that we can evaluate our model during training. For summarization this is a bit more involved than simply calling `rouge_score.compute()` on the model’s predictions, since we need to _decode_ the outputs and labels into text before we can compute the ROUGE scores. The following function does exactly that, and also makes use of the `sent_tokenize()` function from `nltk` to separate the summary sentences with newlines: ``` import numpy as np def compute_metrics(eval_pred): predictions, labels = eval_pred decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True) labels = np.where(labels != -100, labels, tokenizer.pad_token_id) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) decoded_preds = ["\n".join(sent_tokenize(pred.strip())) for pred in decoded_preds] decoded_labels = ["\n".join(sent_tokenize(label.strip())) for label in decoded_labels] result = rouge_score.compute( predictions=decoded_preds, references=decoded_labels, use_stemmer=True ) result = {key: value.mid.fmeasure * 100 for key, value in result.items()} return {k: round(v, 4) for k, v in result.items()}``` Next, we need to define a data collator for our sequence-to-sequence task. Since mT5 is an encoder-decoder Transformer model, one subtlety with preparing our batches is that during decoding we need to shift the labels to the right by one. This is required to ensure that the decoder only sees the previous ground truth labels and not the current or future ones, which would be easy for the model to memorize. This is similar to how masked self-attention is applied to the inputs in a task like [causal language modeling](/course/chapter7/6). Luckily, 🤗 Transformers provides a `DataCollatorForSeq2Seq` collator that will dynamically pad the inputs and the labels for us. To instantiate this collator, we simply need to provide the `tokenizer` and `model`: ``` from transformers import DataCollatorForSeq2Seq data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)``` Let’s see what this collator produces when fed a small batch of examples. First, we need to remove the columns with strings because the collator won’t know how to pad these elements: ``` tokenized_datasets = tokenized_datasets.remove_columns( books_dataset["train"].column_names )``` Since the collator expects a list of `dict`s, where each `dict` represents a single example in the dataset, we also need to wrangle the data into the expected format before passing it to the data collator: ``` features = [tokenized_datasets["train"][i] for i in range(2)] data_collator(features)``` ``` {'attention_mask': tensor([[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, 0, 0, 0, 0, 0, 0], [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]]), 'input_ids': tensor([[ 1494, 259, 8622, 390, 259, 262, 2316, 3435, 955, 772, 281, 772, 1617, 263, 305, 14701, 260, 1385, 3031, 259, 24146, 332, 1037, 259, 43906, 305, 336, 260, 1, 0, 0, 0, 0, 0, 0], [ 259, 27531, 13483, 259, 7505, 260, 112240, 15192, 305, 53198, 276, 259, 74060, 263, 260, 459, 25640, 776, 2119, 336, 259, 2220, 259, 18896, 288, 4906, 288, 1037, 3931, 260, 7083, 101476, 1143, 260, 1]]), 'labels': tensor([[ 7483, 259, 2364, 15695, 1, -100], [ 259, 27531, 13483, 259, 7505, 1]]), 'decoder_input_ids': tensor([[ 0, 7483, 259, 2364, 15695, 1], [ 0, 259, 27531, 13483, 259, 7505]])}``` The main thing to notice here is that the first example is longer than the second one, so the `input_ids` and `attention_mask` of the second example have been padded on the right with a `[PAD]` token (whose ID is `0`). Similarly, we can see that the `labels` have been padded with `-100`s, to make sure the padding tokens are ignored by the loss function. And finally, we can see a new `decoder_input_ids` which has shifted the labels to the right by inserting a `[PAD]` token in the first entry. We finally have all the ingredients we need to train with! We now simply need to instantiate the trainer with the standard arguments: ``` from transformers import Seq2SeqTrainer trainer = Seq2SeqTrainer( model, args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=data_collator, tokenizer=tokenizer, compute_metrics=compute_metrics, )``` and launch our training run: During training, you should see the training loss decrease and the ROUGE scores increase with each epoch. Once the training is complete, you can see the final ROUGE scores by running `Trainer.evaluate()`: ``` {'eval_loss': 3.028524398803711, 'eval_rouge1': 16.9728, 'eval_rouge2': 8.2969, 'eval_rougeL': 16.8366, 'eval_rougeLsum': 16.851, 'eval_gen_len': 10.1597, 'eval_runtime': 6.1054, 'eval_samples_per_second': 38.982, 'eval_steps_per_second': 4.914}``` From the scores we can see that our model has handily outperformed our lead-3 baseline — nice! The final thing to do is push the model weights to the Hub, as follows: ``` trainer.push_to_hub(commit_message="Training complete", tags="summarization")``` ``` 'https://huggingface.co/huggingface-course/mt5-finetuned-amazon-en-es/commit/aa0536b829b28e73e1e4b94b8a5aacec420d40e0'``` This will save the checkpoint and configuration files to `output_dir`, before uploading all the files to the Hub. By specifying the `tags` argument, we also ensure that the widget on the Hub will be one for a summarization pipeline instead of the default text generation one associated with the mT5 architecture (for more information about model tags, see the [🤗 Hub documentation](https://huggingface.co/docs/hub/main#how-is-a-models-type-of-inference-api-and-widget-determined)). The output from `trainer.push_to_hub()` is a URL to the Git commit hash, so you can easily see the changes that were made to the model repository! To wrap up this section, let’s take a look at how we can also fine-tune mT5 using the low-level features provided by 🤗 Accelerate. ## [](#fine-tuning-mt5-with-accelerate)Fine-tuning mT5 with 🤗 Accelerate Fine-tuning our model with 🤗 Accelerate is very similar to the text classification example we encountered in [Chapter 3](/course/chapter3). The main differences will be the need to explicitly generate our summaries during training and define how we compute the ROUGE scores (recall that the `Seq2SeqTrainer` took care of the generation for us). Let’s take a look how we can implement these two requirements within 🤗 Accelerate! ### [](#preparing-everything-for-training)Preparing everything for training The first thing we need to do is create a `DataLoader` for each of our splits. Since the PyTorch dataloaders expect batches of tensors, we need to set the format to `"torch"` in our datasets: ``` tokenized_datasets.set_format("torch")``` Now that we’ve got datasets consisting of just tensors, the next thing to do is instantiate the `DataCollatorForSeq2Seq` again. For this we need to provide a fresh version of the model, so let’s load it again from our cache: ``` model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)``` We can then instantiate the data collator and use this to define our dataloaders: ``` from torch.utils.data import DataLoader batch_size = 8 train_dataloader = DataLoader( tokenized_datasets["train"], shuffle=True, collate_fn=data_collator, batch_size=batch_size, ) eval_dataloader = DataLoader( tokenized_datasets["validation"], collate_fn=data_collator, batch_size=batch_size )``` The next thing to do is define the optimizer we want to use. As in our other examples, we’ll use `AdamW`, which works well for most problems: ``` from torch.optim import AdamW optimizer = AdamW(model.parameters(), lr=2e-5)``` Finally, we feed our model, optimizer, and dataloaders to the `accelerator.prepare()` method: ``` from accelerate import Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )``` 🚨 If you’re training on a TPU, you’ll need to move all the code above into a dedicated training function. See [Chapter 3](/course/chapter3) for more details. Now that we’ve prepared our objects, there are three remaining things to do: - Define the learning rate schedule. - Implement a function to post-process the summaries for evaluation. - Create a repository on the Hub that we can push our model to. For the learning rate schedule, we’ll use the standard linear one from previous sections: ``` from transformers import get_scheduler num_train_epochs = 10 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, )``` For post-processing, we need a function that splits the generated summaries into sentences that are separated by newlines. This is the format the ROUGE metric expects, and we can achieve this with the following snippet of code: ``` def postprocess_text(preds, labels): preds = [pred.strip() for pred in preds] labels = [label.strip() for label in labels] preds = ["\n".join(nltk.sent_tokenize(pred)) for pred in preds] labels = ["\n".join(nltk.sent_tokenize(label)) for label in labels] return preds, labels``` This should look familiar to you if you recall how we defined the `compute_metrics()` function of the `Seq2SeqTrainer`. Finally, we need to create a model repository on the Hugging Face Hub. For this, we can use the appropriately titled 🤗 Hub library. We just need to define a name for our repository, and the library has a utility function to combine the repository ID with the user profile: ``` from huggingface_hub import get_full_repo_name model_name = "test-bert-finetuned-squad-accelerate" repo_name = get_full_repo_name(model_name) repo_name``` ``` 'lewtun/mt5-finetuned-amazon-en-es-accelerate'``` Now we can use this repository name to clone a local version to our results directory that will store the training artifacts: ``` from huggingface_hub import Repository output_dir = "results-mt5-finetuned-squad-accelerate" repo = Repository(output_dir, clone_from=repo_name)``` This will allow us to push the artifacts back to the Hub by calling the `repo.push_to_hub()` method during training! Let’s now wrap up our analysis by writing out the training loop. ### [](#training-loop)Training loop The training loop for summarization is quite similar to the other 🤗 Accelerate examples that we’ve encountered and is roughly split into four main steps: 1. Train the model by iterating over all the examples in `train_dataloader` for each epoch. 2. Generate model summaries at the end of each epoch, by first generating the tokens and then decoding them (and the reference summaries) into text. 3. Compute the ROUGE scores using the same techniques we saw earlier. 4. Save the checkpoints and push everything to the Hub. Here we rely on the nifty `blocking=False` argument of the `Repository` object so that we can push the checkpoints per epoch _asynchronously_. This allows us to continue training without having to wait for the somewhat slow upload associated with a GB-sized model! These steps can be seen in the following block of code: ``` from tqdm.auto import tqdm import torch import numpy as np progress_bar = tqdm(range(num_training_steps)) for epoch in range(num_train_epochs): model.train() for step, batch in enumerate(train_dataloader): outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) model.eval() for step, batch in enumerate(eval_dataloader): with torch.no_grad(): generated_tokens = accelerator.unwrap_model(model).generate( batch["input_ids"], attention_mask=batch["attention_mask"], ) generated_tokens = accelerator.pad_across_processes( generated_tokens, dim=1, pad_index=tokenizer.pad_token_id ) labels = batch["labels"] labels = accelerator.pad_across_processes( batch["labels"], dim=1, pad_index=tokenizer.pad_token_id ) generated_tokens = accelerator.gather(generated_tokens).cpu().numpy() labels = accelerator.gather(labels).cpu().numpy() labels = np.where(labels != -100, labels, tokenizer.pad_token_id) if isinstance(generated_tokens, tuple): generated_tokens = generated_tokens[0] decoded_preds = tokenizer.batch_decode( generated_tokens, skip_special_tokens=True ) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) decoded_preds, decoded_labels = postprocess_text( decoded_preds, decoded_labels ) rouge_score.add_batch(predictions=decoded_preds, references=decoded_labels) result = rouge_score.compute() result = {key: value.mid.fmeasure * 100 for key, value in result.items()} result = {k: round(v, 4) for k, v in result.items()} print(f"Epoch {epoch}:", result) accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=f"Training in progress epoch {epoch}", blocking=False )``` ``` Epoch 0: {'rouge1': 5.6351, 'rouge2': 1.1625, 'rougeL': 5.4866, 'rougeLsum': 5.5005} Epoch 1: {'rouge1': 9.8646, 'rouge2': 3.4106, 'rougeL': 9.9439, 'rougeLsum': 9.9306} Epoch 2: {'rouge1': 11.0872, 'rouge2': 3.3273, 'rougeL': 11.0508, 'rougeLsum': 10.9468} Epoch 3: {'rouge1': 11.8587, 'rouge2': 4.8167, 'rougeL': 11.7986, 'rougeLsum': 11.7518} Epoch 4: {'rouge1': 12.9842, 'rouge2': 5.5887, 'rougeL': 12.7546, 'rougeLsum': 12.7029} Epoch 5: {'rouge1': 13.4628, 'rouge2': 6.4598, 'rougeL': 13.312, 'rougeLsum': 13.2913} Epoch 6: {'rouge1': 12.9131, 'rouge2': 5.8914, 'rougeL': 12.6896, 'rougeLsum': 12.5701} Epoch 7: {'rouge1': 13.3079, 'rouge2': 6.2994, 'rougeL': 13.1536, 'rougeLsum': 13.1194} Epoch 8: {'rouge1': 13.96, 'rouge2': 6.5998, 'rougeL': 13.9123, 'rougeLsum': 13.7744} Epoch 9: {'rouge1': 14.1192, 'rouge2': 7.0059, 'rougeL': 14.1172, 'rougeLsum': 13.9509}``` And that’s it! Once you run this, you’ll have a model and results that are pretty similar to the ones we obtained with the `Trainer`. ## [](#using-your-fine-tuned-model)Using your fine-tuned model Once you’ve pushed the model to the Hub, you can play with it either via the inference widget or with a `pipeline` object, as follows: ``` from transformers import pipeline hub_model_id = "huggingface-course/mt5-small-finetuned-amazon-en-es" summarizer = pipeline("summarization", model=hub_model_id)``` We can feed some examples from the test set (which the model has not seen) to our pipeline to get a feel for the quality of the summaries. First let’s implement a simple function to show the review, title, and generated summary together: ``` def print_summary(idx): review = books_dataset["test"][idx]["review_body"] title = books_dataset["test"][idx]["review_title"] summary = summarizer(books_dataset["test"][idx]["review_body"])[0]["summary_text"] print(f"'>>> Review: {review}'") print(f"\n'>>> Title: {title}'") print(f"\n'>>> Summary: {summary}'")``` Let’s take a look at one of the English examples we get: ``` '>>> Review: Nothing special at all about this product... the book is too small and stiff and hard to write in. The huge sticker on the back doesn’t come off and looks super tacky. I would not purchase this again. I could have just bought a journal from the dollar store and it would be basically the same thing. It’s also really expensive for what it is.' '>>> Title: Not impressed at all... buy something else' '>>> Summary: Nothing special at all about this product'``` This is not too bad! We can see that our model has actually been able to perform _abstractive_ summarization by augmenting parts of the review with new words. And perhaps the coolest aspect of our model is that it is bilingual, so we can also generate summaries of Spanish reviews: ``` '>>> Review: Es una trilogia que se hace muy facil de leer. Me ha gustado, no me esperaba el final para nada' '>>> Title: Buena literatura para adolescentes' '>>> Summary: Muy facil de leer'``` The summary translates into “Very easy to read” in English, which we can see in this case was extracted directly from the review. Nevertheless, this shows the versatility of the mT5 model and has given you a taste of what it’s like to deal with a multilingual corpus! Next, we’ll turn our attention to a slightly more complex task: training a language model from scratch.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Summarization - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter7/5"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Summarization - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;summarization&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;preparing-a-multilingual-corpus&quot;,&quot;title&quot;:&quot;Preparing a multilingual corpus&quot;},{&quot;local&quot;:&quot;models-for-text-summarization&quot;,&quot;title&quot;:&quot;Models for text summarization&quot;},{&quot;local&quot;:&quot;preprocessing-the-data&quot;,&quot;title&quot;:&quot;Preprocessing the data&quot;},{&quot;local&quot;:&quot;metrics-for-text-summarization&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;creating-a-strong-baseline&quot;,&quot;title&quot;:&quot;Creating a strong baseline&quot;}],&quot;title&quot;:&quot;Metrics for text summarization&quot;},{&quot;local&quot;:&quot;fine-tuning-mt5-with-the-trainer-api&quot;,&quot;title&quot;:&quot;Fine-tuning mT5 with the `Trainer` API&quot;},{&quot;local&quot;:&quot;fine-tuning-mt5-with-keras&quot;,&quot;title&quot;:&quot;Fine-tuning mT5 with Keras&quot;},{&quot;local&quot;:&quot;fine-tuning-mt5-with-accelerate&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;preparing-everything-for-training&quot;,&quot;title&quot;:&quot;Preparing everything for training&quot;},{&quot;local&quot;:&quot;training-loop&quot;,&quot;title&quot;:&quot;Training loop&quot;}],&quot;title&quot;:&quot;Fine-tuning mT5 with 🤗 Accelerate&quot;},{&quot;local&quot;:&quot;using-your-fine-tuned-model&quot;,&quot;title&quot;:&quot;Using your fine-tuned model&quot;}],&quot;title&quot;:&quot;Summarization&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter7/5&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Summarization&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Summarization</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/2?fw=pt">Token classification </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/3?fw=pt">Fine-tuning a masked language model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/4?fw=pt">Translation </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter7/5?fw=pt">Summarization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/6?fw=pt">Training a causal language model from scratch </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/7?fw=pt">Question answering </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/8?fw=pt">Mastering NLP </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="summarization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#summarization"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Summarization</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-7-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section5_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section5_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>In this section we’ll take a look at how Transformer models can be used to condense long documents into summaries, a task known as <em>text summarization</em>. This is one of the most challenging NLP tasks as it requires a range of abilities, such as understanding long passages and generating coherent text that captures the main topics in a document. However, when done well, text summarization is a powerful tool that can speed up various business processes by relieving the burden of domain experts to read long documents in detail.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/yHnr5Dk2zCI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Although there already exist various fine-tuned models for summarization on the <a href="https://huggingface.co/models?pipeline_tag=summarization&amp;sort=downloads" rel="nofollow">Hugging Face Hub</a>, almost all of these are only suitable for English documents. So, to add a twist in this section, we’ll train a bilingual model for English and Spanish. By the end of this section, you’ll have a <a href="https://huggingface.co/huggingface-course/mt5-small-finetuned-amazon-en-es" rel="nofollow">model</a> that can summarize customer reviews like the one shown here:</p> <iframe src="https://course-demos-mt5-small-finetuned-amazon-en-es.hf.space" frameborder="0" height="400" title="Gradio app" class="block dark:hidden container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>As we’ll see, these summaries are concise because they’re learned from the titles that customers provide in their product reviews. Let’s start by putting together a suitable bilingual corpus for this task.</p> <h2 class="relative group"><a id="preparing-a-multilingual-corpus" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preparing-a-multilingual-corpus"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preparing a multilingual corpus</span></h2> <p>We’ll use the <a href="https://huggingface.co/datasets/amazon_reviews_multi" rel="nofollow">Multilingual Amazon Reviews Corpus</a> to create our bilingual summarizer. This corpus consists of Amazon product reviews in six languages and is typically used to benchmark multilingual classifiers. However, since each review is accompanied by a short title, we can use the titles as the target summaries for our model to learn from! To get started, let’s download the English and Spanish subsets from the Hugging Face Hub:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset spanish_dataset = load_dataset(<span class="hljs-string">"amazon_reviews_multi"</span>, <span class="hljs-string">"es"</span>) english_dataset = load_dataset(<span class="hljs-string">"amazon_reviews_multi"</span>, <span class="hljs-string">"en"</span>) english_dataset</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'review_id'</span>, <span class="hljs-string">'product_id'</span>, <span class="hljs-string">'reviewer_id'</span>, <span class="hljs-string">'stars'</span>, <span class="hljs-string">'review_body'</span>, <span class="hljs-string">'review_title'</span>, <span class="hljs-string">'language'</span>, <span class="hljs-string">'product_category'</span>], num_rows: <span class="hljs-number">200000</span> }) validation: Dataset({ features: [<span class="hljs-string">'review_id'</span>, <span class="hljs-string">'product_id'</span>, <span class="hljs-string">'reviewer_id'</span>, <span class="hljs-string">'stars'</span>, <span class="hljs-string">'review_body'</span>, <span class="hljs-string">'review_title'</span>, <span class="hljs-string">'language'</span>, <span class="hljs-string">'product_category'</span>], num_rows: <span class="hljs-number">5000</span> }) test: Dataset({ features: [<span class="hljs-string">'review_id'</span>, <span class="hljs-string">'product_id'</span>, <span class="hljs-string">'reviewer_id'</span>, <span class="hljs-string">'stars'</span>, <span class="hljs-string">'review_body'</span>, <span class="hljs-string">'review_title'</span>, <span class="hljs-string">'language'</span>, <span class="hljs-string">'product_category'</span>], num_rows: <span class="hljs-number">5000</span> }) })</pre></div> <p>As you can see, for each language there are 200,000 reviews for the <code>train</code> split, and 5,000 reviews for each of the <code>validation</code> and <code>test</code> splits. The review information we are interested in is contained in the <code>review_body</code> and <code>review_title</code> columns. Let’s take a look at a few examples by creating a simple function that takes a random sample from the training set with the techniques we learned in <a href="/course/chapter5">Chapter 5</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">show_samples</span>(<span class="hljs-params">dataset, num_samples=<span class="hljs-number">3</span>, seed=<span class="hljs-number">42</span></span>): sample = dataset[<span class="hljs-string">"train"</span>].shuffle(seed=seed).select(<span class="hljs-built_in">range</span>(num_samples)) <span class="hljs-keyword">for</span> example <span class="hljs-keyword">in</span> sample: <span class="hljs-built_in">print</span>(<span class="hljs-string">f"\n'&gt;&gt; Title: <span class="hljs-subst">{example[<span class="hljs-string">'review_title'</span>]}</span>'"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"'&gt;&gt; Review: <span class="hljs-subst">{example[<span class="hljs-string">'review_body'</span>]}</span>'"</span>) show_samples(english_dataset)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt; Title: Worked in front position, not rear'</span> <span class="hljs-string">'&gt;&gt; Review: 3 stars because these are not rear brakes as stated in the item description. At least the mount adapter only worked on the front fork of the bike that I got it for.'</span> <span class="hljs-string">'&gt;&gt; Title: meh'</span> <span class="hljs-string">'&gt;&gt; Review: Does it’s job and it’s gorgeous but mine is falling apart, I had to basically put it together again with hot glue'</span> <span class="hljs-string">'&gt;&gt; Title: Can\'t beat these for the money'</span> <span class="hljs-string">'&gt;&gt; Review: Bought this for handling miscellaneous aircraft parts and hanger "stuff" that I needed to organize; it really fit the bill. The unit arrived quickly, was well packaged and arrived intact (always a good sign). There are five wall mounts-- three on the top and two on the bottom. I wanted to mount it on the wall, so all I had to do was to remove the top two layers of plastic drawers, as well as the bottom corner drawers, place it when I wanted and mark it; I then used some of the new plastic screw in wall anchors (the 50 pound variety) and it easily mounted to the wall. Some have remarked that they wanted dividers for the drawers, and that they made those. Good idea. My application was that I needed something that I can see the contents at about eye level, so I wanted the fuller-sized drawers. I also like that these are the new plastic that doesn\'t get brittle and split like my older plastic drawers did. I like the all-plastic construction. It\'s heavy duty enough to hold metal parts, but being made of plastic it\'s not as heavy as a metal frame, so you can easily mount it to the wall and still load it up with heavy stuff, or light stuff. No problem there. For the money, you can\'t beat it. Best one of these I\'ve bought to date-- and I\'ve been using some version of these for over forty years.'</span></pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Change the random seed in the <code>Dataset.shuffle()</code> command to explore other reviews in the corpus. If you’re a Spanish speaker, take a look at some of the reviews in <code>spanish_dataset</code> to see if the titles also seem like reasonable summaries.</p></div> <p>This sample shows the diversity of reviews one typically finds online, ranging from positive to negative (and everything in between!). Although the example with the “meh” title is not very informative, the other titles look like decent summaries of the reviews themselves. Training a summarization model on all 400,000 reviews would take far too long on a single GPU, so instead we’ll focus on generating summaries for a single domain of products. To get a feel for what domains we can choose from, let’s convert <code>english_dataset</code> to a <code>pandas.DataFrame</code> and compute the number of reviews per product category:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>english_dataset.set_format(<span class="hljs-string">"pandas"</span>) english_df = english_dataset[<span class="hljs-string">"train"</span>][:] <span class="hljs-comment"># Show counts for top 20 products</span> english_df[<span class="hljs-string">"product_category"</span>].value_counts()[:<span class="hljs-number">20</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>home <span class="hljs-number">17679</span> apparel <span class="hljs-number">15951</span> wireless <span class="hljs-number">15717</span> other <span class="hljs-number">13418</span> beauty <span class="hljs-number">12091</span> drugstore <span class="hljs-number">11730</span> kitchen <span class="hljs-number">10382</span> toy <span class="hljs-number">8745</span> sports <span class="hljs-number">8277</span> automotive <span class="hljs-number">7506</span> lawn_and_garden <span class="hljs-number">7327</span> home_improvement <span class="hljs-number">7136</span> pet_products <span class="hljs-number">7082</span> digital_ebook_purchase <span class="hljs-number">6749</span> pc <span class="hljs-number">6401</span> electronics <span class="hljs-number">6186</span> office_product <span class="hljs-number">5521</span> shoes <span class="hljs-number">5197</span> grocery <span class="hljs-number">4730</span> book <span class="hljs-number">3756</span> Name: product_category, dtype: int64</pre></div> <p>The most popular products in the English dataset are about household items, clothing, and wireless electronics. To stick with the Amazon theme, though, let’s focus on summarizing book reviews — after all, this is what the company was founded on! We can see two product categories that fit the bill (<code>book</code> and <code>digital_ebook_purchase</code>), so let’s filter the datasets in both languages for just these products. As we saw in <a href="/course/chapter5">Chapter 5</a>, the <code>Dataset.filter()</code> function allows us to slice a dataset very efficiently, so we can define a simple function to do this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">filter_books</span>(<span class="hljs-params">example</span>): <span class="hljs-keyword">return</span> ( example[<span class="hljs-string">"product_category"</span>] == <span class="hljs-string">"book"</span> <span class="hljs-keyword">or</span> example[<span class="hljs-string">"product_category"</span>] == <span class="hljs-string">"digital_ebook_purchase"</span> )</pre></div> <p>Now when we apply this function to <code>english_dataset</code> and <code>spanish_dataset</code>, the result will contain just those rows involving the book categories. Before applying the filter, let’s switch the format of <code>english_dataset</code> from <code>"pandas"</code> back to <code>"arrow"</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>english_dataset.reset_format()</pre></div> <p>We can then apply the filter function, and as a sanity check let’s inspect a sample of reviews to see if they are indeed about books:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>spanish_books = spanish_dataset.<span class="hljs-built_in">filter</span>(filter_books) english_books = english_dataset.<span class="hljs-built_in">filter</span>(filter_books) show_samples(english_books)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt; Title: I\'m dissapointed.'</span> <span class="hljs-string">'&gt;&gt; Review: I guess I had higher expectations for this book from the reviews. I really thought I\'d at least like it. The plot idea was great. I loved Ash but, it just didnt go anywhere. Most of the book was about their radio show and talking to callers. I wanted the author to dig deeper so we could really get to know the characters. All we know about Grace is that she is attractive looking, Latino and is kind of a brat. I\'m dissapointed.'</span> <span class="hljs-string">'&gt;&gt; Title: Good art, good price, poor design'</span> <span class="hljs-string">'&gt;&gt; Review: I had gotten the DC Vintage calendar the past two years, but it was on backorder forever this year and I saw they had shrunk the dimensions for no good reason. This one has good art choices but the design has the fold going through the picture, so it\'s less aesthetically pleasing, especially if you want to keep a picture to hang. For the price, a good calendar'</span> <span class="hljs-string">'&gt;&gt; Title: Helpful'</span> <span class="hljs-string">'&gt;&gt; Review: Nearly all the tips useful and. I consider myself an intermediate to advanced user of OneNote. I would highly recommend.'</span></pre></div> <p>Okay, we can see that the reviews are not strictly about books and might refer to things like calendars and electronic applications such as OneNote. Nevertheless, the domain seems about right to train a summarization model on. Before we look at various models that are suitable for this task, we have one last bit of data preparation to do: combining the English and Spanish reviews as a single <code>DatasetDict</code> object. 🤗 Datasets provides a handy <code>concatenate_datasets()</code> function that (as the name suggests) will stack two <code>Dataset</code> objects on top of each other. So, to create our bilingual dataset, we’ll loop over each split, concatenate the datasets for that split, and shuffle the result to ensure our model doesn’t overfit to a single language:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> concatenate_datasets, DatasetDict books_dataset = DatasetDict() <span class="hljs-keyword">for</span> split <span class="hljs-keyword">in</span> english_books.keys(): books_dataset[split] = concatenate_datasets( [english_books[split], spanish_books[split]] ) books_dataset[split] = books_dataset[split].shuffle(seed=<span class="hljs-number">42</span>) <span class="hljs-comment"># Peek at a few examples</span> show_samples(books_dataset)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt; Title: Easy to follow!!!!'</span> <span class="hljs-string">'&gt;&gt; Review: I loved The dash diet weight loss Solution. Never hungry. I would recommend this diet. Also the menus are well rounded. Try it. Has lots of the information need thanks.'</span> <span class="hljs-string">'&gt;&gt; Title: PARCIALMENTE DAÑADO'</span> <span class="hljs-string">'&gt;&gt; Review: Me llegó el día que tocaba, junto a otros libros que pedí, pero la caja llegó en mal estado lo cual dañó las esquinas de los libros porque venían sin protección (forro).'</span> <span class="hljs-string">'&gt;&gt; Title: no lo he podido descargar'</span> <span class="hljs-string">'&gt;&gt; Review: igual que el anterior'</span></pre></div> <p>This certainly looks like a mix of English and Spanish reviews! Now that we have a training corpus, one final thing to check is the distribution of words in the reviews and their titles. This is especially important for summarization tasks, where short reference summaries in the data can bias the model to only output one or two words in the generated summaries. The plots below show the word distributions, and we can see that the titles are heavily skewed toward just 1-2 words:</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/review-lengths.svg" alt="Word count distributions for the review titles and texts."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/review-lengths-dark.svg" alt="Word count distributions for the review titles and texts."></div> <p>To deal with this, we’ll filter out the examples with very short titles so that our model can produce more interesting summaries. Since we’re dealing with English and Spanish texts, we can use a rough heuristic to split the titles on whitespace and then use our trusty <code>Dataset.filter()</code> method as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>books_dataset = books_dataset.<span class="hljs-built_in">filter</span>(<span class="hljs-keyword">lambda</span> x: <span class="hljs-built_in">len</span>(x[<span class="hljs-string">"review_title"</span>].split()) &gt; <span class="hljs-number">2</span>)</pre></div> <p>Now that we’ve prepared our corpus, let’s take a look at a few possible Transformer models that one might fine-tune on it!</p> <h2 class="relative group"><a id="models-for-text-summarization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#models-for-text-summarization"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Models for text summarization</span></h2> <p>If you think about it, text summarization is a similar sort of task to machine translation: we have a body of text like a review that we’d like to “translate” into a shorter version that captures the salient features of the input. Accordingly, most Transformer models for summarization adopt the encoder-decoder architecture that we first encountered in <a href="/course/chapter1">Chapter 1</a>, although there are some exceptions like the GPT family of models which can also be used for summarization in few-shot settings. The following table lists some popular pretrained models that can be fine-tuned for summarization.</p> <table><thead><tr><th align="center">Transformer model</th> <th>Description</th> <th align="center">Multilingual?</th></tr></thead> <tbody><tr><td align="center"><a href="https://huggingface.co/gpt2-xl" rel="nofollow">GPT-2</a></td> <td>Although trained as an auto-regressive language model, you can make GPT-2 generate summaries by appending “TL;DR” at the end of the input text.</td> <td align="center">❌</td></tr> <tr><td align="center"><a href="https://huggingface.co/google/pegasus-large" rel="nofollow">PEGASUS</a></td> <td>Uses a pretraining objective to predict masked sentences in multi-sentence texts. This pretraining objective is closer to summarization than vanilla language modeling and scores highly on popular benchmarks.</td> <td align="center">❌</td></tr> <tr><td align="center"><a href="https://huggingface.co/t5-base" rel="nofollow">T5</a></td> <td>A universal Transformer architecture that formulates all tasks in a text-to-text framework; e.g., the input format for the model to summarize a document is <code>summarize: ARTICLE</code>.</td> <td align="center">❌</td></tr> <tr><td align="center"><a href="https://huggingface.co/google/mt5-base" rel="nofollow">mT5</a></td> <td>A multilingual version of T5, pretrained on the multilingual Common Crawl corpus (mC4), covering 101 languages.</td> <td align="center">✅</td></tr> <tr><td align="center"><a href="https://huggingface.co/facebook/bart-base" rel="nofollow">BART</a></td> <td>A novel Transformer architecture with both an encoder and a decoder stack trained to reconstruct corrupted input that combines the pretraining schemes of BERT and GPT-2.</td> <td align="center">❌</td></tr> <tr><td align="center"><a href="https://huggingface.co/facebook/mbart-large-50" rel="nofollow">mBART-50</a></td> <td>A multilingual version of BART, pretrained on 50 languages.</td> <td align="center">✅</td></tr></tbody></table> <p>As you can see from this table, the majority of Transformer models for summarization (and indeed most NLP tasks) are monolingual. This is great if your task is in a “high-resource” language like English or German, but less so for the thousands of other languages in use across the world. Fortunately, there is a class of multilingual Transformer models, like mT5 and mBART, that come to the rescue. These models are pretrained using language modeling, but with a twist: instead of training on a corpus of one language, they are trained jointly on texts in over 50 languages at once!</p> <p>We’ll focus on mT5, an interesting architecture based on T5 that was pretrained in a text-to-text framework. In T5, every NLP task is formulated in terms of a prompt prefix like <code>summarize:</code> which conditions the model to adapt the generated text to the prompt. As shown in the figure below, this makes T5 extremely versatile, as you can solve many tasks with a single model!</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/t5.svg" alt="Different tasks performed by the T5 architecture."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/t5-dark.svg" alt="Different tasks performed by the T5 architecture."></div> <p>mT5 doesn’t use prefixes, but shares much of the versatility of T5 and has the advantage of being multilingual. Now that we’ve picked a model, let’s take a look at preparing our data for training.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Once you’ve worked through this section, see how well mT5 compares to mBART by fine-tuning the latter with the same techniques. For bonus points, you can also try fine-tuning T5 on just the English reviews. Since T5 has a special prefix prompt, you’ll need to prepend <code>summarize:</code> to the input examples in the preprocessing steps below.</p></div> <h2 class="relative group"><a id="preprocessing-the-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preprocessing-the-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preprocessing the data</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/1m7BerpSq8A" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Our next task is to tokenize and encode our reviews and their titles. As usual, we begin by loading the tokenizer associated with the pretrained model checkpoint. We’ll use <code>mt5-small</code> as our checkpoint so we can fine-tune the model in a reasonable amount of time:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer model_checkpoint = <span class="hljs-string">"google/mt5-small"</span> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 In the early stages of your NLP projects, a good practice is to train a class of “small” models on a small sample of data. This allows you to debug and iterate faster toward an end-to-end workflow. Once you are confident in the results, you can always scale up the model by simply changing the model checkpoint!</p></div> <p>Let’s test out the mT5 tokenizer on a small example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs = tokenizer(<span class="hljs-string">"I loved reading the Hunger Games!"</span>) inputs</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'input_ids'</span>: [<span class="hljs-number">336</span>, <span class="hljs-number">259</span>, <span class="hljs-number">28387</span>, <span class="hljs-number">11807</span>, <span class="hljs-number">287</span>, <span class="hljs-number">62893</span>, <span class="hljs-number">295</span>, <span class="hljs-number">12507</span>, <span class="hljs-number">1</span>], <span class="hljs-string">'attention_mask'</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]}</pre></div> <p>Here we can see the familiar <code>input_ids</code> and <code>attention_mask</code> that we encountered in our first fine-tuning experiments back in <a href="/course/chapter3">Chapter 3</a>. Let’s decode these input IDs with the tokenizer’s <code>convert_ids_to_tokens()</code> function to see what kind of tokenizer we’re dealing with:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.convert_ids_to_tokens(inputs.input_ids)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'▁I'</span>, <span class="hljs-string">'▁'</span>, <span class="hljs-string">'loved'</span>, <span class="hljs-string">'▁reading'</span>, <span class="hljs-string">'▁the'</span>, <span class="hljs-string">'▁Hung'</span>, <span class="hljs-string">'er'</span>, <span class="hljs-string">'▁Games'</span>, <span class="hljs-string">'&lt;/s&gt;'</span>]</pre></div> <p>The special Unicode character <code>▁</code> and end-of-sequence token <code>&lt;/s&gt;</code> indicate that we’re dealing with the SentencePiece tokenizer, which is based on the Unigram segmentation algorithm discussed in <a href="/course/chapter6">Chapter 6</a>. Unigram is especially useful for multilingual corpora since it allows SentencePiece to be agnostic about accents, punctuation, and the fact that many languages, like Japanese, do not have whitespace characters.</p> <p>To tokenize our corpus, we have to deal with a subtlety associated with summarization: because our labels are also text, it is possible that they exceed the model’s maximum context size. This means we need to apply truncation to both the reviews and their titles to ensure we don’t pass excessively long inputs to our model. The tokenizers in 🤗 Transformers provide a nifty <code>text_target</code> argument that allows you to tokenize the labels in parallel to the inputs. Here is an example of how the inputs and targets are processed for mT5:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>max_input_length = <span class="hljs-number">512</span> max_target_length = <span class="hljs-number">30</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_function</span>(<span class="hljs-params">examples</span>): model_inputs = tokenizer( examples[<span class="hljs-string">"review_body"</span>], max_length=max_input_length, truncation=<span class="hljs-literal">True</span>, ) labels = tokenizer( examples[<span class="hljs-string">"review_title"</span>], max_length=max_target_length, truncation=<span class="hljs-literal">True</span> ) model_inputs[<span class="hljs-string">"labels"</span>] = labels[<span class="hljs-string">"input_ids"</span>] <span class="hljs-keyword">return</span> model_inputs</pre></div> <p>Let’s walk through this code to understand what’s happening. The first thing we’ve done is define values for <code>max_input_length</code> and <code>max_target_length</code>, which set the upper limits for how long our reviews and titles can be. Since the review body is typically much larger than the title, we’ve scaled these values accordingly.</p> <p>With <code>preprocess_function()</code>, it is then a simple matter to tokenize the whole corpus using the handy <code>Dataset.map()</code> function we’ve used extensively throughout this course:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_datasets = books_dataset.<span class="hljs-built_in">map</span>(preprocess_function, batched=<span class="hljs-literal">True</span>)</pre></div> <p>Now that the corpus has been preprocessed, let’s take a look at some metrics that are commonly used for summarization. As we’ll see, there is no silver bullet when it comes to measuring the quality of machine-generated text.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 You may have noticed that we used <code>batched=True</code> in our <code>Dataset.map()</code> function above. This encodes the examples in batches of 1,000 (the default) and allows you to make use of the multithreading capabilities of the fast tokenizers in 🤗 Transformers. Where possible, try using <code>batched=True</code> to get the most out of your preprocessing!</p></div> <h2 class="relative group"><a id="metrics-for-text-summarization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#metrics-for-text-summarization"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Metrics for text summarization</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/TMshhnrEXlg" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>In comparison to most of the other tasks we’ve covered in this course, measuring the performance of text generation tasks like summarization or translation is not as straightforward. For example, given a review like “I loved reading the Hunger Games”, there are multiple valid summaries, like “I loved the Hunger Games” or “Hunger Games is a great read”. Clearly, applying some sort of exact match between the generated summary and the label is not a good solution — even humans would fare poorly under such a metric, because we all have our own writing style.</p> <p>For summarization, one of the most commonly used metrics is the <a href="https://en.wikipedia.org/wiki/ROUGE_(metric)" rel="nofollow">ROUGE score</a> (short for Recall-Oriented Understudy for Gisting Evaluation). The basic idea behind this metric is to compare a generated summary against a set of reference summaries that are typically created by humans. To make this more precise, suppose we want to compare the following two summaries:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>generated_summary = <span class="hljs-string">"I absolutely loved reading the Hunger Games"</span> reference_summary = <span class="hljs-string">"I loved reading the Hunger Games"</span></pre></div> <p>One way to compare them could be to count the number of overlapping words, which in this case would be 6. However, this is a bit crude, so instead ROUGE is based on computing the <em>precision</em> and <em>recall</em> scores for the overlap.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>🙋 Don’t worry if this is the first time you’ve heard of precision and recall — we’ll go through some explicit examples together to make it all clear. These metrics are usually encountered in classification tasks, so if you want to understand how precision and recall are defined in that context, we recommend checking out the <code>scikit-learn</code> <a href="https://scikit-learn.org/stable/auto_examples/model_selection/plot_precision_recall.html" rel="nofollow">guides</a>.</p></div> <p>For ROUGE, recall measures how much of the reference summary is captured by the generated one. If we are just comparing words, recall can be calculated according to the following formula: <span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mrow><mi mathvariant="normal">R</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">c</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">l</mi><mi mathvariant="normal">l</mi></mrow><mo>=</mo><mfrac><mrow><mi mathvariant="normal">N</mi><mi mathvariant="normal">u</mi><mi mathvariant="normal">m</mi><mi mathvariant="normal">b</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">r</mi><mtext> </mtext><mi mathvariant="normal">o</mi><mi mathvariant="normal">f</mi><mtext> </mtext><mi mathvariant="normal">o</mi><mi mathvariant="normal">v</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">l</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">p</mi><mi mathvariant="normal">p</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">g</mi><mtext> </mtext><mi mathvariant="normal">w</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">s</mi></mrow><mrow><mi mathvariant="normal">T</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">l</mi><mtext> </mtext><mi mathvariant="normal">n</mi><mi mathvariant="normal">u</mi><mi mathvariant="normal">m</mi><mi mathvariant="normal">b</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">r</mi><mtext> </mtext><mi mathvariant="normal">o</mi><mi mathvariant="normal">f</mi><mtext> </mtext><mi mathvariant="normal">w</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">s</mi><mtext> </mtext><mi mathvariant="normal">i</mi><mi mathvariant="normal">n</mi><mtext> </mtext><mi mathvariant="normal">r</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">f</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">c</mi><mi mathvariant="normal">e</mi><mtext> </mtext><mi mathvariant="normal">s</mi><mi mathvariant="normal">u</mi><mi mathvariant="normal">m</mi><mi mathvariant="normal">m</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">y</mi></mrow></mfrac></mrow><annotation encoding="application/x-tex"> \mathrm{Recall} = \frac{\mathrm{Number\,of\,overlapping\, words}}{\mathrm{Total\, number\, of\, words\, in\, reference\, summary}} </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord"><span class="mord mathrm">Recall</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:2.2519em;vertical-align:-0.8804em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.3714em;"><span style="top:-2.314em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">Total</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm">number</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm" style="margin-right:0.07778em;">of</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm">words</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm">in</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm">reference</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm" style="margin-right:0.01389em;">summary</span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">Number</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm" style="margin-right:0.07778em;">of</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm" style="margin-right:0.01389em;">overlapping</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm">words</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.8804em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span></span></p> <p>For our simple example above, this formula gives a perfect recall of 6/6 = 1; i.e., all the words in the reference summary have been produced by the model. This may sound great, but imagine if our generated summary had been “I really really loved reading the Hunger Games all night”. This would also have perfect recall, but is arguably a worse summary since it is verbose. To deal with these scenarios we also compute the precision, which in the ROUGE context measures how much of the generated summary was relevant: <span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mrow><mi mathvariant="normal">P</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">c</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">s</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">n</mi></mrow><mo>=</mo><mfrac><mrow><mi mathvariant="normal">N</mi><mi mathvariant="normal">u</mi><mi mathvariant="normal">m</mi><mi mathvariant="normal">b</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">r</mi><mtext> </mtext><mi mathvariant="normal">o</mi><mi mathvariant="normal">f</mi><mtext> </mtext><mi mathvariant="normal">o</mi><mi mathvariant="normal">v</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">l</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">p</mi><mi mathvariant="normal">p</mi><mi mathvariant="normal">i</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">g</mi><mtext> </mtext><mi mathvariant="normal">w</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">s</mi></mrow><mrow><mi mathvariant="normal">T</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">l</mi><mtext> </mtext><mi mathvariant="normal">n</mi><mi mathvariant="normal">u</mi><mi mathvariant="normal">m</mi><mi mathvariant="normal">b</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">r</mi><mtext> </mtext><mi mathvariant="normal">o</mi><mi mathvariant="normal">f</mi><mtext> </mtext><mi mathvariant="normal">w</mi><mi mathvariant="normal">o</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">d</mi><mi mathvariant="normal">s</mi><mtext> </mtext><mi mathvariant="normal">i</mi><mi mathvariant="normal">n</mi><mtext> </mtext><mi mathvariant="normal">g</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">n</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">t</mi><mi mathvariant="normal">e</mi><mi mathvariant="normal">d</mi><mtext> </mtext><mi mathvariant="normal">s</mi><mi mathvariant="normal">u</mi><mi mathvariant="normal">m</mi><mi mathvariant="normal">m</mi><mi mathvariant="normal">a</mi><mi mathvariant="normal">r</mi><mi mathvariant="normal">y</mi></mrow></mfrac></mrow><annotation encoding="application/x-tex"> \mathrm{Precision} = \frac{\mathrm{Number\,of\,overlapping\, words}}{\mathrm{Total\, number\, of\, words\, in\, generated\, summary}} </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord"><span class="mord mathrm">Precision</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:2.2519em;vertical-align:-0.8804em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.3714em;"><span style="top:-2.314em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">Total</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm">number</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm" style="margin-right:0.07778em;">of</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm">words</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm">in</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm">generated</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm" style="margin-right:0.01389em;">summary</span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"><span class="mord mathrm">Number</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm" style="margin-right:0.07778em;">of</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm" style="margin-right:0.01389em;">overlapping</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathrm">words</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.8804em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span></span></p> <p>Applying this to our verbose summary gives a precision of 6/10 = 0.6, which is considerably worse than the precision of 6/7 = 0.86 obtained by our shorter one. In practice, both precision and recall are usually computed, and then the F1-score (the harmonic mean of precision and recall) is reported. We can do this easily in 🤗 Datasets by first installing the <code>rouge_score</code> package:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!pip install rouge_score</pre></div> <p>and then loading the ROUGE metric as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> evaluate rouge_score = evaluate.load(<span class="hljs-string">"rouge"</span>)</pre></div> <p>Then we can use the <code>rouge_score.compute()</code> function to calculate all the metrics at once:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>scores = rouge_score.compute( predictions=[generated_summary], references=[reference_summary] ) scores</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'rouge1'</span>: AggregateScore(low=Score(precision=<span class="hljs-number">0.86</span>, recall=<span class="hljs-number">1.0</span>, fmeasure=<span class="hljs-number">0.92</span>), mid=Score(precision=<span class="hljs-number">0.86</span>, recall=<span class="hljs-number">1.0</span>, fmeasure=<span class="hljs-number">0.92</span>), high=Score(precision=<span class="hljs-number">0.86</span>, recall=<span class="hljs-number">1.0</span>, fmeasure=<span class="hljs-number">0.92</span>)), <span class="hljs-string">'rouge2'</span>: AggregateScore(low=Score(precision=<span class="hljs-number">0.67</span>, recall=<span class="hljs-number">0.8</span>, fmeasure=<span class="hljs-number">0.73</span>), mid=Score(precision=<span class="hljs-number">0.67</span>, recall=<span class="hljs-number">0.8</span>, fmeasure=<span class="hljs-number">0.73</span>), high=Score(precision=<span class="hljs-number">0.67</span>, recall=<span class="hljs-number">0.8</span>, fmeasure=<span class="hljs-number">0.73</span>)), <span class="hljs-string">'rougeL'</span>: AggregateScore(low=Score(precision=<span class="hljs-number">0.86</span>, recall=<span class="hljs-number">1.0</span>, fmeasure=<span class="hljs-number">0.92</span>), mid=Score(precision=<span class="hljs-number">0.86</span>, recall=<span class="hljs-number">1.0</span>, fmeasure=<span class="hljs-number">0.92</span>), high=Score(precision=<span class="hljs-number">0.86</span>, recall=<span class="hljs-number">1.0</span>, fmeasure=<span class="hljs-number">0.92</span>)), <span class="hljs-string">'rougeLsum'</span>: AggregateScore(low=Score(precision=<span class="hljs-number">0.86</span>, recall=<span class="hljs-number">1.0</span>, fmeasure=<span class="hljs-number">0.92</span>), mid=Score(precision=<span class="hljs-number">0.86</span>, recall=<span class="hljs-number">1.0</span>, fmeasure=<span class="hljs-number">0.92</span>), high=Score(precision=<span class="hljs-number">0.86</span>, recall=<span class="hljs-number">1.0</span>, fmeasure=<span class="hljs-number">0.92</span>))}</pre></div> <p>Whoa, there’s a lot of information in that output — what does it all mean? First, 🤗 Datasets actually computes confidence intervals for precision, recall, and F1-score; these are the <code>low</code>, <code>mid</code>, and <code>high</code> attributes you can see here. Moreover, 🤗 Datasets computes a variety of ROUGE scores which are based on different types of text granularity when comparing the generated and reference summaries. The <code>rouge1</code> variant is the overlap of unigrams — this is just a fancy way of saying the overlap of words and is exactly the metric we’ve discussed above. To verify this, let’s pull out the <code>mid</code> value of our scores:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>scores[<span class="hljs-string">"rouge1"</span>].mid</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Score(precision=<span class="hljs-number">0.86</span>, recall=<span class="hljs-number">1.0</span>, fmeasure=<span class="hljs-number">0.92</span>)</pre></div> <p>Great, the precision and recall numbers match up! Now what about those other ROUGE scores? <code>rouge2</code> measures the overlap between bigrams (think the overlap of pairs of words), while <code>rougeL</code> and <code>rougeLsum</code> measure the longest matching sequences of words by looking for the longest common substrings in the generated and reference summaries. The “sum” in <code>rougeLsum</code> refers to the fact that this metric is computed over a whole summary, while <code>rougeL</code> is computed as the average over individual sentences.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Try it out!</strong> Create your own example of a generated and reference summary and see if the resulting ROUGE scores agree with a manual calculation based on the formulas for precision and recall. For bonus points, split the text into bigrams and compare the precision and recall for the <code>rouge2</code> metric.</p></div> <p>We’ll use these ROUGE scores to track the performance of our model, but before doing that let’s do something every good NLP practitioner should do: create a strong, yet simple baseline!</p> <h3 class="relative group"><a id="creating-a-strong-baseline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#creating-a-strong-baseline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Creating a strong baseline</span></h3> <p>A common baseline for text summarization is to simply take the first three sentences of an article, often called the <em>lead-3</em> baseline. We could use full stops to track the sentence boundaries, but this will fail on acronyms like “U.S.” or “U.N.” — so instead we’ll use the <code>nltk</code> library, which includes a better algorithm to handle these cases. You can install the package using <code>pip</code> as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!pip install nltk</pre></div> <p>and then download the punctuation rules:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> nltk nltk.download(<span class="hljs-string">"punkt"</span>)</pre></div> <p>Next, we import the sentence tokenizer from <code>nltk</code> and create a simple function to extract the first three sentences in a review. The convention in text summarization is to separate each summary with a newline, so let’s also include this and test it on a training example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> nltk.tokenize <span class="hljs-keyword">import</span> sent_tokenize <span class="hljs-keyword">def</span> <span class="hljs-title function_">three_sentence_summary</span>(<span class="hljs-params">text</span>): <span class="hljs-keyword">return</span> <span class="hljs-string">"\n"</span>.join(sent_tokenize(text)[:<span class="hljs-number">3</span>]) <span class="hljs-built_in">print</span>(three_sentence_summary(books_dataset[<span class="hljs-string">"train"</span>][<span class="hljs-number">1</span>][<span class="hljs-string">"review_body"</span>]))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'I grew up reading Koontz, and years ago, I stopped,convinced i had "outgrown" him.'</span> <span class="hljs-string">'Still,when a friend was looking for something suspenseful too read, I suggested Koontz.'</span> <span class="hljs-string">'She found Strangers.'</span></pre></div> <p>This seems to work, so let’s now implement a function that extracts these “summaries” from a dataset and computes the ROUGE scores for the baseline:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">evaluate_baseline</span>(<span class="hljs-params">dataset, metric</span>): summaries = [three_sentence_summary(text) <span class="hljs-keyword">for</span> text <span class="hljs-keyword">in</span> dataset[<span class="hljs-string">"review_body"</span>]] <span class="hljs-keyword">return</span> metric.compute(predictions=summaries, references=dataset[<span class="hljs-string">"review_title"</span>])</pre></div> <p>We can then use this function to compute the ROUGE scores over the validation set and prettify them a bit using Pandas:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd score = evaluate_baseline(books_dataset[<span class="hljs-string">"validation"</span>], rouge_score) rouge_names = [<span class="hljs-string">"rouge1"</span>, <span class="hljs-string">"rouge2"</span>, <span class="hljs-string">"rougeL"</span>, <span class="hljs-string">"rougeLsum"</span>] rouge_dict = <span class="hljs-built_in">dict</span>((rn, <span class="hljs-built_in">round</span>(score[rn].mid.fmeasure * <span class="hljs-number">100</span>, <span class="hljs-number">2</span>)) <span class="hljs-keyword">for</span> rn <span class="hljs-keyword">in</span> rouge_names) rouge_dict</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'rouge1'</span>: <span class="hljs-number">16.74</span>, <span class="hljs-string">'rouge2'</span>: <span class="hljs-number">8.83</span>, <span class="hljs-string">'rougeL'</span>: <span class="hljs-number">15.6</span>, <span class="hljs-string">'rougeLsum'</span>: <span class="hljs-number">15.96</span>}</pre></div> <p>We can see that the <code>rouge2</code> score is significantly lower than the rest; this likely reflects the fact that review titles are typically concise and so the lead-3 baseline is too verbose. Now that we have a good baseline to work from, let’s turn our attention toward fine-tuning mT5!</p> <h2 class="relative group"><a id="fine-tuning-mt5-with-the-trainer-api" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-mt5-with-the-trainer-api"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning mT5 with the <code>Trainer</code> API</span></h2> <p>Fine-tuning a model for summarization is very similar to the other tasks we’ve covered in this chapter. The first thing we need to do is load the pretrained model from the <code>mt5-small</code> checkpoint. Since summarization is a sequence-to-sequence task, we can load the model with the <code>AutoModelForSeq2SeqLM</code> class, which will automatically download and cache the weights:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSeq2SeqLM model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 If you’re wondering why you don’t see any warnings about fine-tuning the model on a downstream task, that’s because for sequence-to-sequence tasks we keep all the weights of the network. Compare this to our text classification model in <a href="/course/chapter3">Chapter 3</a>, where the head of the pretrained model was replaced with a randomly initialized network.</p></div> <p>The next thing we need to do is log in to the Hugging Face Hub. If you’re running this code in a notebook, you can do so with the following utility function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login notebook_login()</pre></div> <p>which will display a widget where you can enter your credentials. Alternatively, you can run this command in your terminal and log in there:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-<span class="hljs-keyword">cli</span> login</pre></div> <p>We’ll need to generate summaries in order to compute ROUGE scores during training. Fortunately, 🤗 Transformers provides dedicated <code>Seq2SeqTrainingArguments</code> and <code>Seq2SeqTrainer</code> classes that can do this for us automatically! To see how this works, let’s first define the hyperparameters and other arguments for our experiments:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Seq2SeqTrainingArguments batch_size = <span class="hljs-number">8</span> num_train_epochs = <span class="hljs-number">8</span> <span class="hljs-comment"># Show the training loss with every epoch</span> logging_steps = <span class="hljs-built_in">len</span>(tokenized_datasets[<span class="hljs-string">"train"</span>]) // batch_size model_name = model_checkpoint.split(<span class="hljs-string">"/"</span>)[-<span class="hljs-number">1</span>] args = Seq2SeqTrainingArguments( output_dir=<span class="hljs-string">f"<span class="hljs-subst">{model_name}</span>-finetuned-amazon-en-es"</span>, evaluation_strategy=<span class="hljs-string">"epoch"</span>, learning_rate=<span class="hljs-number">5.6e-5</span>, per_device_train_batch_size=batch_size, per_device_eval_batch_size=batch_size, weight_decay=<span class="hljs-number">0.01</span>, save_total_limit=<span class="hljs-number">3</span>, num_train_epochs=num_train_epochs, predict_with_generate=<span class="hljs-literal">True</span>, logging_steps=logging_steps, push_to_hub=<span class="hljs-literal">True</span>, )</pre></div> <p>Here, the <code>predict_with_generate</code> argument has been set to indicate that we should generate summaries during evaluation so that we can compute ROUGE scores for each epoch. As discussed in <a href="/course/chapter1">Chapter 1</a>, the decoder performs inference by predicting tokens one by one, and this is implemented by the model’s <code>generate()</code> method. Setting <code>predict_with_generate=True</code> tells the <code>Seq2SeqTrainer</code> to use that method for evaluation. We’ve also adjusted some of the default hyperparameters, like the learning rate, number of epochs, and weight decay, and we’ve set the <code>save_total_limit</code> option to only save up to 3 checkpoints during training — this is because even the “small” version of mT5 uses around a GB of hard drive space, and we can save a bit of room by limiting the number of copies we save.</p> <p>The <code>push_to_hub=True</code> argument will allow us to push the model to the Hub after training; you’ll find the repository under your user profile in the location defined by <code>output_dir</code>. Note that you can specify the name of the repository you want to push to with the <code>hub_model_id</code> argument (in particular, you will have to use this argument to push to an organization). For instance, when we pushed the model to the <a href="https://huggingface.co/huggingface-course" rel="nofollow"><code>huggingface-course</code> organization</a>, we added <code>hub_model_id="huggingface-course/mt5-finetuned-amazon-en-es"</code> to <code>Seq2SeqTrainingArguments</code>.</p> <p>The next thing we need to do is provide the trainer with a <code>compute_metrics()</code> function so that we can evaluate our model during training. For summarization this is a bit more involved than simply calling <code>rouge_score.compute()</code> on the model’s predictions, since we need to <em>decode</em> the outputs and labels into text before we can compute the ROUGE scores. The following function does exactly that, and also makes use of the <code>sent_tokenize()</code> function from <code>nltk</code> to separate the summary sentences with newlines:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_pred</span>): predictions, labels = eval_pred <span class="hljs-comment"># Decode generated summaries into text</span> decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=<span class="hljs-literal">True</span>) <span class="hljs-comment"># Replace -100 in the labels as we can't decode them</span> labels = np.where(labels != -<span class="hljs-number">100</span>, labels, tokenizer.pad_token_id) <span class="hljs-comment"># Decode reference summaries into text</span> decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=<span class="hljs-literal">True</span>) <span class="hljs-comment"># ROUGE expects a newline after each sentence</span> decoded_preds = [<span class="hljs-string">"\n"</span>.join(sent_tokenize(pred.strip())) <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> decoded_preds] decoded_labels = [<span class="hljs-string">"\n"</span>.join(sent_tokenize(label.strip())) <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> decoded_labels] <span class="hljs-comment"># Compute ROUGE scores</span> result = rouge_score.compute( predictions=decoded_preds, references=decoded_labels, use_stemmer=<span class="hljs-literal">True</span> ) <span class="hljs-comment"># Extract the median scores</span> result = {key: value.mid.fmeasure * <span class="hljs-number">100</span> <span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">in</span> result.items()} <span class="hljs-keyword">return</span> {k: <span class="hljs-built_in">round</span>(v, <span class="hljs-number">4</span>) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> result.items()}</pre></div> <p>Next, we need to define a data collator for our sequence-to-sequence task. Since mT5 is an encoder-decoder Transformer model, one subtlety with preparing our batches is that during decoding we need to shift the labels to the right by one. This is required to ensure that the decoder only sees the previous ground truth labels and not the current or future ones, which would be easy for the model to memorize. This is similar to how masked self-attention is applied to the inputs in a task like <a href="/course/chapter7/6">causal language modeling</a>.</p> <p>Luckily, 🤗 Transformers provides a <code>DataCollatorForSeq2Seq</code> collator that will dynamically pad the inputs and the labels for us. To instantiate this collator, we simply need to provide the <code>tokenizer</code> and <code>model</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorForSeq2Seq data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)</pre></div> <p>Let’s see what this collator produces when fed a small batch of examples. First, we need to remove the columns with strings because the collator won’t know how to pad these elements:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_datasets = tokenized_datasets.remove_columns( books_dataset[<span class="hljs-string">"train"</span>].column_names )</pre></div> <p>Since the collator expects a list of <code>dict</code>s, where each <code>dict</code> represents a single example in the dataset, we also need to wrangle the data into the expected format before passing it to the data collator:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>features = [tokenized_datasets[<span class="hljs-string">"train"</span>][i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">2</span>)] data_collator(features)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'attention_mask'</span>: tensor([[<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]]), <span class="hljs-string">'input_ids'</span>: tensor([[ <span class="hljs-number">1494</span>, <span class="hljs-number">259</span>, <span class="hljs-number">8622</span>, <span class="hljs-number">390</span>, <span class="hljs-number">259</span>, <span class="hljs-number">262</span>, <span class="hljs-number">2316</span>, <span class="hljs-number">3435</span>, <span class="hljs-number">955</span>, <span class="hljs-number">772</span>, <span class="hljs-number">281</span>, <span class="hljs-number">772</span>, <span class="hljs-number">1617</span>, <span class="hljs-number">263</span>, <span class="hljs-number">305</span>, <span class="hljs-number">14701</span>, <span class="hljs-number">260</span>, <span class="hljs-number">1385</span>, <span class="hljs-number">3031</span>, <span class="hljs-number">259</span>, <span class="hljs-number">24146</span>, <span class="hljs-number">332</span>, <span class="hljs-number">1037</span>, <span class="hljs-number">259</span>, <span class="hljs-number">43906</span>, <span class="hljs-number">305</span>, <span class="hljs-number">336</span>, <span class="hljs-number">260</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [ <span class="hljs-number">259</span>, <span class="hljs-number">27531</span>, <span class="hljs-number">13483</span>, <span class="hljs-number">259</span>, <span class="hljs-number">7505</span>, <span class="hljs-number">260</span>, <span class="hljs-number">112240</span>, <span class="hljs-number">15192</span>, <span class="hljs-number">305</span>, <span class="hljs-number">53198</span>, <span class="hljs-number">276</span>, <span class="hljs-number">259</span>, <span class="hljs-number">74060</span>, <span class="hljs-number">263</span>, <span class="hljs-number">260</span>, <span class="hljs-number">459</span>, <span class="hljs-number">25640</span>, <span class="hljs-number">776</span>, <span class="hljs-number">2119</span>, <span class="hljs-number">336</span>, <span class="hljs-number">259</span>, <span class="hljs-number">2220</span>, <span class="hljs-number">259</span>, <span class="hljs-number">18896</span>, <span class="hljs-number">288</span>, <span class="hljs-number">4906</span>, <span class="hljs-number">288</span>, <span class="hljs-number">1037</span>, <span class="hljs-number">3931</span>, <span class="hljs-number">260</span>, <span class="hljs-number">7083</span>, <span class="hljs-number">101476</span>, <span class="hljs-number">1143</span>, <span class="hljs-number">260</span>, <span class="hljs-number">1</span>]]), <span class="hljs-string">'labels'</span>: tensor([[ <span class="hljs-number">7483</span>, <span class="hljs-number">259</span>, <span class="hljs-number">2364</span>, <span class="hljs-number">15695</span>, <span class="hljs-number">1</span>, -<span class="hljs-number">100</span>], [ <span class="hljs-number">259</span>, <span class="hljs-number">27531</span>, <span class="hljs-number">13483</span>, <span class="hljs-number">259</span>, <span class="hljs-number">7505</span>, <span class="hljs-number">1</span>]]), <span class="hljs-string">'decoder_input_ids'</span>: tensor([[ <span class="hljs-number">0</span>, <span class="hljs-number">7483</span>, <span class="hljs-number">259</span>, <span class="hljs-number">2364</span>, <span class="hljs-number">15695</span>, <span class="hljs-number">1</span>], [ <span class="hljs-number">0</span>, <span class="hljs-number">259</span>, <span class="hljs-number">27531</span>, <span class="hljs-number">13483</span>, <span class="hljs-number">259</span>, <span class="hljs-number">7505</span>]])}</pre></div> <p>The main thing to notice here is that the first example is longer than the second one, so the <code>input_ids</code> and <code>attention_mask</code> of the second example have been padded on the right with a <code>[PAD]</code> token (whose ID is <code>0</code>). Similarly, we can see that the <code>labels</code> have been padded with <code>-100</code>s, to make sure the padding tokens are ignored by the loss function. And finally, we can see a new <code>decoder_input_ids</code> which has shifted the labels to the right by inserting a <code>[PAD]</code> token in the first entry.</p> <p>We finally have all the ingredients we need to train with! We now simply need to instantiate the trainer with the standard arguments:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Seq2SeqTrainer trainer = Seq2SeqTrainer( model, args, train_dataset=tokenized_datasets[<span class="hljs-string">"train"</span>], eval_dataset=tokenized_datasets[<span class="hljs-string">"validation"</span>], data_collator=data_collator, tokenizer=tokenizer, compute_metrics=compute_metrics, )</pre></div> <p>and launch our training run:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.train()</pre></div> <p>During training, you should see the training loss decrease and the ROUGE scores increase with each epoch. Once the training is complete, you can see the final ROUGE scores by running <code>Trainer.evaluate()</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.evaluate()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'eval_loss'</span>: <span class="hljs-number">3.028524398803711</span>, <span class="hljs-string">'eval_rouge1'</span>: <span class="hljs-number">16.9728</span>, <span class="hljs-string">'eval_rouge2'</span>: <span class="hljs-number">8.2969</span>, <span class="hljs-string">'eval_rougeL'</span>: <span class="hljs-number">16.8366</span>, <span class="hljs-string">'eval_rougeLsum'</span>: <span class="hljs-number">16.851</span>, <span class="hljs-string">'eval_gen_len'</span>: <span class="hljs-number">10.1597</span>, <span class="hljs-string">'eval_runtime'</span>: <span class="hljs-number">6.1054</span>, <span class="hljs-string">'eval_samples_per_second'</span>: <span class="hljs-number">38.982</span>, <span class="hljs-string">'eval_steps_per_second'</span>: <span class="hljs-number">4.914</span>}</pre></div> <p>From the scores we can see that our model has handily outperformed our lead-3 baseline — nice! The final thing to do is push the model weights to the Hub, as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.push_to_hub(<span class="hljs-attribute">commit_message</span>=<span class="hljs-string">"Training complete"</span>, <span class="hljs-attribute">tags</span>=<span class="hljs-string">"summarization"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'https://huggingface.co/huggingface-course/mt5-finetuned-amazon-en-es/commit/aa0536b829b28e73e1e4b94b8a5aacec420d40e0'</span></pre></div> <p>This will save the checkpoint and configuration files to <code>output_dir</code>, before uploading all the files to the Hub. By specifying the <code>tags</code> argument, we also ensure that the widget on the Hub will be one for a summarization pipeline instead of the default text generation one associated with the mT5 architecture (for more information about model tags, see the <a href="https://huggingface.co/docs/hub/main#how-is-a-models-type-of-inference-api-and-widget-determined" rel="nofollow">🤗 Hub documentation</a>). The output from <code>trainer.push_to_hub()</code> is a URL to the Git commit hash, so you can easily see the changes that were made to the model repository!</p> <p>To wrap up this section, let’s take a look at how we can also fine-tune mT5 using the low-level features provided by 🤗 Accelerate.</p> <h2 class="relative group"><a id="fine-tuning-mt5-with-accelerate" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-mt5-with-accelerate"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning mT5 with 🤗 Accelerate</span></h2> <p>Fine-tuning our model with 🤗 Accelerate is very similar to the text classification example we encountered in <a href="/course/chapter3">Chapter 3</a>. The main differences will be the need to explicitly generate our summaries during training and define how we compute the ROUGE scores (recall that the <code>Seq2SeqTrainer</code> took care of the generation for us). Let’s take a look how we can implement these two requirements within 🤗 Accelerate!</p> <h3 class="relative group"><a id="preparing-everything-for-training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preparing-everything-for-training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preparing everything for training</span></h3> <p>The first thing we need to do is create a <code>DataLoader</code> for each of our splits. Since the PyTorch dataloaders expect batches of tensors, we need to set the format to <code>"torch"</code> in our datasets:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenized_datasets.set_format(<span class="hljs-string">"torch"</span>)</pre></div> <p>Now that we’ve got datasets consisting of just tensors, the next thing to do is instantiate the <code>DataCollatorForSeq2Seq</code> again. For this we need to provide a fresh version of the model, so let’s load it again from our cache:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)</pre></div> <p>We can then instantiate the data collator and use this to define our dataloaders:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader batch_size = <span class="hljs-number">8</span> train_dataloader = DataLoader( tokenized_datasets[<span class="hljs-string">"train"</span>], shuffle=<span class="hljs-literal">True</span>, collate_fn=data_collator, batch_size=batch_size, ) eval_dataloader = DataLoader( tokenized_datasets[<span class="hljs-string">"validation"</span>], collate_fn=data_collator, batch_size=batch_size )</pre></div> <p>The next thing to do is define the optimizer we want to use. As in our other examples, we’ll use <code>AdamW</code>, which works well for most problems:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.optim <span class="hljs-keyword">import</span> AdamW optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">2e-5</span>)</pre></div> <p>Finally, we feed our model, optimizer, and dataloaders to the <code>accelerator.prepare()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator accelerator = Accelerator() model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>🚨 If you’re training on a TPU, you’ll need to move all the code above into a dedicated training function. See <a href="/course/chapter3">Chapter 3</a> for more details.</p></div> <p>Now that we’ve prepared our objects, there are three remaining things to do:</p> <ul><li>Define the learning rate schedule.</li> <li>Implement a function to post-process the summaries for evaluation.</li> <li>Create a repository on the Hub that we can push our model to.</li></ul> <p>For the learning rate schedule, we’ll use the standard linear one from previous sections:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler num_train_epochs = <span class="hljs-number">10</span> num_update_steps_per_epoch = <span class="hljs-built_in">len</span>(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( <span class="hljs-string">"linear"</span>, optimizer=optimizer, num_warmup_steps=<span class="hljs-number">0</span>, num_training_steps=num_training_steps, )</pre></div> <p>For post-processing, we need a function that splits the generated summaries into sentences that are separated by newlines. This is the format the ROUGE metric expects, and we can achieve this with the following snippet of code:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">postprocess_text</span>(<span class="hljs-params">preds, labels</span>): preds = [pred.strip() <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> preds] labels = [label.strip() <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> labels] <span class="hljs-comment"># ROUGE expects a newline after each sentence</span> preds = [<span class="hljs-string">"\n"</span>.join(nltk.sent_tokenize(pred)) <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> preds] labels = [<span class="hljs-string">"\n"</span>.join(nltk.sent_tokenize(label)) <span class="hljs-keyword">for</span> label <span class="hljs-keyword">in</span> labels] <span class="hljs-keyword">return</span> preds, labels</pre></div> <p>This should look familiar to you if you recall how we defined the <code>compute_metrics()</code> function of the <code>Seq2SeqTrainer</code>.</p> <p>Finally, we need to create a model repository on the Hugging Face Hub. For this, we can use the appropriately titled 🤗 Hub library. We just need to define a name for our repository, and the library has a utility function to combine the repository ID with the user profile:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> get_full_repo_name model_name = <span class="hljs-string">"test-bert-finetuned-squad-accelerate"</span> repo_name = get_full_repo_name(model_name) repo_name</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'lewtun/mt5-finetuned-amazon-en-es-accelerate'</span></pre></div> <p>Now we can use this repository name to clone a local version to our results directory that will store the training artifacts:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> Repository output_dir = <span class="hljs-string">"results-mt5-finetuned-squad-accelerate"</span> repo = Repository(output_dir, clone_from=repo_name)</pre></div> <p>This will allow us to push the artifacts back to the Hub by calling the <code>repo.push_to_hub()</code> method during training! Let’s now wrap up our analysis by writing out the training loop.</p> <h3 class="relative group"><a id="training-loop" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-loop"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training loop</span></h3> <p>The training loop for summarization is quite similar to the other 🤗 Accelerate examples that we’ve encountered and is roughly split into four main steps:</p> <ol><li>Train the model by iterating over all the examples in <code>train_dataloader</code> for each epoch.</li> <li>Generate model summaries at the end of each epoch, by first generating the tokens and then decoding them (and the reference summaries) into text.</li> <li>Compute the ROUGE scores using the same techniques we saw earlier.</li> <li>Save the checkpoints and push everything to the Hub. Here we rely on the nifty <code>blocking=False</code> argument of the <code>Repository</code> object so that we can push the checkpoints per epoch <em>asynchronously</em>. This allows us to continue training without having to wait for the somewhat slow upload associated with a GB-sized model!</li></ol> <p>These steps can be seen in the following block of code:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm <span class="hljs-keyword">import</span> torch <span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps)) <span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_train_epochs): <span class="hljs-comment"># Training</span> model.train() <span class="hljs-keyword">for</span> step, batch <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(train_dataloader): outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(<span class="hljs-number">1</span>) <span class="hljs-comment"># Evaluation</span> model.<span class="hljs-built_in">eval</span>() <span class="hljs-keyword">for</span> step, batch <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(eval_dataloader): <span class="hljs-keyword">with</span> torch.no_grad(): generated_tokens = accelerator.unwrap_model(model).generate( batch[<span class="hljs-string">"input_ids"</span>], attention_mask=batch[<span class="hljs-string">"attention_mask"</span>], ) generated_tokens = accelerator.pad_across_processes( generated_tokens, dim=<span class="hljs-number">1</span>, pad_index=tokenizer.pad_token_id ) labels = batch[<span class="hljs-string">"labels"</span>] <span class="hljs-comment"># If we did not pad to max length, we need to pad the labels too</span> labels = accelerator.pad_across_processes( batch[<span class="hljs-string">"labels"</span>], dim=<span class="hljs-number">1</span>, pad_index=tokenizer.pad_token_id ) generated_tokens = accelerator.gather(generated_tokens).cpu().numpy() labels = accelerator.gather(labels).cpu().numpy() <span class="hljs-comment"># Replace -100 in the labels as we can't decode them</span> labels = np.where(labels != -<span class="hljs-number">100</span>, labels, tokenizer.pad_token_id) <span class="hljs-keyword">if</span> <span class="hljs-built_in">isinstance</span>(generated_tokens, <span class="hljs-built_in">tuple</span>): generated_tokens = generated_tokens[<span class="hljs-number">0</span>] decoded_preds = tokenizer.batch_decode( generated_tokens, skip_special_tokens=<span class="hljs-literal">True</span> ) decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=<span class="hljs-literal">True</span>) decoded_preds, decoded_labels = postprocess_text( decoded_preds, decoded_labels ) rouge_score.add_batch(predictions=decoded_preds, references=decoded_labels) <span class="hljs-comment"># Compute metrics</span> result = rouge_score.compute() <span class="hljs-comment"># Extract the median ROUGE scores</span> result = {key: value.mid.fmeasure * <span class="hljs-number">100</span> <span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">in</span> result.items()} result = {k: <span class="hljs-built_in">round</span>(v, <span class="hljs-number">4</span>) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> result.items()} <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Epoch <span class="hljs-subst">{epoch}</span>:"</span>, result) <span class="hljs-comment"># Save and upload</span> accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) <span class="hljs-keyword">if</span> accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=<span class="hljs-string">f"Training in progress epoch <span class="hljs-subst">{epoch}</span>"</span>, blocking=<span class="hljs-literal">False</span> )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Epoch <span class="hljs-number">0</span>: {<span class="hljs-string">'rouge1'</span>: <span class="hljs-number">5.6351</span>, <span class="hljs-string">'rouge2'</span>: <span class="hljs-number">1.1625</span>, <span class="hljs-string">'rougeL'</span>: <span class="hljs-number">5.4866</span>, <span class="hljs-string">'rougeLsum'</span>: <span class="hljs-number">5.5005</span>} Epoch <span class="hljs-number">1</span>: {<span class="hljs-string">'rouge1'</span>: <span class="hljs-number">9.8646</span>, <span class="hljs-string">'rouge2'</span>: <span class="hljs-number">3.4106</span>, <span class="hljs-string">'rougeL'</span>: <span class="hljs-number">9.9439</span>, <span class="hljs-string">'rougeLsum'</span>: <span class="hljs-number">9.9306</span>} Epoch <span class="hljs-number">2</span>: {<span class="hljs-string">'rouge1'</span>: <span class="hljs-number">11.0872</span>, <span class="hljs-string">'rouge2'</span>: <span class="hljs-number">3.3273</span>, <span class="hljs-string">'rougeL'</span>: <span class="hljs-number">11.0508</span>, <span class="hljs-string">'rougeLsum'</span>: <span class="hljs-number">10.9468</span>} Epoch <span class="hljs-number">3</span>: {<span class="hljs-string">'rouge1'</span>: <span class="hljs-number">11.8587</span>, <span class="hljs-string">'rouge2'</span>: <span class="hljs-number">4.8167</span>, <span class="hljs-string">'rougeL'</span>: <span class="hljs-number">11.7986</span>, <span class="hljs-string">'rougeLsum'</span>: <span class="hljs-number">11.7518</span>} Epoch <span class="hljs-number">4</span>: {<span class="hljs-string">'rouge1'</span>: <span class="hljs-number">12.9842</span>, <span class="hljs-string">'rouge2'</span>: <span class="hljs-number">5.5887</span>, <span class="hljs-string">'rougeL'</span>: <span class="hljs-number">12.7546</span>, <span class="hljs-string">'rougeLsum'</span>: <span class="hljs-number">12.7029</span>} Epoch <span class="hljs-number">5</span>: {<span class="hljs-string">'rouge1'</span>: <span class="hljs-number">13.4628</span>, <span class="hljs-string">'rouge2'</span>: <span class="hljs-number">6.4598</span>, <span class="hljs-string">'rougeL'</span>: <span class="hljs-number">13.312</span>, <span class="hljs-string">'rougeLsum'</span>: <span class="hljs-number">13.2913</span>} Epoch <span class="hljs-number">6</span>: {<span class="hljs-string">'rouge1'</span>: <span class="hljs-number">12.9131</span>, <span class="hljs-string">'rouge2'</span>: <span class="hljs-number">5.8914</span>, <span class="hljs-string">'rougeL'</span>: <span class="hljs-number">12.6896</span>, <span class="hljs-string">'rougeLsum'</span>: <span class="hljs-number">12.5701</span>} Epoch <span class="hljs-number">7</span>: {<span class="hljs-string">'rouge1'</span>: <span class="hljs-number">13.3079</span>, <span class="hljs-string">'rouge2'</span>: <span class="hljs-number">6.2994</span>, <span class="hljs-string">'rougeL'</span>: <span class="hljs-number">13.1536</span>, <span class="hljs-string">'rougeLsum'</span>: <span class="hljs-number">13.1194</span>} Epoch <span class="hljs-number">8</span>: {<span class="hljs-string">'rouge1'</span>: <span class="hljs-number">13.96</span>, <span class="hljs-string">'rouge2'</span>: <span class="hljs-number">6.5998</span>, <span class="hljs-string">'rougeL'</span>: <span class="hljs-number">13.9123</span>, <span class="hljs-string">'rougeLsum'</span>: <span class="hljs-number">13.7744</span>} Epoch <span class="hljs-number">9</span>: {<span class="hljs-string">'rouge1'</span>: <span class="hljs-number">14.1192</span>, <span class="hljs-string">'rouge2'</span>: <span class="hljs-number">7.0059</span>, <span class="hljs-string">'rougeL'</span>: <span class="hljs-number">14.1172</span>, <span class="hljs-string">'rougeLsum'</span>: <span class="hljs-number">13.9509</span>}</pre></div> <p>And that’s it! Once you run this, you’ll have a model and results that are pretty similar to the ones we obtained with the <code>Trainer</code>.</p> <h2 class="relative group"><a id="using-your-fine-tuned-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-your-fine-tuned-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using your fine-tuned model</span></h2> <p>Once you’ve pushed the model to the Hub, you can play with it either via the inference widget or with a <code>pipeline</code> object, as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline hub_model_id = <span class="hljs-string">"huggingface-course/mt5-small-finetuned-amazon-en-es"</span> summarizer = pipeline(<span class="hljs-string">"summarization"</span>, model=hub_model_id)</pre></div> <p>We can feed some examples from the test set (which the model has not seen) to our pipeline to get a feel for the quality of the summaries. First let’s implement a simple function to show the review, title, and generated summary together:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">print_summary</span>(<span class="hljs-params">idx</span>): review = books_dataset[<span class="hljs-string">"test"</span>][idx][<span class="hljs-string">"review_body"</span>] title = books_dataset[<span class="hljs-string">"test"</span>][idx][<span class="hljs-string">"review_title"</span>] summary = summarizer(books_dataset[<span class="hljs-string">"test"</span>][idx][<span class="hljs-string">"review_body"</span>])[<span class="hljs-number">0</span>][<span class="hljs-string">"summary_text"</span>] <span class="hljs-built_in">print</span>(<span class="hljs-string">f"'&gt;&gt;&gt; Review: <span class="hljs-subst">{review}</span>'"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"\n'&gt;&gt;&gt; Title: <span class="hljs-subst">{title}</span>'"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"\n'&gt;&gt;&gt; Summary: <span class="hljs-subst">{summary}</span>'"</span>)</pre></div> <p>Let’s take a look at one of the English examples we get:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>print_summary(<span class="hljs-number">100</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt;&gt; Review: Nothing special at all about this product... the book is too small and stiff and hard to write in. The huge sticker on the back doesn’t come off and looks super tacky. I would not purchase this again. I could have just bought a journal from the dollar store and it would be basically the same thing. It’s also really expensive for what it is.'</span> <span class="hljs-string">'&gt;&gt;&gt; Title: Not impressed at all... buy something else'</span> <span class="hljs-string">'&gt;&gt;&gt; Summary: Nothing special at all about this product'</span></pre></div> <p>This is not too bad! We can see that our model has actually been able to perform <em>abstractive</em> summarization by augmenting parts of the review with new words. And perhaps the coolest aspect of our model is that it is bilingual, so we can also generate summaries of Spanish reviews:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>print_summary(<span class="hljs-number">0</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'&gt;&gt;&gt; Review: Es una trilogia que se hace muy facil de leer. Me ha gustado, no me esperaba el final para nada'</span> <span class="hljs-string">'&gt;&gt;&gt; Title: Buena literatura para adolescentes'</span> <span class="hljs-string">'&gt;&gt;&gt; Summary: Muy facil de leer'</span></pre></div> <p>The summary translates into “Very easy to read” in English, which we can see in this case was extracted directly from the review. Nevertheless, this shows the versatility of the mT5 model and has given you a taste of what it’s like to deal with a multilingual corpus!</p> <p>Next, we’ll turn our attention to a slightly more complex task: training a language model from scratch.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter7/4?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Translation</a> <a href="/learn/nlp-course/chapter7/6?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Training a causal language model from scratch<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Summarization&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;summarization&quot;,&quot;url&quot;:&quot;#summarization&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Preparing a multilingual corpus&quot;,&quot;id&quot;:&quot;preparing-a-multilingual-corpus&quot;,&quot;url&quot;:&quot;#preparing-a-multilingual-corpus&quot;},{&quot;title&quot;:&quot;Models for text summarization&quot;,&quot;id&quot;:&quot;models-for-text-summarization&quot;,&quot;url&quot;:&quot;#models-for-text-summarization&quot;},{&quot;title&quot;:&quot;Preprocessing the data&quot;,&quot;id&quot;:&quot;preprocessing-the-data&quot;,&quot;url&quot;:&quot;#preprocessing-the-data&quot;},{&quot;title&quot;:&quot;Metrics for text summarization&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;metrics-for-text-summarization&quot;,&quot;url&quot;:&quot;#metrics-for-text-summarization&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Creating a strong baseline&quot;,&quot;id&quot;:&quot;creating-a-strong-baseline&quot;,&quot;url&quot;:&quot;#creating-a-strong-baseline&quot;}]},{&quot;title&quot;:&quot;Fine-tuning mT5 with the `Trainer` API&quot;,&quot;id&quot;:&quot;fine-tuning-mt5-with-the-trainer-api&quot;,&quot;url&quot;:&quot;#fine-tuning-mt5-with-the-trainer-api&quot;},{&quot;title&quot;:&quot;Fine-tuning mT5 with Keras&quot;,&quot;id&quot;:&quot;fine-tuning-mt5-with-keras&quot;,&quot;url&quot;:&quot;#fine-tuning-mt5-with-keras&quot;},{&quot;title&quot;:&quot;Fine-tuning mT5 with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;fine-tuning-mt5-with-accelerate&quot;,&quot;url&quot;:&quot;#fine-tuning-mt5-with-accelerate&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Preparing everything for training&quot;,&quot;id&quot;:&quot;preparing-everything-for-training&quot;,&quot;url&quot;:&quot;#preparing-everything-for-training&quot;},{&quot;title&quot;:&quot;Training loop&quot;,&quot;id&quot;:&quot;training-loop&quot;,&quot;url&quot;:&quot;#training-loop&quot;}]},{&quot;title&quot;:&quot;Using your fine-tuned model&quot;,&quot;id&quot;:&quot;using-your-fine-tuned-model&quot;,&quot;url&quot;:&quot;#using-your-fine-tuned-model&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#summarization" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-summarization"><wbr>Summarization</a> <a href="#preparing-a-multilingual-corpus" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preparing-a-multilingual-corpus"><wbr>Preparing a multilingual corpus</a> <a href="#models-for-text-summarization" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-models-for-text-summarization"><wbr>Models for text summarization</a> <a href="#preprocessing-the-data" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preprocessing-the-data"><wbr>Preprocessing the data</a> <a href="#metrics-for-text-summarization" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-metrics-for-text-summarization"><wbr>Metrics for text summarization</a> <a href="#creating-a-strong-baseline" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-creating-a-strong-baseline"><wbr>Creating a strong baseline</a> <a href="#fine-tuning-mt5-with-the-trainer-api" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-mt5-with-the-trainer-api"><wbr>Fine-tuning m<wbr>T5 with the `<wbr>Trainer` API</a> <a href="#fine-tuning-mt5-with-accelerate" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-mt5-with-accelerate"><wbr>Fine-tuning m<wbr>T5 with 🤗 <wbr>Accelerate</a> <a href="#preparing-everything-for-training" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preparing-everything-for-training"><wbr>Preparing everything for training</a> <a href="#training-loop" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-loop"><wbr>Training loop</a> <a href="#using-your-fine-tuned-model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-your-fine-tuned-model"><wbr>Using your fine-tuned model</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter7/5" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter7/5"); } </script> <iframe name="__privateStripeMetricsController3280" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter7%2F5%3Ffw%3Dpt&amp;title=Summarization%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:30.734Z
Question answering - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter7/7?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#question-answering)Question answering [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-7-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section7_pt.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section7_pt.ipynb) Time to look at question answering! This task comes in many flavors, but the one we’ll focus on in this section is called _extractive_ question answering. This involves posing questions about a document and identifying the answers as _spans of text_ in the document itself. We will fine-tune a BERT model on the [SQuAD dataset](https://rajpurkar.github.io/SQuAD-explorer/), which consists of questions posed by crowdworkers on a set of Wikipedia articles. This will give us a model able to compute predictions like this one: This is actually showcasing the model that was trained and uploaded to the Hub using the code shown in this section. You can find it and double-check the predictions [here](https://huggingface.co/huggingface-course/bert-finetuned-squad?context=%F0%9F%A4%97+Transformers+is+backed+by+the+three+most+popular+deep+learning+libraries+%E2%80%94+Jax%2C+PyTorch+and+TensorFlow+%E2%80%94+with+a+seamless+integration+between+them.+It%27s+straightforward+to+train+your+models+with+one+before+loading+them+for+inference+with+the+other.&question=Which+deep+learning+libraries+back+%F0%9F%A4%97+Transformers%3F). 💡 Encoder-only models like BERT tend to be great at extracting answers to factoid questions like “Who invented the Transformer architecture?” but fare poorly when given open-ended questions like “Why is the sky blue?” In these more challenging cases, encoder-decoder models like T5 and BART are typically used to synthesize the information in a way that’s quite similar to [text summarization](/course/chapter7/5). If you’re interested in this type of _generative_ question answering, we recommend checking out our [demo](https://yjernite.github.io/lfqa.html) based on the [ELI5 dataset](https://huggingface.co/datasets/eli5). ## [](#preparing-the-data)Preparing the data The dataset that is used the most as an academic benchmark for extractive question answering is [SQuAD](https://rajpurkar.github.io/SQuAD-explorer/), so that’s the one we’ll use here. There is also a harder [SQuAD v2](https://huggingface.co/datasets/squad_v2) benchmark, which includes questions that don’t have an answer. As long as your own dataset contains a column for contexts, a column for questions, and a column for answers, you should be able to adapt the steps below. ### [](#the-squad-dataset)The SQuAD dataset As usual, we can download and cache the dataset in just one step thanks to `load_dataset()`: ``` from datasets import load_dataset raw_datasets = load_dataset("squad")``` We can then have a look at this object to learn more about the SQuAD dataset: ``` DatasetDict({ train: Dataset({ features: ['id', 'title', 'context', 'question', 'answers'], num_rows: 87599 }) validation: Dataset({ features: ['id', 'title', 'context', 'question', 'answers'], num_rows: 10570 }) })``` It looks like we have everything we need with the `context`, `question`, and `answers` fields, so let’s print those for the first element of our training set: ``` print("Context: ", raw_datasets["train"][0]["context"]) print("Question: ", raw_datasets["train"][0]["question"]) print("Answer: ", raw_datasets["train"][0]["answers"])``` ``` Context: 'Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.' Question: 'To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?' Answer: {'text': ['Saint Bernadette Soubirous'], 'answer_start': [515]}``` The `context` and `question` fields are very straightforward to use. The `answers` field is a bit trickier as it comports a dictionary with two fields that are both lists. This is the format that will be expected by the `squad` metric during evaluation; if you are using your own data, you don’t necessarily need to worry about putting the answers in the same format. The `text` field is rather obvious, and the `answer_start` field contains the starting character index of each answer in the context. During training, there is only one possible answer. We can double-check this by using the `Dataset.filter()` method: ``` raw_datasets["train"].filter(lambda x: len(x["answers"]["text"]) != 1)``` ``` Dataset({ features: ['id', 'title', 'context', 'question', 'answers'], num_rows: 0 })``` For evaluation, however, there are several possible answers for each sample, which may be the same or different: ``` print(raw_datasets["validation"][0]["answers"]) print(raw_datasets["validation"][2]["answers"])``` ``` {'text': ['Denver Broncos', 'Denver Broncos', 'Denver Broncos'], 'answer_start': [177, 177, 177]} {'text': ['Santa Clara, California', "Levi's Stadium", "Levi's Stadium in the San Francisco Bay Area at Santa Clara, California."], 'answer_start': [403, 355, 355]}``` We won’t dive into the evaluation script as it will all be wrapped up by a 🤗 Datasets metric for us, but the short version is that some of the questions have several possible answers, and this script will compare a predicted answer to all the acceptable answers and take the best score. If we take a look at the sample at index 2, for instance: ``` print(raw_datasets["validation"][2]["context"]) print(raw_datasets["validation"][2]["question"])``` ``` 'Super Bowl 50 was an American football game to determine the champion of the National Football League (NFL) for the 2015 season. The American Football Conference (AFC) champion Denver Broncos defeated the National Football Conference (NFC) champion Carolina Panthers 24–10 to earn their third Super Bowl title. The game was played on February 7, 2016, at Levi\'s Stadium in the San Francisco Bay Area at Santa Clara, California. As this was the 50th Super Bowl, the league emphasized the "golden anniversary" with various gold-themed initiatives, as well as temporarily suspending the tradition of naming each Super Bowl game with Roman numerals (under which the game would have been known as "Super Bowl L"), so that the logo could prominently feature the Arabic numerals 50.' 'Where did Super Bowl 50 take place?'``` we can see that the answer can indeed be one of the three possibilities we saw before. ### [](#processing-the-training-data)Processing the training data Let’s start with preprocessing the training data. The hard part will be to generate labels for the question’s answer, which will be the start and end positions of the tokens corresponding to the answer inside the context. But let’s not get ahead of ourselves. First, we need to convert the text in the input into IDs the model can make sense of, using a tokenizer: ``` from transformers import AutoTokenizer model_checkpoint = "bert-base-cased" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)``` As mentioned previously, we’ll be fine-tuning a BERT model, but you can use any other model type as long as it has a fast tokenizer implemented. You can see all the architectures that come with a fast version in [this big table](https://huggingface.co/transformers/#supported-frameworks), and to check that the `tokenizer` object you’re using is indeed backed by 🤗 Tokenizers you can look at its `is_fast` attribute: We can pass to our tokenizer the question and the context together, and it will properly insert the special tokens to form a sentence like this: ``` [CLS] question [SEP] context [SEP]``` Let’s double-check: ``` context = raw_datasets["train"][0]["context"] question = raw_datasets["train"][0]["question"] inputs = tokenizer(question, context) tokenizer.decode(inputs["input_ids"])``` ``` '[CLS] To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France? [SEP] Architecturally, ' 'the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin ' 'Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms ' 'upraised with the legend " Venite Ad Me Omnes ". Next to the Main Building is the Basilica of the Sacred ' 'Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a ' 'replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette ' 'Soubirous in 1858. At the end of the main drive ( and in a direct line that connects through 3 statues ' 'and the Gold Dome ), is a simple, modern stone statue of Mary. [SEP]'``` The labels will then be the index of the tokens starting and ending the answer, and the model will be tasked to predicted one start and end logit per token in the input, with the theoretical labels being as follow: ![One-hot encoded labels for question answering.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/qa_labels.svg) ![One-hot encoded labels for question answering.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/qa_labels-dark.svg) In this case the context is not too long, but some of the examples in the dataset have very long contexts that will exceed the maximum length we set (which is 384 in this case). As we saw in [Chapter 6](/course/chapter6/4) when we explored the internals of the `question-answering` pipeline, we will deal with long contexts by creating several training features from one sample of our dataset, with a sliding window between them. To see how this works using the current example, we can limit the length to 100 and use a sliding window of 50 tokens. As a reminder, we use: - `max_length` to set the maximum length (here 100) - `truncation="only_second"` to truncate the context (which is in the second position) when the question with its context is too long - `stride` to set the number of overlapping tokens between two successive chunks (here 50) - `return_overflowing_tokens=True` to let the tokenizer know we want the overflowing tokens ``` inputs = tokenizer( question, context, max_length=100, truncation="only_second", stride=50, return_overflowing_tokens=True, ) for ids in inputs["input_ids"]: print(tokenizer.decode(ids))``` ``` '[CLS] To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France? [SEP] Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend " Venite Ad Me Omnes ". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basi [SEP]' '[CLS] To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France? [SEP] the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend " Venite Ad Me Omnes ". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin [SEP]' '[CLS] To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France? [SEP] Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive ( and in a direct line that connects through 3 [SEP]' '[CLS] To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France? [SEP]. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive ( and in a direct line that connects through 3 statues and the Gold Dome ), is a simple, modern stone statue of Mary. [SEP]'``` As we can see, our example has been in split into four inputs, each of them containing the question and some part of the context. Note that the answer to the question (“Bernadette Soubirous”) only appears in the third and last inputs, so by dealing with long contexts in this way we will create some training examples where the answer is not included in the context. For those examples, the labels will be `start_position = end_position = 0` (so we predict the `[CLS]` token). We will also set those labels in the unfortunate case where the answer has been truncated so that we only have the start (or end) of it. For the examples where the answer is fully in the context, the labels will be the index of the token where the answer starts and the index of the token where the answer ends. The dataset provides us with the start character of the answer in the context, and by adding the length of the answer, we can find the end character in the context. To map those to token indices, we will need to use the offset mappings we studied in [Chapter 6](/course/chapter6/4). We can have our tokenizer return these by passing along `return_offsets_mapping=True`: ``` inputs = tokenizer( question, context, max_length=100, truncation="only_second", stride=50, return_overflowing_tokens=True, return_offsets_mapping=True, ) inputs.keys()``` ``` dict_keys(['input_ids', 'token_type_ids', 'attention_mask', 'offset_mapping', 'overflow_to_sample_mapping'])``` As we can see, we get back the usual input IDs, token type IDs, and attention mask, as well as the offset mapping we required and an extra key, `overflow_to_sample_mapping`. The corresponding value will be of use to us when we tokenize several texts at the same time (which we should do to benefit from the fact that our tokenizer is backed by Rust). Since one sample can give several features, it maps each feature to the example it originated from. Because here we only tokenized one example, we get a list of `0`s: ``` inputs["overflow_to_sample_mapping"]``` But if we tokenize more examples, this will become more useful: ``` inputs = tokenizer( raw_datasets["train"][2:6]["question"], raw_datasets["train"][2:6]["context"], max_length=100, truncation="only_second", stride=50, return_overflowing_tokens=True, return_offsets_mapping=True, ) print(f"The 4 examples gave {len(inputs['input_ids'])} features.") print(f"Here is where each comes from: {inputs['overflow_to_sample_mapping']}.")``` ``` 'The 4 examples gave 19 features.' 'Here is where each comes from: [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3].'``` As we can see, the first three examples (at indices 2, 3, and 4 in the training set) each gave four features and the last example (at index 5 in the training set) gave 7 features. This information will be useful to map each feature we get to its corresponding label. As mentioned earlier, those labels are: - `(0, 0)` if the answer is not in the corresponding span of the context - `(start_position, end_position)` if the answer is in the corresponding span of the context, with `start_position` being the index of the token (in the input IDs) at the start of the answer and `end_position` being the index of the token (in the input IDs) where the answer ends To determine which of these is the case and, if relevant, the positions of the tokens, we first find the indices that start and end the context in the input IDs. We could use the token type IDs to do this, but since those do not necessarily exist for all models (DistilBERT does not require them, for instance), we’ll instead use the `sequence_ids()` method of the `BatchEncoding` our tokenizer returns. Once we have those token indices, we look at the corresponding offsets, which are tuples of two integers representing the span of characters inside the original context. We can thus detect if the chunk of the context in this feature starts after the answer or ends before the answer begins (in which case the label is `(0, 0)`). If that’s not the case, we loop to find the first and last token of the answer: ``` answers = raw_datasets["train"][2:6]["answers"] start_positions = [] end_positions = [] for i, offset in enumerate(inputs["offset_mapping"]): sample_idx = inputs["overflow_to_sample_mapping"][i] answer = answers[sample_idx] start_char = answer["answer_start"][0] end_char = answer["answer_start"][0] + len(answer["text"][0]) sequence_ids = inputs.sequence_ids(i) idx = 0 while sequence_ids[idx] != 1: idx += 1 context_start = idx while sequence_ids[idx] == 1: idx += 1 context_end = idx - 1 if offset[context_start][0] > start_char or offset[context_end][1] < end_char: start_positions.append(0) end_positions.append(0) else: idx = context_start while idx <= context_end and offset[idx][0] <= start_char: idx += 1 start_positions.append(idx - 1) idx = context_end while idx >= context_start and offset[idx][1] >= end_char: idx -= 1 end_positions.append(idx + 1) start_positions, end_positions``` ``` ([83, 51, 19, 0, 0, 64, 27, 0, 34, 0, 0, 0, 67, 34, 0, 0, 0, 0, 0], [85, 53, 21, 0, 0, 70, 33, 0, 40, 0, 0, 0, 68, 35, 0, 0, 0, 0, 0])``` Let’s take a look at a few results to verify that our approach is correct. For the first feature we find `(83, 85)` as labels, so let’s compare the theoretical answer with the decoded span of tokens from 83 to 85 (inclusive): ``` idx = 0 sample_idx = inputs["overflow_to_sample_mapping"][idx] answer = answers[sample_idx]["text"][0] start = start_positions[idx] end = end_positions[idx] labeled_answer = tokenizer.decode(inputs["input_ids"][idx][start : end + 1]) print(f"Theoretical answer: {answer}, labels give: {labeled_answer}")``` ``` 'Theoretical answer: the Main Building, labels give: the Main Building'``` So that’s a match! Now let’s check index 4, where we set the labels to `(0, 0)`, which means the answer is not in the context chunk of that feature: ``` idx = 4 sample_idx = inputs["overflow_to_sample_mapping"][idx] answer = answers[sample_idx]["text"][0] decoded_example = tokenizer.decode(inputs["input_ids"][idx]) print(f"Theoretical answer: {answer}, decoded example: {decoded_example}")``` ``` 'Theoretical answer: a Marian place of prayer and reflection, decoded example: [CLS] What is the Grotto at Notre Dame? [SEP] Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend " Venite Ad Me Omnes ". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grot [SEP]'``` Indeed, we don’t see the answer inside the context. ✏️ **Your turn!** When using the XLNet architecture, padding is applied on the left and the question and context are switched. Adapt all the code we just saw to the XLNet architecture (and add `padding=True`). Be aware that the `[CLS]` token may not be at the 0 position with padding applied. Now that we have seen step by step how to preprocess our training data, we can group it in a function we will apply on the whole training dataset. We’ll pad every feature to the maximum length we set, as most of the contexts will be long (and the corresponding samples will be split into several features), so there is no real benefit to applying dynamic padding here: ``` max_length = 384 stride = 128 def preprocess_training_examples(examples): questions = [q.strip() for q in examples["question"]] inputs = tokenizer( questions, examples["context"], max_length=max_length, truncation="only_second", stride=stride, return_overflowing_tokens=True, return_offsets_mapping=True, padding="max_length", ) offset_mapping = inputs.pop("offset_mapping") sample_map = inputs.pop("overflow_to_sample_mapping") answers = examples["answers"] start_positions = [] end_positions = [] for i, offset in enumerate(offset_mapping): sample_idx = sample_map[i] answer = answers[sample_idx] start_char = answer["answer_start"][0] end_char = answer["answer_start"][0] + len(answer["text"][0]) sequence_ids = inputs.sequence_ids(i) idx = 0 while sequence_ids[idx] != 1: idx += 1 context_start = idx while sequence_ids[idx] == 1: idx += 1 context_end = idx - 1 if offset[context_start][0] > start_char or offset[context_end][1] < end_char: start_positions.append(0) end_positions.append(0) else: idx = context_start while idx <= context_end and offset[idx][0] <= start_char: idx += 1 start_positions.append(idx - 1) idx = context_end while idx >= context_start and offset[idx][1] >= end_char: idx -= 1 end_positions.append(idx + 1) inputs["start_positions"] = start_positions inputs["end_positions"] = end_positions return inputs``` Note that we defined two constants to determine the maximum length used as well as the length of the sliding window, and that we added a tiny bit of cleanup before tokenizing: some of the questions in the SQuAD dataset have extra spaces at the beginning and the end that don’t add anything (and take up space when being tokenized if you use a model like RoBERTa), so we removed those extra spaces. To apply this function to the whole training set, we use the `Dataset.map()` method with the `batched=True` flag. It’s necessary here as we are changing the length of the dataset (since one example can give several training features): ``` train_dataset = raw_datasets["train"].map( preprocess_training_examples, batched=True, remove_columns=raw_datasets["train"].column_names, ) len(raw_datasets["train"]), len(train_dataset)``` As we can see, the preprocessing added roughly 1,000 features. Our training set is now ready to be used — let’s dig into the preprocessing of the validation set! ### [](#processing-the-validation-data)Processing the validation data Preprocessing the validation data will be slightly easier as we don’t need to generate labels (unless we want to compute a validation loss, but that number won’t really help us understand how good the model is). The real joy will be to interpret the predictions of the model into spans of the original context. For this, we will just need to store both the offset mappings and some way to match each created feature to the original example it comes from. Since there is an ID column in the original dataset, we’ll use that ID. The only thing we’ll add here is a tiny bit of cleanup of the offset mappings. They will contain offsets for the question and the context, but once we’re in the post-processing stage we won’t have any way to know which part of the input IDs corresponded to the context and which part was the question (the `sequence_ids()` method we used is available for the output of the tokenizer only). So, we’ll set the offsets corresponding to the question to `None`: ``` def preprocess_validation_examples(examples): questions = [q.strip() for q in examples["question"]] inputs = tokenizer( questions, examples["context"], max_length=max_length, truncation="only_second", stride=stride, return_overflowing_tokens=True, return_offsets_mapping=True, padding="max_length", ) sample_map = inputs.pop("overflow_to_sample_mapping") example_ids = [] for i in range(len(inputs["input_ids"])): sample_idx = sample_map[i] example_ids.append(examples["id"][sample_idx]) sequence_ids = inputs.sequence_ids(i) offset = inputs["offset_mapping"][i] inputs["offset_mapping"][i] = [ o if sequence_ids[k] == 1 else None for k, o in enumerate(offset) ] inputs["example_id"] = example_ids return inputs``` We can apply this function on the whole validation dataset like before: ``` validation_dataset = raw_datasets["validation"].map( preprocess_validation_examples, batched=True, remove_columns=raw_datasets["validation"].column_names, ) len(raw_datasets["validation"]), len(validation_dataset)``` In this case we’ve only added a couple of hundred samples, so it appears the contexts in the validation dataset are a bit shorter. Now that we have preprocessed all the data, we can get to the training. ## [](#fine-tuning-the-model-with-the-trainer-api)Fine-tuning the model with the `Trainer` API The training code for this example will look a lot like the code in the previous sections — the hardest thing will be to write the `compute_metrics()` function. Since we padded all the samples to the maximum length we set, there is no data collator to define, so this metric computation is really the only thing we have to worry about. The difficult part will be to post-process the model predictions into spans of text in the original examples; once we have done that, the metric from the 🤗 Datasets library will do most of the work for us. ### [](#post-processing)Post-processing The model will output logits for the start and end positions of the answer in the input IDs, as we saw during our exploration of the [`question-answering` pipeline](/course/chapter6/3b). The post-processing step will be similar to what we did there, so here’s a quick reminder of the actions we took: - We masked the start and end logits corresponding to tokens outside of the context. - We then converted the start and end logits into probabilities using a softmax. - We attributed a score to each `(start_token, end_token)` pair by taking the product of the corresponding two probabilities. - We looked for the pair with the maximum score that yielded a valid answer (e.g., a `start_token` lower than `end_token`). Here we will change this process slightly because we don’t need to compute actual scores (just the predicted answer). This means we can skip the softmax step. To go faster, we also won’t score all the possible `(start_token, end_token)` pairs, but only the ones corresponding to the highest `n_best` logits (with `n_best=20`). Since we will skip the softmax, those scores will be logit scores, and will be obtained by taking the sum of the start and end logits (instead of the product, because of the rule log⁡(ab)\=log⁡(a)+log⁡(b)\\log(ab) = \\log(a) + \\log(b)). To demonstrate all of this, we will need some kind of predictions. Since we have not trained our model yet, we are going to use the default model for the QA pipeline to generate some predictions on a small part of the validation set. We can use the same processing function as before; because it relies on the global constant `tokenizer`, we just have to change that object to the tokenizer of the model we want to use temporarily: ``` small_eval_set = raw_datasets["validation"].select(range(100)) trained_checkpoint = "distilbert-base-cased-distilled-squad" tokenizer = AutoTokenizer.from_pretrained(trained_checkpoint) eval_set = small_eval_set.map( preprocess_validation_examples, batched=True, remove_columns=raw_datasets["validation"].column_names, )``` Now that the preprocessing is done, we change the tokenizer back to the one we originally picked: ``` tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)``` We then remove the columns of our `eval_set` that are not expected by the model, build a batch with all of that small validation set, and pass it through the model. If a GPU is available, we use it to go faster: ``` import torch from transformers import AutoModelForQuestionAnswering eval_set_for_model = eval_set.remove_columns(["example_id", "offset_mapping"]) eval_set_for_model.set_format("torch") device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") batch = {k: eval_set_for_model[k].to(device) for k in eval_set_for_model.column_names} trained_model = AutoModelForQuestionAnswering.from_pretrained(trained_checkpoint).to( device ) with torch.no_grad(): outputs = trained_model(**batch)``` Since the `Trainer` will give us predictions as NumPy arrays, we grab the start and end logits and convert them to that format: ``` start_logits = outputs.start_logits.cpu().numpy() end_logits = outputs.end_logits.cpu().numpy()``` Now, we need to find the predicted answer for each example in our `small_eval_set`. One example may have been split into several features in `eval_set`, so the first step is to map each example in `small_eval_set` to the corresponding features in `eval_set`: ``` import collections example_to_features = collections.defaultdict(list) for idx, feature in enumerate(eval_set): example_to_features[feature["example_id"]].append(idx)``` With this in hand, we can really get to work by looping through all the examples and, for each example, through all the associated features. As we said before, we’ll look at the logit scores for the `n_best` start logits and end logits, excluding positions that give: - An answer that wouldn’t be inside the context - An answer with negative length - An answer that is too long (we limit the possibilities at `max_answer_length=30`) Once we have all the scored possible answers for one example, we just pick the one with the best logit score: ``` import numpy as np n_best = 20 max_answer_length = 30 predicted_answers = [] for example in small_eval_set: example_id = example["id"] context = example["context"] answers = [] for feature_index in example_to_features[example_id]: start_logit = start_logits[feature_index] end_logit = end_logits[feature_index] offsets = eval_set["offset_mapping"][feature_index] start_indexes = np.argsort(start_logit)[-1 : -n_best - 1 : -1].tolist() end_indexes = np.argsort(end_logit)[-1 : -n_best - 1 : -1].tolist() for start_index in start_indexes: for end_index in end_indexes: if offsets[start_index] is None or offsets[end_index] is None: continue if ( end_index < start_index or end_index - start_index + 1 > max_answer_length ): continue answers.append( { "text": context[offsets[start_index][0] : offsets[end_index][1]], "logit_score": start_logit[start_index] + end_logit[end_index], } ) best_answer = max(answers, key=lambda x: x["logit_score"]) predicted_answers.append({"id": example_id, "prediction_text": best_answer["text"]})``` The final format of the predicted answers is the one that will be expected by the metric we will use. As usual, we can load it with the help of the 🤗 Evaluate library: ``` import evaluate metric = evaluate.load("squad")``` This metric expects the predicted answers in the format we saw above (a list of dictionaries with one key for the ID of the example and one key for the predicted text) and the theoretical answers in the format below (a list of dictionaries with one key for the ID of the example and one key for the possible answers): ``` theoretical_answers = [ {"id": ex["id"], "answers": ex["answers"]} for ex in small_eval_set ]``` We can now check that we get sensible results by looking at the first element of both lists: ``` print(predicted_answers[0]) print(theoretical_answers[0])``` ``` {'id': '56be4db0acb8001400a502ec', 'prediction_text': 'Denver Broncos'} {'id': '56be4db0acb8001400a502ec', 'answers': {'text': ['Denver Broncos', 'Denver Broncos', 'Denver Broncos'], 'answer_start': [177, 177, 177]}}``` Not too bad! Now let’s have a look at the score the metric gives us: ``` metric.compute(predictions=predicted_answers, references=theoretical_answers)``` ``` {'exact_match': 83.0, 'f1': 88.25}``` Again, that’s rather good considering that according to [its paper](https://arxiv.org/abs/1910.01108v2) DistilBERT fine-tuned on SQuAD obtains 79.1 and 86.9 for those scores on the whole dataset. Now let’s put everything we just did in a `compute_metrics()` function that we will use in the `Trainer`. Normally, that `compute_metrics()` function only receives a tuple `eval_preds` with logits and labels. Here we will need a bit more, as we have to look in the dataset of features for the offset and in the dataset of examples for the original contexts, so we won’t be able to use this function to get regular evaluation results during training. We will only use it at the end of training to check the results. The `compute_metrics()` function groups the same steps as before; we just add a small check in case we don’t come up with any valid answers (in which case we predict an empty string). ``` from tqdm.auto import tqdm def compute_metrics(start_logits, end_logits, features, examples): example_to_features = collections.defaultdict(list) for idx, feature in enumerate(features): example_to_features[feature["example_id"]].append(idx) predicted_answers = [] for example in tqdm(examples): example_id = example["id"] context = example["context"] answers = [] for feature_index in example_to_features[example_id]: start_logit = start_logits[feature_index] end_logit = end_logits[feature_index] offsets = features[feature_index]["offset_mapping"] start_indexes = np.argsort(start_logit)[-1 : -n_best - 1 : -1].tolist() end_indexes = np.argsort(end_logit)[-1 : -n_best - 1 : -1].tolist() for start_index in start_indexes: for end_index in end_indexes: if offsets[start_index] is None or offsets[end_index] is None: continue if ( end_index < start_index or end_index - start_index + 1 > max_answer_length ): continue answer = { "text": context[offsets[start_index][0] : offsets[end_index][1]], "logit_score": start_logit[start_index] + end_logit[end_index], } answers.append(answer) if len(answers) > 0: best_answer = max(answers, key=lambda x: x["logit_score"]) predicted_answers.append( {"id": example_id, "prediction_text": best_answer["text"]} ) else: predicted_answers.append({"id": example_id, "prediction_text": ""}) theoretical_answers = [{"id": ex["id"], "answers": ex["answers"]} for ex in examples] return metric.compute(predictions=predicted_answers, references=theoretical_answers)``` We can check it works on our predictions: ``` compute_metrics(start_logits, end_logits, eval_set, small_eval_set)``` ``` {'exact_match': 83.0, 'f1': 88.25}``` Looking good! Now let’s use this to fine-tune our model. ### [](#fine-tuning-the-model)Fine-tuning the model We are now ready to train our model. Let’s create it first, using the `AutoModelForQuestionAnswering` class like before: ``` model = AutoModelForQuestionAnswering.from_pretrained(model_checkpoint)``` As usual, we get a warning that some weights are not used (the ones from the pretraining head) and some others are initialized randomly (the ones for the question answering head). You should be used to this by now, but that means this model is not ready to be used just yet and needs fine-tuning — good thing we’re about to do that! To be able to push our model to the Hub, we’ll need to log in to Hugging Face. If you’re running this code in a notebook, you can do so with the following utility function, which displays a widget where you can enter your login credentials: ``` from huggingface_hub import notebook_login notebook_login()``` If you aren’t working in a notebook, just type the following line in your terminal: Once this is done, we can define our `TrainingArguments`. As we said when we defined our function to compute the metric, we won’t be able to have a regular evaluation loop because of the signature of the `compute_metrics()` function. We could write our own subclass of `Trainer` to do this (an approach you can find in the [question answering example script](https://github.com/huggingface/transformers/blob/master/examples/pytorch/question-answering/trainer_qa.py)), but that’s a bit too long for this section. Instead, we will only evaluate the model at the end of training here and show you how to do a regular evaluation in “A custom training loop” below. This is really where the `Trainer` API shows its limits and the 🤗 Accelerate library shines: customizing the class to a specific use case can be painful, but tweaking a fully exposed training loop is easy. Let’s take a look at our `TrainingArguments`: ``` from transformers import TrainingArguments args = TrainingArguments( "bert-finetuned-squad", evaluation_strategy="no", save_strategy="epoch", learning_rate=2e-5, num_train_epochs=3, weight_decay=0.01, fp16=True, push_to_hub=True, )``` We’ve seen most of these before: we set some hyperparameters (like the learning rate, the number of epochs we train for, and some weight decay) and indicate that we want to save the model at the end of every epoch, skip evaluation, and upload our results to the Model Hub. We also enable mixed-precision training with `fp16=True`, as it can speed up the training nicely on a recent GPU. By default, the repository used will be in your namespace and named after the output directory you set, so in our case it will be in `"sgugger/bert-finetuned-squad"`. We can override this by passing a `hub_model_id`; for instance, to push the model to the `huggingface_course` organization we used `hub_model_id="huggingface_course/bert-finetuned-squad"` (which is the model we linked to at the beginning of this section). 💡 If the output directory you are using exists, it needs to be a local clone of the repository you want to push to (so set a new name if you get an error when defining your `Trainer`). Finally, we just pass everything to the `Trainer` class and launch the training: ``` from transformers import Trainer trainer = Trainer( model=model, args=args, train_dataset=train_dataset, eval_dataset=validation_dataset, tokenizer=tokenizer, ) trainer.train()``` Note that while the training happens, each time the model is saved (here, every epoch) it is uploaded to the Hub in the background. This way, you will be able to to resume your training on another machine if necessary. The whole training takes a while (a little over an hour on a Titan RTX), so you can grab a coffee or reread some of the parts of the course that you’ve found more challenging while it proceeds. Also note that as soon as the first epoch is finished, you will see some weights uploaded to the Hub and you can start playing with your model on its page. Once the training is complete, we can finally evaluate our model (and pray we didn’t spend all that compute time on nothing). The `predict()` method of the `Trainer` will return a tuple where the first elements will be the predictions of the model (here a pair with the start and end logits). We send this to our `compute_metrics()` function: ``` predictions, _, _ = trainer.predict(validation_dataset) start_logits, end_logits = predictions compute_metrics(start_logits, end_logits, validation_dataset, raw_datasets["validation"])``` ``` {'exact_match': 81.18259224219489, 'f1': 88.67381321905516}``` Great! As a comparison, the baseline scores reported in the BERT article for this model are 80.8 and 88.5, so we’re right where we should be. Finally, we use the `push_to_hub()` method to make sure we upload the latest version of the model: ``` trainer.push_to_hub(commit_message="Training complete")``` This returns the URL of the commit it just did, if you want to inspect it: ``` 'https://huggingface.co/sgugger/bert-finetuned-squad/commit/9dcee1fbc25946a6ed4bb32efb1bd71d5fa90b68'``` The `Trainer` also drafts a model card with all the evaluation results and uploads it. At this stage, you can use the inference widget on the Model Hub to test the model and share it with your friends, family, and favorite pets. You have successfully fine-tuned a model on a question answering task — congratulations! ✏️ **Your turn!** Try another model architecture to see if it performs better on this task! If you want to dive a bit more deeply into the training loop, we will now show you how to do the same thing using 🤗 Accelerate. ## [](#a-custom-training-loop)A custom training loop Let’s now have a look at the full training loop, so you can easily customize the parts you need. It will look a lot like the training loop in [Chapter 3](/course/chapter3/4), with the exception of the evaluation loop. We will be able to evaluate the model regularly since we’re not constrained by the `Trainer` class anymore. ### [](#preparing-everything-for-training)Preparing everything for training First we need to build the `DataLoader`s from our datasets. We set the format of those datasets to `"torch"`, and remove the columns in the validation set that are not used by the model. Then, we can use the `default_data_collator` provided by Transformers as a `collate_fn` and shuffle the training set, but not the validation set: ``` from torch.utils.data import DataLoader from transformers import default_data_collator train_dataset.set_format("torch") validation_set = validation_dataset.remove_columns(["example_id", "offset_mapping"]) validation_set.set_format("torch") train_dataloader = DataLoader( train_dataset, shuffle=True, collate_fn=default_data_collator, batch_size=8, ) eval_dataloader = DataLoader( validation_set, collate_fn=default_data_collator, batch_size=8 )``` Next we reinstantiate our model, to make sure we’re not continuing the fine-tuning from before but starting from the BERT pretrained model again: ``` model = AutoModelForQuestionAnswering.from_pretrained(model_checkpoint)``` Then we will need an optimizer. As usual we use the classic `AdamW`, which is like Adam, but with a fix in the way weight decay is applied: ``` from torch.optim import AdamW optimizer = AdamW(model.parameters(), lr=2e-5)``` Once we have all those objects, we can send them to the `accelerator.prepare()` method. Remember that if you want to train on TPUs in a Colab notebook, you will need to move all of this code into a training function, and that shouldn’t execute any cell that instantiates an `Accelerator`. We can force mixed-precision training by passing `fp16=True` to the `Accelerator` (or, if you are executing the code as a script, just make sure to fill in the 🤗 Accelerate `config` appropriately). ``` from accelerate import Accelerator accelerator = Accelerator(fp16=True) model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )``` As you should know from the previous sections, we can only use the `train_dataloader` length to compute the number of training steps after it has gone through the `accelerator.prepare()` method. We use the same linear schedule as in the previous sections: ``` from transformers import get_scheduler num_train_epochs = 3 num_update_steps_per_epoch = len(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps, )``` To push our model to the Hub, we will need to create a `Repository` object in a working folder. First log in to the Hugging Face Hub, if you’re not logged in already. We’ll determine the repository name from the model ID we want to give our model (feel free to replace the `repo_name` with your own choice; it just needs to contain your username, which is what the function `get_full_repo_name()` does): ``` from huggingface_hub import Repository, get_full_repo_name model_name = "bert-finetuned-squad-accelerate" repo_name = get_full_repo_name(model_name) repo_name``` ``` 'sgugger/bert-finetuned-squad-accelerate'``` Then we can clone that repository in a local folder. If it already exists, this local folder should be a clone of the repository we are working with: ``` output_dir = "bert-finetuned-squad-accelerate" repo = Repository(output_dir, clone_from=repo_name)``` We can now upload anything we save in `output_dir` by calling the `repo.push_to_hub()` method. This will help us upload the intermediate models at the end of each epoch. ## [](#training-loop)Training loop We are now ready to write the full training loop. After defining a progress bar to follow how training goes, the loop has three parts: - The training in itself, which is the classic iteration over the `train_dataloader`, forward pass through the model, then backward pass and optimizer step. - The evaluation, in which we gather all the values for `start_logits` and `end_logits` before converting them to NumPy arrays. Once the evaluation loop is finished, we concatenate all the results. Note that we need to truncate because the `Accelerator` may have added a few samples at the end to ensure we have the same number of examples in each process. - Saving and uploading, where we first save the model and the tokenizer, then call `repo.push_to_hub()`. As we did before, we use the argument `blocking=False` to tell the 🤗 Hub library to push in an asynchronous process. This way, training continues normally and this (long) instruction is executed in the background. Here’s the complete code for the training loop: ``` from tqdm.auto import tqdm import torch progress_bar = tqdm(range(num_training_steps)) for epoch in range(num_train_epochs): model.train() for step, batch in enumerate(train_dataloader): outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1) model.eval() start_logits = [] end_logits = [] accelerator.print("Evaluation!") for batch in tqdm(eval_dataloader): with torch.no_grad(): outputs = model(**batch) start_logits.append(accelerator.gather(outputs.start_logits).cpu().numpy()) end_logits.append(accelerator.gather(outputs.end_logits).cpu().numpy()) start_logits = np.concatenate(start_logits) end_logits = np.concatenate(end_logits) start_logits = start_logits[: len(validation_dataset)] end_logits = end_logits[: len(validation_dataset)] metrics = compute_metrics( start_logits, end_logits, validation_dataset, raw_datasets["validation"] ) print(f"epoch {epoch}:", metrics) accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) if accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=f"Training in progress epoch {epoch}", blocking=False )``` In case this is the first time you’re seeing a model saved with 🤗 Accelerate, let’s take a moment to inspect the three lines of code that go with it: ``` accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save)``` The first line is self-explanatory: it tells all the processes to wait until everyone is at that stage before continuing. This is to make sure we have the same model in every process before saving. Then we grab the `unwrapped_model`, which is the base model we defined. The `accelerator.prepare()` method changes the model to work in distributed training, so it won’t have the `save_pretrained()` method anymore; the `accelerator.unwrap_model()` method undoes that step. Lastly, we call `save_pretrained()` but tell that method to use `accelerator.save()` instead of `torch.save()`. Once this is done, you should have a model that produces results pretty similar to the one trained with the `Trainer`. You can check the model we trained using this code at [_huggingface-course/bert-finetuned-squad-accelerate_](https://huggingface.co/huggingface-course/bert-finetuned-squad-accelerate). And if you want to test out any tweaks to the training loop, you can directly implement them by editing the code shown above! ## [](#using-the-fine-tuned-model)Using the fine-tuned model We’ve already shown you how you can use the model we fine-tuned on the Model Hub with the inference widget. To use it locally in a `pipeline`, you just have to specify the model identifier: ``` from transformers import pipeline model_checkpoint = "huggingface-course/bert-finetuned-squad" question_answerer = pipeline("question-answering", model=model_checkpoint) context = """ 🤗 Transformers is backed by the three most popular deep learning libraries — Jax, PyTorch and TensorFlow — with a seamless integration between them. It's straightforward to train your models with one before loading them for inference with the other. """ question = "Which deep learning libraries back 🤗 Transformers?" question_answerer(question=question, context=context)``` ``` {'score': 0.9979003071784973, 'start': 78, 'end': 105, 'answer': 'Jax, PyTorch and TensorFlow'}``` Great! Our model is working as well as the default one for this pipeline!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Question answering - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter7/7"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Question answering - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;question-answering&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;preparing-the-data&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;the-squad-dataset&quot;,&quot;title&quot;:&quot;The SQuAD dataset&quot;},{&quot;local&quot;:&quot;processing-the-training-data&quot;,&quot;title&quot;:&quot;Processing the training data&quot;},{&quot;local&quot;:&quot;processing-the-validation-data&quot;,&quot;title&quot;:&quot;Processing the validation data&quot;}],&quot;title&quot;:&quot;Preparing the data&quot;},{&quot;local&quot;:&quot;fine-tuning-the-model-with-the-trainer-api&quot;,&quot;title&quot;:&quot;Fine-tuning the model with the `Trainer` API&quot;},{&quot;local&quot;:&quot;fine-tuning-the-model-with-keras&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;post-processing&quot;,&quot;title&quot;:&quot;Post-processing&quot;},{&quot;local&quot;:&quot;fine-tuning-the-model&quot;,&quot;title&quot;:&quot;Fine-tuning the model&quot;}],&quot;title&quot;:&quot;Fine-tuning the model with Keras&quot;},{&quot;local&quot;:&quot;a-custom-training-loop&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;preparing-everything-for-training&quot;,&quot;title&quot;:&quot;Preparing everything for training&quot;}],&quot;title&quot;:&quot;A custom training loop&quot;},{&quot;local&quot;:&quot;training-loop&quot;,&quot;title&quot;:&quot;Training loop&quot;},{&quot;local&quot;:&quot;using-the-fine-tuned-model&quot;,&quot;title&quot;:&quot;Using the fine-tuned model&quot;}],&quot;title&quot;:&quot;Question answering&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter7/7&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Question answering&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Question answering</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/2?fw=pt">Token classification </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/3?fw=pt">Fine-tuning a masked language model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/4?fw=pt">Translation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/5?fw=pt">Summarization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/6?fw=pt">Training a causal language model from scratch </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter7/7?fw=pt">Question answering </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/8?fw=pt">Mastering NLP </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="question-answering" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#question-answering"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Question answering</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-7-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter7/section7_pt.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter7/section7_pt.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>Time to look at question answering! This task comes in many flavors, but the one we’ll focus on in this section is called <em>extractive</em> question answering. This involves posing questions about a document and identifying the answers as <em>spans of text</em> in the document itself.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/ajPx5LwJD-I" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>We will fine-tune a BERT model on the <a href="https://rajpurkar.github.io/SQuAD-explorer/" rel="nofollow">SQuAD dataset</a>, which consists of questions posed by crowdworkers on a set of Wikipedia articles. This will give us a model able to compute predictions like this one:</p> <iframe src="https://course-demos-bert-finetuned-squad.hf.space" frameborder="0" height="450" title="Gradio app" class="block dark:hidden container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>This is actually showcasing the model that was trained and uploaded to the Hub using the code shown in this section. You can find it and double-check the predictions <a href="https://huggingface.co/huggingface-course/bert-finetuned-squad?context=%F0%9F%A4%97+Transformers+is+backed+by+the+three+most+popular+deep+learning+libraries+%E2%80%94+Jax%2C+PyTorch+and+TensorFlow+%E2%80%94+with+a+seamless+integration+between+them.+It%27s+straightforward+to+train+your+models+with+one+before+loading+them+for+inference+with+the+other.&amp;question=Which+deep+learning+libraries+back+%F0%9F%A4%97+Transformers%3F" rel="nofollow">here</a>.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 Encoder-only models like BERT tend to be great at extracting answers to factoid questions like “Who invented the Transformer architecture?” but fare poorly when given open-ended questions like “Why is the sky blue?” In these more challenging cases, encoder-decoder models like T5 and BART are typically used to synthesize the information in a way that’s quite similar to <a href="/course/chapter7/5">text summarization</a>. If you’re interested in this type of <em>generative</em> question answering, we recommend checking out our <a href="https://yjernite.github.io/lfqa.html" rel="nofollow">demo</a> based on the <a href="https://huggingface.co/datasets/eli5" rel="nofollow">ELI5 dataset</a>.</p></div> <h2 class="relative group"><a id="preparing-the-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preparing-the-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preparing the data</span></h2> <p>The dataset that is used the most as an academic benchmark for extractive question answering is <a href="https://rajpurkar.github.io/SQuAD-explorer/" rel="nofollow">SQuAD</a>, so that’s the one we’ll use here. There is also a harder <a href="https://huggingface.co/datasets/squad_v2" rel="nofollow">SQuAD v2</a> benchmark, which includes questions that don’t have an answer. As long as your own dataset contains a column for contexts, a column for questions, and a column for answers, you should be able to adapt the steps below.</p> <h3 class="relative group"><a id="the-squad-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-squad-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The SQuAD dataset</span></h3> <p>As usual, we can download and cache the dataset in just one step thanks to <code>load_dataset()</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset raw_datasets = load_dataset(<span class="hljs-string">"squad"</span>)</pre></div> <p>We can then have a look at this object to learn more about the SQuAD dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>raw_datasets</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>DatasetDict({ train: Dataset({ features: [<span class="hljs-string">'id'</span>, <span class="hljs-string">'title'</span>, <span class="hljs-string">'context'</span>, <span class="hljs-string">'question'</span>, <span class="hljs-string">'answers'</span>], num_rows: <span class="hljs-number">87599</span> }) validation: Dataset({ features: [<span class="hljs-string">'id'</span>, <span class="hljs-string">'title'</span>, <span class="hljs-string">'context'</span>, <span class="hljs-string">'question'</span>, <span class="hljs-string">'answers'</span>], num_rows: <span class="hljs-number">10570</span> }) })</pre></div> <p>It looks like we have everything we need with the <code>context</code>, <code>question</code>, and <code>answers</code> fields, so let’s print those for the first element of our training set:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(<span class="hljs-string">"Context: "</span>, raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"context"</span>]) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Question: "</span>, raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"question"</span>]) <span class="hljs-built_in">print</span>(<span class="hljs-string">"Answer: "</span>, raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"answers"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Context: <span class="hljs-string">'Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.'</span> Question: <span class="hljs-string">'To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?'</span> Answer: {<span class="hljs-string">'text'</span>: [<span class="hljs-string">'Saint Bernadette Soubirous'</span>], <span class="hljs-string">'answer_start'</span>: [<span class="hljs-number">515</span>]}</pre></div> <p>The <code>context</code> and <code>question</code> fields are very straightforward to use. The <code>answers</code> field is a bit trickier as it comports a dictionary with two fields that are both lists. This is the format that will be expected by the <code>squad</code> metric during evaluation; if you are using your own data, you don’t necessarily need to worry about putting the answers in the same format. The <code>text</code> field is rather obvious, and the <code>answer_start</code> field contains the starting character index of each answer in the context.</p> <p>During training, there is only one possible answer. We can double-check this by using the <code>Dataset.filter()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>raw_datasets[<span class="hljs-string">"train"</span>].<span class="hljs-built_in">filter</span>(<span class="hljs-keyword">lambda</span> x: <span class="hljs-built_in">len</span>(x[<span class="hljs-string">"answers"</span>][<span class="hljs-string">"text"</span>]) != <span class="hljs-number">1</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Dataset({ features: [<span class="hljs-string">'id'</span>, <span class="hljs-string">'title'</span>, <span class="hljs-string">'context'</span>, <span class="hljs-string">'question'</span>, <span class="hljs-string">'answers'</span>], num_rows: <span class="hljs-number">0</span> })</pre></div> <p>For evaluation, however, there are several possible answers for each sample, which may be the same or different:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(raw_datasets[<span class="hljs-string">"validation"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"answers"</span>]) <span class="hljs-built_in">print</span>(raw_datasets[<span class="hljs-string">"validation"</span>][<span class="hljs-number">2</span>][<span class="hljs-string">"answers"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'text'</span>: [<span class="hljs-string">'Denver Broncos'</span>, <span class="hljs-string">'Denver Broncos'</span>, <span class="hljs-string">'Denver Broncos'</span>], <span class="hljs-string">'answer_start'</span>: [<span class="hljs-number">177</span>, <span class="hljs-number">177</span>, <span class="hljs-number">177</span>]} {<span class="hljs-string">'text'</span>: [<span class="hljs-string">'Santa Clara, California'</span>, <span class="hljs-string">"Levi's Stadium"</span>, <span class="hljs-string">"Levi's Stadium in the San Francisco Bay Area at Santa Clara, California."</span>], <span class="hljs-string">'answer_start'</span>: [<span class="hljs-number">403</span>, <span class="hljs-number">355</span>, <span class="hljs-number">355</span>]}</pre></div> <p>We won’t dive into the evaluation script as it will all be wrapped up by a 🤗 Datasets metric for us, but the short version is that some of the questions have several possible answers, and this script will compare a predicted answer to all the acceptable answers and take the best score. If we take a look at the sample at index 2, for instance:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(raw_datasets[<span class="hljs-string">"validation"</span>][<span class="hljs-number">2</span>][<span class="hljs-string">"context"</span>]) <span class="hljs-built_in">print</span>(raw_datasets[<span class="hljs-string">"validation"</span>][<span class="hljs-number">2</span>][<span class="hljs-string">"question"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'Super Bowl 50 was an American football game to determine the champion of the National Football League (NFL) for the 2015 season. The American Football Conference (AFC) champion Denver Broncos defeated the National Football Conference (NFC) champion Carolina Panthers 24–10 to earn their third Super Bowl title. The game was played on February 7, 2016, at Levi\'s Stadium in the San Francisco Bay Area at Santa Clara, California. As this was the 50th Super Bowl, the league emphasized the "golden anniversary" with various gold-themed initiatives, as well as temporarily suspending the tradition of naming each Super Bowl game with Roman numerals (under which the game would have been known as "Super Bowl L"), so that the logo could prominently feature the Arabic numerals 50.'</span> <span class="hljs-string">'Where did Super Bowl 50 take place?'</span></pre></div> <p>we can see that the answer can indeed be one of the three possibilities we saw before.</p> <h3 class="relative group"><a id="processing-the-training-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#processing-the-training-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Processing the training data</span></h3> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/qgaM0weJHpA" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Let’s start with preprocessing the training data. The hard part will be to generate labels for the question’s answer, which will be the start and end positions of the tokens corresponding to the answer inside the context.</p> <p>But let’s not get ahead of ourselves. First, we need to convert the text in the input into IDs the model can make sense of, using a tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer model_checkpoint = <span class="hljs-string">"bert-base-cased"</span> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)</pre></div> <p>As mentioned previously, we’ll be fine-tuning a BERT model, but you can use any other model type as long as it has a fast tokenizer implemented. You can see all the architectures that come with a fast version in <a href="https://huggingface.co/transformers/#supported-frameworks" rel="nofollow">this big table</a>, and to check that the <code>tokenizer</code> object you’re using is indeed backed by 🤗 Tokenizers you can look at its <code>is_fast</code> attribute:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.is_fast</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-literal">True</span></pre></div> <p>We can pass to our tokenizer the question and the context together, and it will properly insert the special tokens to form a sentence like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-selector-attr">[CLS]</span> question <span class="hljs-selector-attr">[SEP]</span> context <span class="hljs-selector-attr">[SEP]</span></pre></div> <p>Let’s double-check:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>context = raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"context"</span>] question = raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">0</span>][<span class="hljs-string">"question"</span>] inputs = tokenizer(question, context) tokenizer.decode(inputs[<span class="hljs-string">"input_ids"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'[CLS] To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France? [SEP] Architecturally, '</span> <span class="hljs-string">'the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin '</span> <span class="hljs-string">'Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms '</span> <span class="hljs-string">'upraised with the legend " Venite Ad Me Omnes ". Next to the Main Building is the Basilica of the Sacred '</span> <span class="hljs-string">'Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a '</span> <span class="hljs-string">'replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette '</span> <span class="hljs-string">'Soubirous in 1858. At the end of the main drive ( and in a direct line that connects through 3 statues '</span> <span class="hljs-string">'and the Gold Dome ), is a simple, modern stone statue of Mary. [SEP]'</span></pre></div> <p>The labels will then be the index of the tokens starting and ending the answer, and the model will be tasked to predicted one start and end logit per token in the input, with the theoretical labels being as follow:</p> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/qa_labels.svg" alt="One-hot encoded labels for question answering."> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter7/qa_labels-dark.svg" alt="One-hot encoded labels for question answering."></div> <p>In this case the context is not too long, but some of the examples in the dataset have very long contexts that will exceed the maximum length we set (which is 384 in this case). As we saw in <a href="/course/chapter6/4">Chapter 6</a> when we explored the internals of the <code>question-answering</code> pipeline, we will deal with long contexts by creating several training features from one sample of our dataset, with a sliding window between them.</p> <p>To see how this works using the current example, we can limit the length to 100 and use a sliding window of 50 tokens. As a reminder, we use:</p> <ul><li><code>max_length</code> to set the maximum length (here 100)</li> <li><code>truncation="only_second"</code> to truncate the context (which is in the second position) when the question with its context is too long</li> <li><code>stride</code> to set the number of overlapping tokens between two successive chunks (here 50)</li> <li><code>return_overflowing_tokens=True</code> to let the tokenizer know we want the overflowing tokens</li></ul> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs = tokenizer( question, context, max_length=<span class="hljs-number">100</span>, truncation=<span class="hljs-string">"only_second"</span>, stride=<span class="hljs-number">50</span>, return_overflowing_tokens=<span class="hljs-literal">True</span>, ) <span class="hljs-keyword">for</span> ids <span class="hljs-keyword">in</span> inputs[<span class="hljs-string">"input_ids"</span>]: <span class="hljs-built_in">print</span>(tokenizer.decode(ids))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'[CLS] To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France? [SEP] Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend " Venite Ad Me Omnes ". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basi [SEP]'</span> <span class="hljs-string">'[CLS] To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France? [SEP] the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend " Venite Ad Me Omnes ". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin [SEP]'</span> <span class="hljs-string">'[CLS] To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France? [SEP] Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive ( and in a direct line that connects through 3 [SEP]'</span> <span class="hljs-string">'[CLS] To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France? [SEP]. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive ( and in a direct line that connects through 3 statues and the Gold Dome ), is a simple, modern stone statue of Mary. [SEP]'</span></pre></div> <p>As we can see, our example has been in split into four inputs, each of them containing the question and some part of the context. Note that the answer to the question (“Bernadette Soubirous”) only appears in the third and last inputs, so by dealing with long contexts in this way we will create some training examples where the answer is not included in the context. For those examples, the labels will be <code>start_position = end_position = 0</code> (so we predict the <code>[CLS]</code> token). We will also set those labels in the unfortunate case where the answer has been truncated so that we only have the start (or end) of it. For the examples where the answer is fully in the context, the labels will be the index of the token where the answer starts and the index of the token where the answer ends.</p> <p>The dataset provides us with the start character of the answer in the context, and by adding the length of the answer, we can find the end character in the context. To map those to token indices, we will need to use the offset mappings we studied in <a href="/course/chapter6/4">Chapter 6</a>. We can have our tokenizer return these by passing along <code>return_offsets_mapping=True</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs = tokenizer( question, context, max_length=<span class="hljs-number">100</span>, truncation=<span class="hljs-string">"only_second"</span>, stride=<span class="hljs-number">50</span>, return_overflowing_tokens=<span class="hljs-literal">True</span>, return_offsets_mapping=<span class="hljs-literal">True</span>, ) inputs.keys()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>dict_keys([<span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'token_type_ids'</span>, <span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'offset_mapping'</span>, <span class="hljs-string">'overflow_to_sample_mapping'</span>])</pre></div> <p>As we can see, we get back the usual input IDs, token type IDs, and attention mask, as well as the offset mapping we required and an extra key, <code>overflow_to_sample_mapping</code>. The corresponding value will be of use to us when we tokenize several texts at the same time (which we should do to benefit from the fact that our tokenizer is backed by Rust). Since one sample can give several features, it maps each feature to the example it originated from. Because here we only tokenized one example, we get a list of <code>0</code>s:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs[<span class="hljs-string">"overflow_to_sample_mapping"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]</pre></div> <p>But if we tokenize more examples, this will become more useful:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs = tokenizer( raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">2</span>:<span class="hljs-number">6</span>][<span class="hljs-string">"question"</span>], raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">2</span>:<span class="hljs-number">6</span>][<span class="hljs-string">"context"</span>], max_length=<span class="hljs-number">100</span>, truncation=<span class="hljs-string">"only_second"</span>, stride=<span class="hljs-number">50</span>, return_overflowing_tokens=<span class="hljs-literal">True</span>, return_offsets_mapping=<span class="hljs-literal">True</span>, ) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"The 4 examples gave <span class="hljs-subst">{<span class="hljs-built_in">len</span>(inputs[<span class="hljs-string">'input_ids'</span>])}</span> features."</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Here is where each comes from: <span class="hljs-subst">{inputs[<span class="hljs-string">'overflow_to_sample_mapping'</span>]}</span>."</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'The 4 examples gave 19 features.'</span> <span class="hljs-string">'Here is where each comes from: [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3].'</span></pre></div> <p>As we can see, the first three examples (at indices 2, 3, and 4 in the training set) each gave four features and the last example (at index 5 in the training set) gave 7 features.</p> <p>This information will be useful to map each feature we get to its corresponding label. As mentioned earlier, those labels are:</p> <ul><li><code>(0, 0)</code> if the answer is not in the corresponding span of the context</li> <li><code>(start_position, end_position)</code> if the answer is in the corresponding span of the context, with <code>start_position</code> being the index of the token (in the input IDs) at the start of the answer and <code>end_position</code> being the index of the token (in the input IDs) where the answer ends</li></ul> <p>To determine which of these is the case and, if relevant, the positions of the tokens, we first find the indices that start and end the context in the input IDs. We could use the token type IDs to do this, but since those do not necessarily exist for all models (DistilBERT does not require them, for instance), we’ll instead use the <code>sequence_ids()</code> method of the <code>BatchEncoding</code> our tokenizer returns.</p> <p>Once we have those token indices, we look at the corresponding offsets, which are tuples of two integers representing the span of characters inside the original context. We can thus detect if the chunk of the context in this feature starts after the answer or ends before the answer begins (in which case the label is <code>(0, 0)</code>). If that’s not the case, we loop to find the first and last token of the answer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>answers = raw_datasets[<span class="hljs-string">"train"</span>][<span class="hljs-number">2</span>:<span class="hljs-number">6</span>][<span class="hljs-string">"answers"</span>] start_positions = [] end_positions = [] <span class="hljs-keyword">for</span> i, offset <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(inputs[<span class="hljs-string">"offset_mapping"</span>]): sample_idx = inputs[<span class="hljs-string">"overflow_to_sample_mapping"</span>][i] answer = answers[sample_idx] start_char = answer[<span class="hljs-string">"answer_start"</span>][<span class="hljs-number">0</span>] end_char = answer[<span class="hljs-string">"answer_start"</span>][<span class="hljs-number">0</span>] + <span class="hljs-built_in">len</span>(answer[<span class="hljs-string">"text"</span>][<span class="hljs-number">0</span>]) sequence_ids = inputs.sequence_ids(i) <span class="hljs-comment"># Find the start and end of the context</span> idx = <span class="hljs-number">0</span> <span class="hljs-keyword">while</span> sequence_ids[idx] != <span class="hljs-number">1</span>: idx += <span class="hljs-number">1</span> context_start = idx <span class="hljs-keyword">while</span> sequence_ids[idx] == <span class="hljs-number">1</span>: idx += <span class="hljs-number">1</span> context_end = idx - <span class="hljs-number">1</span> <span class="hljs-comment"># If the answer is not fully inside the context, label is (0, 0)</span> <span class="hljs-keyword">if</span> offset[context_start][<span class="hljs-number">0</span>] &gt; start_char <span class="hljs-keyword">or</span> offset[context_end][<span class="hljs-number">1</span>] &lt; end_char: start_positions.append(<span class="hljs-number">0</span>) end_positions.append(<span class="hljs-number">0</span>) <span class="hljs-keyword">else</span>: <span class="hljs-comment"># Otherwise it's the start and end token positions</span> idx = context_start <span class="hljs-keyword">while</span> idx &lt;= context_end <span class="hljs-keyword">and</span> offset[idx][<span class="hljs-number">0</span>] &lt;= start_char: idx += <span class="hljs-number">1</span> start_positions.append(idx - <span class="hljs-number">1</span>) idx = context_end <span class="hljs-keyword">while</span> idx &gt;= context_start <span class="hljs-keyword">and</span> offset[idx][<span class="hljs-number">1</span>] &gt;= end_char: idx -= <span class="hljs-number">1</span> end_positions.append(idx + <span class="hljs-number">1</span>) start_positions, end_positions</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>([<span class="hljs-number">83</span>, <span class="hljs-number">51</span>, <span class="hljs-number">19</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">64</span>, <span class="hljs-number">27</span>, <span class="hljs-number">0</span>, <span class="hljs-number">34</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">67</span>, <span class="hljs-number">34</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">85</span>, <span class="hljs-number">53</span>, <span class="hljs-number">21</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">70</span>, <span class="hljs-number">33</span>, <span class="hljs-number">0</span>, <span class="hljs-number">40</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">68</span>, <span class="hljs-number">35</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>])</pre></div> <p>Let’s take a look at a few results to verify that our approach is correct. For the first feature we find <code>(83, 85)</code> as labels, so let’s compare the theoretical answer with the decoded span of tokens from 83 to 85 (inclusive):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>idx = <span class="hljs-number">0</span> sample_idx = inputs[<span class="hljs-string">"overflow_to_sample_mapping"</span>][idx] answer = answers[sample_idx][<span class="hljs-string">"text"</span>][<span class="hljs-number">0</span>] start = start_positions[idx] end = end_positions[idx] labeled_answer = tokenizer.decode(inputs[<span class="hljs-string">"input_ids"</span>][idx][start : end + <span class="hljs-number">1</span>]) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Theoretical answer: <span class="hljs-subst">{answer}</span>, labels give: <span class="hljs-subst">{labeled_answer}</span>"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'Theoretical answer: the Main Building, labels give: the Main Building'</span></pre></div> <p>So that’s a match! Now let’s check index 4, where we set the labels to <code>(0, 0)</code>, which means the answer is not in the context chunk of that feature:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>idx = <span class="hljs-number">4</span> sample_idx = inputs[<span class="hljs-string">"overflow_to_sample_mapping"</span>][idx] answer = answers[sample_idx][<span class="hljs-string">"text"</span>][<span class="hljs-number">0</span>] decoded_example = tokenizer.decode(inputs[<span class="hljs-string">"input_ids"</span>][idx]) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Theoretical answer: <span class="hljs-subst">{answer}</span>, decoded example: <span class="hljs-subst">{decoded_example}</span>"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'Theoretical answer: a Marian place of prayer and reflection, decoded example: [CLS] What is the Grotto at Notre Dame? [SEP] Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend " Venite Ad Me Omnes ". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grot [SEP]'</span></pre></div> <p>Indeed, we don’t see the answer inside the context.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Your turn!</strong> When using the XLNet architecture, padding is applied on the left and the question and context are switched. Adapt all the code we just saw to the XLNet architecture (and add <code>padding=True</code>). Be aware that the <code>[CLS]</code> token may not be at the 0 position with padding applied.</p></div> <p>Now that we have seen step by step how to preprocess our training data, we can group it in a function we will apply on the whole training dataset. We’ll pad every feature to the maximum length we set, as most of the contexts will be long (and the corresponding samples will be split into several features), so there is no real benefit to applying dynamic padding here:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>max_length = <span class="hljs-number">384</span> stride = <span class="hljs-number">128</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_training_examples</span>(<span class="hljs-params">examples</span>): questions = [q.strip() <span class="hljs-keyword">for</span> q <span class="hljs-keyword">in</span> examples[<span class="hljs-string">"question"</span>]] inputs = tokenizer( questions, examples[<span class="hljs-string">"context"</span>], max_length=max_length, truncation=<span class="hljs-string">"only_second"</span>, stride=stride, return_overflowing_tokens=<span class="hljs-literal">True</span>, return_offsets_mapping=<span class="hljs-literal">True</span>, padding=<span class="hljs-string">"max_length"</span>, ) offset_mapping = inputs.pop(<span class="hljs-string">"offset_mapping"</span>) sample_map = inputs.pop(<span class="hljs-string">"overflow_to_sample_mapping"</span>) answers = examples[<span class="hljs-string">"answers"</span>] start_positions = [] end_positions = [] <span class="hljs-keyword">for</span> i, offset <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(offset_mapping): sample_idx = sample_map[i] answer = answers[sample_idx] start_char = answer[<span class="hljs-string">"answer_start"</span>][<span class="hljs-number">0</span>] end_char = answer[<span class="hljs-string">"answer_start"</span>][<span class="hljs-number">0</span>] + <span class="hljs-built_in">len</span>(answer[<span class="hljs-string">"text"</span>][<span class="hljs-number">0</span>]) sequence_ids = inputs.sequence_ids(i) <span class="hljs-comment"># Find the start and end of the context</span> idx = <span class="hljs-number">0</span> <span class="hljs-keyword">while</span> sequence_ids[idx] != <span class="hljs-number">1</span>: idx += <span class="hljs-number">1</span> context_start = idx <span class="hljs-keyword">while</span> sequence_ids[idx] == <span class="hljs-number">1</span>: idx += <span class="hljs-number">1</span> context_end = idx - <span class="hljs-number">1</span> <span class="hljs-comment"># If the answer is not fully inside the context, label is (0, 0)</span> <span class="hljs-keyword">if</span> offset[context_start][<span class="hljs-number">0</span>] &gt; start_char <span class="hljs-keyword">or</span> offset[context_end][<span class="hljs-number">1</span>] &lt; end_char: start_positions.append(<span class="hljs-number">0</span>) end_positions.append(<span class="hljs-number">0</span>) <span class="hljs-keyword">else</span>: <span class="hljs-comment"># Otherwise it's the start and end token positions</span> idx = context_start <span class="hljs-keyword">while</span> idx &lt;= context_end <span class="hljs-keyword">and</span> offset[idx][<span class="hljs-number">0</span>] &lt;= start_char: idx += <span class="hljs-number">1</span> start_positions.append(idx - <span class="hljs-number">1</span>) idx = context_end <span class="hljs-keyword">while</span> idx &gt;= context_start <span class="hljs-keyword">and</span> offset[idx][<span class="hljs-number">1</span>] &gt;= end_char: idx -= <span class="hljs-number">1</span> end_positions.append(idx + <span class="hljs-number">1</span>) inputs[<span class="hljs-string">"start_positions"</span>] = start_positions inputs[<span class="hljs-string">"end_positions"</span>] = end_positions <span class="hljs-keyword">return</span> inputs</pre></div> <p>Note that we defined two constants to determine the maximum length used as well as the length of the sliding window, and that we added a tiny bit of cleanup before tokenizing: some of the questions in the SQuAD dataset have extra spaces at the beginning and the end that don’t add anything (and take up space when being tokenized if you use a model like RoBERTa), so we removed those extra spaces.</p> <p>To apply this function to the whole training set, we use the <code>Dataset.map()</code> method with the <code>batched=True</code> flag. It’s necessary here as we are changing the length of the dataset (since one example can give several training features):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>train_dataset = raw_datasets[<span class="hljs-string">"train"</span>].<span class="hljs-built_in">map</span>( preprocess_training_examples, batched=<span class="hljs-literal">True</span>, remove_columns=raw_datasets[<span class="hljs-string">"train"</span>].column_names, ) <span class="hljs-built_in">len</span>(raw_datasets[<span class="hljs-string">"train"</span>]), <span class="hljs-built_in">len</span>(train_dataset)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-number">87599</span>, <span class="hljs-number">88729</span>)</pre></div> <p>As we can see, the preprocessing added roughly 1,000 features. Our training set is now ready to be used — let’s dig into the preprocessing of the validation set!</p> <h3 class="relative group"><a id="processing-the-validation-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#processing-the-validation-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Processing the validation data</span></h3> <p>Preprocessing the validation data will be slightly easier as we don’t need to generate labels (unless we want to compute a validation loss, but that number won’t really help us understand how good the model is). The real joy will be to interpret the predictions of the model into spans of the original context. For this, we will just need to store both the offset mappings and some way to match each created feature to the original example it comes from. Since there is an ID column in the original dataset, we’ll use that ID.</p> <p>The only thing we’ll add here is a tiny bit of cleanup of the offset mappings. They will contain offsets for the question and the context, but once we’re in the post-processing stage we won’t have any way to know which part of the input IDs corresponded to the context and which part was the question (the <code>sequence_ids()</code> method we used is available for the output of the tokenizer only). So, we’ll set the offsets corresponding to the question to <code>None</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_validation_examples</span>(<span class="hljs-params">examples</span>): questions = [q.strip() <span class="hljs-keyword">for</span> q <span class="hljs-keyword">in</span> examples[<span class="hljs-string">"question"</span>]] inputs = tokenizer( questions, examples[<span class="hljs-string">"context"</span>], max_length=max_length, truncation=<span class="hljs-string">"only_second"</span>, stride=stride, return_overflowing_tokens=<span class="hljs-literal">True</span>, return_offsets_mapping=<span class="hljs-literal">True</span>, padding=<span class="hljs-string">"max_length"</span>, ) sample_map = inputs.pop(<span class="hljs-string">"overflow_to_sample_mapping"</span>) example_ids = [] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(inputs[<span class="hljs-string">"input_ids"</span>])): sample_idx = sample_map[i] example_ids.append(examples[<span class="hljs-string">"id"</span>][sample_idx]) sequence_ids = inputs.sequence_ids(i) offset = inputs[<span class="hljs-string">"offset_mapping"</span>][i] inputs[<span class="hljs-string">"offset_mapping"</span>][i] = [ o <span class="hljs-keyword">if</span> sequence_ids[k] == <span class="hljs-number">1</span> <span class="hljs-keyword">else</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">for</span> k, o <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(offset) ] inputs[<span class="hljs-string">"example_id"</span>] = example_ids <span class="hljs-keyword">return</span> inputs</pre></div> <p>We can apply this function on the whole validation dataset like before:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>validation_dataset = raw_datasets[<span class="hljs-string">"validation"</span>].<span class="hljs-built_in">map</span>( preprocess_validation_examples, batched=<span class="hljs-literal">True</span>, remove_columns=raw_datasets[<span class="hljs-string">"validation"</span>].column_names, ) <span class="hljs-built_in">len</span>(raw_datasets[<span class="hljs-string">"validation"</span>]), <span class="hljs-built_in">len</span>(validation_dataset)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>(<span class="hljs-number">10570</span>, <span class="hljs-number">10822</span>)</pre></div> <p>In this case we’ve only added a couple of hundred samples, so it appears the contexts in the validation dataset are a bit shorter.</p> <p>Now that we have preprocessed all the data, we can get to the training.</p> <h2 class="relative group"><a id="fine-tuning-the-model-with-the-trainer-api" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-the-model-with-the-trainer-api"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning the model with the <code>Trainer</code> API</span></h2> <p>The training code for this example will look a lot like the code in the previous sections — the hardest thing will be to write the <code>compute_metrics()</code> function. Since we padded all the samples to the maximum length we set, there is no data collator to define, so this metric computation is really the only thing we have to worry about. The difficult part will be to post-process the model predictions into spans of text in the original examples; once we have done that, the metric from the 🤗 Datasets library will do most of the work for us.</p> <h3 class="relative group"><a id="post-processing" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#post-processing"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Post-processing</span></h3> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/BNy08iIWVJM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The model will output logits for the start and end positions of the answer in the input IDs, as we saw during our exploration of the <a href="/course/chapter6/3b"><code>question-answering</code> pipeline</a>. The post-processing step will be similar to what we did there, so here’s a quick reminder of the actions we took:</p> <ul><li>We masked the start and end logits corresponding to tokens outside of the context.</li> <li>We then converted the start and end logits into probabilities using a softmax.</li> <li>We attributed a score to each <code>(start_token, end_token)</code> pair by taking the product of the corresponding two probabilities.</li> <li>We looked for the pair with the maximum score that yielded a valid answer (e.g., a <code>start_token</code> lower than <code>end_token</code>).</li></ul> <p>Here we will change this process slightly because we don’t need to compute actual scores (just the predicted answer). This means we can skip the softmax step. To go faster, we also won’t score all the possible <code>(start_token, end_token)</code> pairs, but only the ones corresponding to the highest <code>n_best</code> logits (with <code>n_best=20</code>). Since we will skip the softmax, those scores will be logit scores, and will be obtained by taking the sum of the start and end logits (instead of the product, because of the rule <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>log</mi><mo>⁡</mo><mo stretchy="false">(</mo><mi>a</mi><mi>b</mi><mo stretchy="false">)</mo><mo>=</mo><mi>log</mi><mo>⁡</mo><mo stretchy="false">(</mo><mi>a</mi><mo stretchy="false">)</mo><mo>+</mo><mi>log</mi><mo>⁡</mo><mo stretchy="false">(</mo><mi>b</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\log(ab) = \log(a) + \log(b)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mop">lo<span style="margin-right:0.01389em;">g</span></span><span class="mopen">(</span><span class="mord mathnormal">ab</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mop">lo<span style="margin-right:0.01389em;">g</span></span><span class="mopen">(</span><span class="mord mathnormal">a</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mop">lo<span style="margin-right:0.01389em;">g</span></span><span class="mopen">(</span><span class="mord mathnormal">b</span><span class="mclose">)</span></span></span></span>).</p> <p>To demonstrate all of this, we will need some kind of predictions. Since we have not trained our model yet, we are going to use the default model for the QA pipeline to generate some predictions on a small part of the validation set. We can use the same processing function as before; because it relies on the global constant <code>tokenizer</code>, we just have to change that object to the tokenizer of the model we want to use temporarily:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>small_eval_set = raw_datasets[<span class="hljs-string">"validation"</span>].select(<span class="hljs-built_in">range</span>(<span class="hljs-number">100</span>)) trained_checkpoint = <span class="hljs-string">"distilbert-base-cased-distilled-squad"</span> tokenizer = AutoTokenizer.from_pretrained(trained_checkpoint) eval_set = small_eval_set.<span class="hljs-built_in">map</span>( preprocess_validation_examples, batched=<span class="hljs-literal">True</span>, remove_columns=raw_datasets[<span class="hljs-string">"validation"</span>].column_names, )</pre></div> <p>Now that the preprocessing is done, we change the tokenizer back to the one we originally picked:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)</pre></div> <p>We then remove the columns of our <code>eval_set</code> that are not expected by the model, build a batch with all of that small validation set, and pass it through the model. If a GPU is available, we use it to go faster:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForQuestionAnswering eval_set_for_model = eval_set.remove_columns([<span class="hljs-string">"example_id"</span>, <span class="hljs-string">"offset_mapping"</span>]) eval_set_for_model.set_format(<span class="hljs-string">"torch"</span>) device = torch.device(<span class="hljs-string">"cuda"</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">"cpu"</span>) batch = {k: eval_set_for_model[k].to(device) <span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> eval_set_for_model.column_names} trained_model = AutoModelForQuestionAnswering.from_pretrained(trained_checkpoint).to( device ) <span class="hljs-keyword">with</span> torch.no_grad(): outputs = trained_model(**batch)</pre></div> <p>Since the <code>Trainer</code> will give us predictions as NumPy arrays, we grab the start and end logits and convert them to that format:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>start_logits = outputs.start_logits.cpu().numpy() end_logits = outputs.end_logits.cpu().numpy()</pre></div> <p>Now, we need to find the predicted answer for each example in our <code>small_eval_set</code>. One example may have been split into several features in <code>eval_set</code>, so the first step is to map each example in <code>small_eval_set</code> to the corresponding features in <code>eval_set</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> collections example_to_features = collections.defaultdict(<span class="hljs-built_in">list</span>) <span class="hljs-keyword">for</span> idx, feature <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(eval_set): example_to_features[feature[<span class="hljs-string">"example_id"</span>]].append(idx)</pre></div> <p>With this in hand, we can really get to work by looping through all the examples and, for each example, through all the associated features. As we said before, we’ll look at the logit scores for the <code>n_best</code> start logits and end logits, excluding positions that give:</p> <ul><li>An answer that wouldn’t be inside the context</li> <li>An answer with negative length</li> <li>An answer that is too long (we limit the possibilities at <code>max_answer_length=30</code>)</li></ul> <p>Once we have all the scored possible answers for one example, we just pick the one with the best logit score:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np n_best = <span class="hljs-number">20</span> max_answer_length = <span class="hljs-number">30</span> predicted_answers = [] <span class="hljs-keyword">for</span> example <span class="hljs-keyword">in</span> small_eval_set: example_id = example[<span class="hljs-string">"id"</span>] context = example[<span class="hljs-string">"context"</span>] answers = [] <span class="hljs-keyword">for</span> feature_index <span class="hljs-keyword">in</span> example_to_features[example_id]: start_logit = start_logits[feature_index] end_logit = end_logits[feature_index] offsets = eval_set[<span class="hljs-string">"offset_mapping"</span>][feature_index] start_indexes = np.argsort(start_logit)[-<span class="hljs-number">1</span> : -n_best - <span class="hljs-number">1</span> : -<span class="hljs-number">1</span>].tolist() end_indexes = np.argsort(end_logit)[-<span class="hljs-number">1</span> : -n_best - <span class="hljs-number">1</span> : -<span class="hljs-number">1</span>].tolist() <span class="hljs-keyword">for</span> start_index <span class="hljs-keyword">in</span> start_indexes: <span class="hljs-keyword">for</span> end_index <span class="hljs-keyword">in</span> end_indexes: <span class="hljs-comment"># Skip answers that are not fully in the context</span> <span class="hljs-keyword">if</span> offsets[start_index] <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">or</span> offsets[end_index] <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>: <span class="hljs-keyword">continue</span> <span class="hljs-comment"># Skip answers with a length that is either &lt; 0 or &gt; max_answer_length.</span> <span class="hljs-keyword">if</span> ( end_index &lt; start_index <span class="hljs-keyword">or</span> end_index - start_index + <span class="hljs-number">1</span> &gt; max_answer_length ): <span class="hljs-keyword">continue</span> answers.append( { <span class="hljs-string">"text"</span>: context[offsets[start_index][<span class="hljs-number">0</span>] : offsets[end_index][<span class="hljs-number">1</span>]], <span class="hljs-string">"logit_score"</span>: start_logit[start_index] + end_logit[end_index], } ) best_answer = <span class="hljs-built_in">max</span>(answers, key=<span class="hljs-keyword">lambda</span> x: x[<span class="hljs-string">"logit_score"</span>]) predicted_answers.append({<span class="hljs-string">"id"</span>: example_id, <span class="hljs-string">"prediction_text"</span>: best_answer[<span class="hljs-string">"text"</span>]})</pre></div> <p>The final format of the predicted answers is the one that will be expected by the metric we will use. As usual, we can load it with the help of the 🤗 Evaluate library:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> evaluate metric = evaluate.load(<span class="hljs-string">"squad"</span>)</pre></div> <p>This metric expects the predicted answers in the format we saw above (a list of dictionaries with one key for the ID of the example and one key for the predicted text) and the theoretical answers in the format below (a list of dictionaries with one key for the ID of the example and one key for the possible answers):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>theoretical_answers = [ {<span class="hljs-string">"id"</span>: ex[<span class="hljs-string">"id"</span>], <span class="hljs-string">"answers"</span>: ex[<span class="hljs-string">"answers"</span>]} <span class="hljs-keyword">for</span> ex <span class="hljs-keyword">in</span> small_eval_set ]</pre></div> <p>We can now check that we get sensible results by looking at the first element of both lists:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">print</span>(predicted_answers[<span class="hljs-number">0</span>]) <span class="hljs-built_in">print</span>(theoretical_answers[<span class="hljs-number">0</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'id'</span>: <span class="hljs-string">'56be4db0acb8001400a502ec'</span>, <span class="hljs-string">'prediction_text'</span>: <span class="hljs-string">'Denver Broncos'</span>} {<span class="hljs-string">'id'</span>: <span class="hljs-string">'56be4db0acb8001400a502ec'</span>, <span class="hljs-string">'answers'</span>: {<span class="hljs-string">'text'</span>: [<span class="hljs-string">'Denver Broncos'</span>, <span class="hljs-string">'Denver Broncos'</span>, <span class="hljs-string">'Denver Broncos'</span>], <span class="hljs-string">'answer_start'</span>: [<span class="hljs-number">177</span>, <span class="hljs-number">177</span>, <span class="hljs-number">177</span>]}}</pre></div> <p>Not too bad! Now let’s have a look at the score the metric gives us:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>metric.compute(predictions=predicted_answers, references=theoretical_answers)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'exact_match'</span>: <span class="hljs-number">83.0</span>, <span class="hljs-string">'f1'</span>: <span class="hljs-number">88.25</span>}</pre></div> <p>Again, that’s rather good considering that according to <a href="https://arxiv.org/abs/1910.01108v2" rel="nofollow">its paper</a> DistilBERT fine-tuned on SQuAD obtains 79.1 and 86.9 for those scores on the whole dataset.</p> <p>Now let’s put everything we just did in a <code>compute_metrics()</code> function that we will use in the <code>Trainer</code>. Normally, that <code>compute_metrics()</code> function only receives a tuple <code>eval_preds</code> with logits and labels. Here we will need a bit more, as we have to look in the dataset of features for the offset and in the dataset of examples for the original contexts, so we won’t be able to use this function to get regular evaluation results during training. We will only use it at the end of training to check the results.</p> <p>The <code>compute_metrics()</code> function groups the same steps as before; we just add a small check in case we don’t come up with any valid answers (in which case we predict an empty string).</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm <span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">start_logits, end_logits, features, examples</span>): example_to_features = collections.defaultdict(<span class="hljs-built_in">list</span>) <span class="hljs-keyword">for</span> idx, feature <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(features): example_to_features[feature[<span class="hljs-string">"example_id"</span>]].append(idx) predicted_answers = [] <span class="hljs-keyword">for</span> example <span class="hljs-keyword">in</span> tqdm(examples): example_id = example[<span class="hljs-string">"id"</span>] context = example[<span class="hljs-string">"context"</span>] answers = [] <span class="hljs-comment"># Loop through all features associated with that example</span> <span class="hljs-keyword">for</span> feature_index <span class="hljs-keyword">in</span> example_to_features[example_id]: start_logit = start_logits[feature_index] end_logit = end_logits[feature_index] offsets = features[feature_index][<span class="hljs-string">"offset_mapping"</span>] start_indexes = np.argsort(start_logit)[-<span class="hljs-number">1</span> : -n_best - <span class="hljs-number">1</span> : -<span class="hljs-number">1</span>].tolist() end_indexes = np.argsort(end_logit)[-<span class="hljs-number">1</span> : -n_best - <span class="hljs-number">1</span> : -<span class="hljs-number">1</span>].tolist() <span class="hljs-keyword">for</span> start_index <span class="hljs-keyword">in</span> start_indexes: <span class="hljs-keyword">for</span> end_index <span class="hljs-keyword">in</span> end_indexes: <span class="hljs-comment"># Skip answers that are not fully in the context</span> <span class="hljs-keyword">if</span> offsets[start_index] <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span> <span class="hljs-keyword">or</span> offsets[end_index] <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>: <span class="hljs-keyword">continue</span> <span class="hljs-comment"># Skip answers with a length that is either &lt; 0 or &gt; max_answer_length</span> <span class="hljs-keyword">if</span> ( end_index &lt; start_index <span class="hljs-keyword">or</span> end_index - start_index + <span class="hljs-number">1</span> &gt; max_answer_length ): <span class="hljs-keyword">continue</span> answer = { <span class="hljs-string">"text"</span>: context[offsets[start_index][<span class="hljs-number">0</span>] : offsets[end_index][<span class="hljs-number">1</span>]], <span class="hljs-string">"logit_score"</span>: start_logit[start_index] + end_logit[end_index], } answers.append(answer) <span class="hljs-comment"># Select the answer with the best score</span> <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(answers) &gt; <span class="hljs-number">0</span>: best_answer = <span class="hljs-built_in">max</span>(answers, key=<span class="hljs-keyword">lambda</span> x: x[<span class="hljs-string">"logit_score"</span>]) predicted_answers.append( {<span class="hljs-string">"id"</span>: example_id, <span class="hljs-string">"prediction_text"</span>: best_answer[<span class="hljs-string">"text"</span>]} ) <span class="hljs-keyword">else</span>: predicted_answers.append({<span class="hljs-string">"id"</span>: example_id, <span class="hljs-string">"prediction_text"</span>: <span class="hljs-string">""</span>}) theoretical_answers = [{<span class="hljs-string">"id"</span>: ex[<span class="hljs-string">"id"</span>], <span class="hljs-string">"answers"</span>: ex[<span class="hljs-string">"answers"</span>]} <span class="hljs-keyword">for</span> ex <span class="hljs-keyword">in</span> examples] <span class="hljs-keyword">return</span> metric.compute(predictions=predicted_answers, references=theoretical_answers)</pre></div> <p>We can check it works on our predictions:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>compute_metrics(start_logits, end_logits, eval_set, small_eval_set)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'exact_match'</span>: <span class="hljs-number">83.0</span>, <span class="hljs-string">'f1'</span>: <span class="hljs-number">88.25</span>}</pre></div> <p>Looking good! Now let’s use this to fine-tune our model.</p> <h3 class="relative group"><a id="fine-tuning-the-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fine-tuning-the-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tuning the model</span></h3> <p>We are now ready to train our model. Let’s create it first, using the <code>AutoModelForQuestionAnswering</code> class like before:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model = AutoModelForQuestionAnswering.from_pretrained(model_checkpoint)</pre></div> <p>As usual, we get a warning that some weights are not used (the ones from the pretraining head) and some others are initialized randomly (the ones for the question answering head). You should be used to this by now, but that means this model is not ready to be used just yet and needs fine-tuning — good thing we’re about to do that!</p> <p>To be able to push our model to the Hub, we’ll need to log in to Hugging Face. If you’re running this code in a notebook, you can do so with the following utility function, which displays a widget where you can enter your login credentials:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login notebook_login()</pre></div> <p>If you aren’t working in a notebook, just type the following line in your terminal:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-cli login</pre></div> <p>Once this is done, we can define our <code>TrainingArguments</code>. As we said when we defined our function to compute the metric, we won’t be able to have a regular evaluation loop because of the signature of the <code>compute_metrics()</code> function. We could write our own subclass of <code>Trainer</code> to do this (an approach you can find in the <a href="https://github.com/huggingface/transformers/blob/master/examples/pytorch/question-answering/trainer_qa.py" rel="nofollow">question answering example script</a>), but that’s a bit too long for this section. Instead, we will only evaluate the model at the end of training here and show you how to do a regular evaluation in “A custom training loop” below.</p> <p>This is really where the <code>Trainer</code> API shows its limits and the 🤗 Accelerate library shines: customizing the class to a specific use case can be painful, but tweaking a fully exposed training loop is easy.</p> <p>Let’s take a look at our <code>TrainingArguments</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments args = TrainingArguments( <span class="hljs-string">"bert-finetuned-squad"</span>, evaluation_strategy=<span class="hljs-string">"no"</span>, save_strategy=<span class="hljs-string">"epoch"</span>, learning_rate=<span class="hljs-number">2e-5</span>, num_train_epochs=<span class="hljs-number">3</span>, weight_decay=<span class="hljs-number">0.01</span>, fp16=<span class="hljs-literal">True</span>, push_to_hub=<span class="hljs-literal">True</span>, )</pre></div> <p>We’ve seen most of these before: we set some hyperparameters (like the learning rate, the number of epochs we train for, and some weight decay) and indicate that we want to save the model at the end of every epoch, skip evaluation, and upload our results to the Model Hub. We also enable mixed-precision training with <code>fp16=True</code>, as it can speed up the training nicely on a recent GPU.</p> <p>By default, the repository used will be in your namespace and named after the output directory you set, so in our case it will be in <code>"sgugger/bert-finetuned-squad"</code>. We can override this by passing a <code>hub_model_id</code>; for instance, to push the model to the <code>huggingface_course</code> organization we used <code>hub_model_id="huggingface_course/bert-finetuned-squad"</code> (which is the model we linked to at the beginning of this section).</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 If the output directory you are using exists, it needs to be a local clone of the repository you want to push to (so set a new name if you get an error when defining your <code>Trainer</code>).</p></div> <p>Finally, we just pass everything to the <code>Trainer</code> class and launch the training:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Trainer trainer = Trainer( model=model, args=args, train_dataset=train_dataset, eval_dataset=validation_dataset, tokenizer=tokenizer, ) trainer.train()</pre></div> <p>Note that while the training happens, each time the model is saved (here, every epoch) it is uploaded to the Hub in the background. This way, you will be able to to resume your training on another machine if necessary. The whole training takes a while (a little over an hour on a Titan RTX), so you can grab a coffee or reread some of the parts of the course that you’ve found more challenging while it proceeds. Also note that as soon as the first epoch is finished, you will see some weights uploaded to the Hub and you can start playing with your model on its page.</p> <p>Once the training is complete, we can finally evaluate our model (and pray we didn’t spend all that compute time on nothing). The <code>predict()</code> method of the <code>Trainer</code> will return a tuple where the first elements will be the predictions of the model (here a pair with the start and end logits). We send this to our <code>compute_metrics()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>predictions, _, _ = trainer.predict(validation_dataset) start_logits, end_logits = predictions compute_metrics(start_logits, end_logits, validation_dataset, raw_datasets[<span class="hljs-string">"validation"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'exact_match'</span>: <span class="hljs-number">81.18259224219489</span>, <span class="hljs-string">'f1'</span>: <span class="hljs-number">88.67381321905516</span>}</pre></div> <p>Great! As a comparison, the baseline scores reported in the BERT article for this model are 80.8 and 88.5, so we’re right where we should be.</p> <p>Finally, we use the <code>push_to_hub()</code> method to make sure we upload the latest version of the model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.push_to_hub(commit_message=<span class="hljs-string">"Training complete"</span>)</pre></div> <p>This returns the URL of the commit it just did, if you want to inspect it:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'https://huggingface.co/sgugger/bert-finetuned-squad/commit/9dcee1fbc25946a6ed4bb32efb1bd71d5fa90b68'</span></pre></div> <p>The <code>Trainer</code> also drafts a model card with all the evaluation results and uploads it.</p> <p>At this stage, you can use the inference widget on the Model Hub to test the model and share it with your friends, family, and favorite pets. You have successfully fine-tuned a model on a question answering task — congratulations!</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Your turn!</strong> Try another model architecture to see if it performs better on this task!</p></div> <p>If you want to dive a bit more deeply into the training loop, we will now show you how to do the same thing using 🤗 Accelerate.</p> <h2 class="relative group"><a id="a-custom-training-loop" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#a-custom-training-loop"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>A custom training loop</span></h2> <p>Let’s now have a look at the full training loop, so you can easily customize the parts you need. It will look a lot like the training loop in <a href="/course/chapter3/4">Chapter 3</a>, with the exception of the evaluation loop. We will be able to evaluate the model regularly since we’re not constrained by the <code>Trainer</code> class anymore.</p> <h3 class="relative group"><a id="preparing-everything-for-training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preparing-everything-for-training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preparing everything for training</span></h3> <p>First we need to build the <code>DataLoader</code>s from our datasets. We set the format of those datasets to <code>"torch"</code>, and remove the columns in the validation set that are not used by the model. Then, we can use the <code>default_data_collator</code> provided by Transformers as a <code>collate_fn</code> and shuffle the training set, but not the validation set:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> default_data_collator train_dataset.set_format(<span class="hljs-string">"torch"</span>) validation_set = validation_dataset.remove_columns([<span class="hljs-string">"example_id"</span>, <span class="hljs-string">"offset_mapping"</span>]) validation_set.set_format(<span class="hljs-string">"torch"</span>) train_dataloader = DataLoader( train_dataset, shuffle=<span class="hljs-literal">True</span>, collate_fn=default_data_collator, batch_size=<span class="hljs-number">8</span>, ) eval_dataloader = DataLoader( validation_set, collate_fn=default_data_collator, batch_size=<span class="hljs-number">8</span> )</pre></div> <p>Next we reinstantiate our model, to make sure we’re not continuing the fine-tuning from before but starting from the BERT pretrained model again:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model = AutoModelForQuestionAnswering.from_pretrained(model_checkpoint)</pre></div> <p>Then we will need an optimizer. As usual we use the classic <code>AdamW</code>, which is like Adam, but with a fix in the way weight decay is applied:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> torch.optim <span class="hljs-keyword">import</span> AdamW optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">2e-5</span>)</pre></div> <p>Once we have all those objects, we can send them to the <code>accelerator.prepare()</code> method. Remember that if you want to train on TPUs in a Colab notebook, you will need to move all of this code into a training function, and that shouldn’t execute any cell that instantiates an <code>Accelerator</code>. We can force mixed-precision training by passing <code>fp16=True</code> to the <code>Accelerator</code> (or, if you are executing the code as a script, just make sure to fill in the 🤗 Accelerate <code>config</code> appropriately).</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator accelerator = Accelerator(fp16=<span class="hljs-literal">True</span>) model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( model, optimizer, train_dataloader, eval_dataloader )</pre></div> <p>As you should know from the previous sections, we can only use the <code>train_dataloader</code> length to compute the number of training steps after it has gone through the <code>accelerator.prepare()</code> method. We use the same linear schedule as in the previous sections:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler num_train_epochs = <span class="hljs-number">3</span> num_update_steps_per_epoch = <span class="hljs-built_in">len</span>(train_dataloader) num_training_steps = num_train_epochs * num_update_steps_per_epoch lr_scheduler = get_scheduler( <span class="hljs-string">"linear"</span>, optimizer=optimizer, num_warmup_steps=<span class="hljs-number">0</span>, num_training_steps=num_training_steps, )</pre></div> <p>To push our model to the Hub, we will need to create a <code>Repository</code> object in a working folder. First log in to the Hugging Face Hub, if you’re not logged in already. We’ll determine the repository name from the model ID we want to give our model (feel free to replace the <code>repo_name</code> with your own choice; it just needs to contain your username, which is what the function <code>get_full_repo_name()</code> does):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> Repository, get_full_repo_name model_name = <span class="hljs-string">"bert-finetuned-squad-accelerate"</span> repo_name = get_full_repo_name(model_name) repo_name</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'sgugger/bert-finetuned-squad-accelerate'</span></pre></div> <p>Then we can clone that repository in a local folder. If it already exists, this local folder should be a clone of the repository we are working with:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>output_dir = <span class="hljs-string">"bert-finetuned-squad-accelerate"</span> repo = Repository(output_dir, clone_from=repo_name)</pre></div> <p>We can now upload anything we save in <code>output_dir</code> by calling the <code>repo.push_to_hub()</code> method. This will help us upload the intermediate models at the end of each epoch.</p> <h2 class="relative group"><a id="training-loop" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-loop"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training loop</span></h2> <p>We are now ready to write the full training loop. After defining a progress bar to follow how training goes, the loop has three parts:</p> <ul><li>The training in itself, which is the classic iteration over the <code>train_dataloader</code>, forward pass through the model, then backward pass and optimizer step.</li> <li>The evaluation, in which we gather all the values for <code>start_logits</code> and <code>end_logits</code> before converting them to NumPy arrays. Once the evaluation loop is finished, we concatenate all the results. Note that we need to truncate because the <code>Accelerator</code> may have added a few samples at the end to ensure we have the same number of examples in each process.</li> <li>Saving and uploading, where we first save the model and the tokenizer, then call <code>repo.push_to_hub()</code>. As we did before, we use the argument <code>blocking=False</code> to tell the 🤗 Hub library to push in an asynchronous process. This way, training continues normally and this (long) instruction is executed in the background.</li></ul> <p>Here’s the complete code for the training loop:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm <span class="hljs-keyword">import</span> torch progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps)) <span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_train_epochs): <span class="hljs-comment"># Training</span> model.train() <span class="hljs-keyword">for</span> step, batch <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(train_dataloader): outputs = model(**batch) loss = outputs.loss accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(<span class="hljs-number">1</span>) <span class="hljs-comment"># Evaluation</span> model.<span class="hljs-built_in">eval</span>() start_logits = [] end_logits = [] accelerator.<span class="hljs-built_in">print</span>(<span class="hljs-string">"Evaluation!"</span>) <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> tqdm(eval_dataloader): <span class="hljs-keyword">with</span> torch.no_grad(): outputs = model(**batch) start_logits.append(accelerator.gather(outputs.start_logits).cpu().numpy()) end_logits.append(accelerator.gather(outputs.end_logits).cpu().numpy()) start_logits = np.concatenate(start_logits) end_logits = np.concatenate(end_logits) start_logits = start_logits[: <span class="hljs-built_in">len</span>(validation_dataset)] end_logits = end_logits[: <span class="hljs-built_in">len</span>(validation_dataset)] metrics = compute_metrics( start_logits, end_logits, validation_dataset, raw_datasets[<span class="hljs-string">"validation"</span>] ) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"epoch <span class="hljs-subst">{epoch}</span>:"</span>, metrics) <span class="hljs-comment"># Save and upload</span> accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save) <span class="hljs-keyword">if</span> accelerator.is_main_process: tokenizer.save_pretrained(output_dir) repo.push_to_hub( commit_message=<span class="hljs-string">f"Training in progress epoch <span class="hljs-subst">{epoch}</span>"</span>, blocking=<span class="hljs-literal">False</span> )</pre></div> <p>In case this is the first time you’re seeing a model saved with 🤗 Accelerate, let’s take a moment to inspect the three lines of code that go with it:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>accelerator.wait_for_everyone() unwrapped_model = accelerator.unwrap_model(model) unwrapped_model.save_pretrained(output_dir, save_function=accelerator.save)</pre></div> <p>The first line is self-explanatory: it tells all the processes to wait until everyone is at that stage before continuing. This is to make sure we have the same model in every process before saving. Then we grab the <code>unwrapped_model</code>, which is the base model we defined. The <code>accelerator.prepare()</code> method changes the model to work in distributed training, so it won’t have the <code>save_pretrained()</code> method anymore; the <code>accelerator.unwrap_model()</code> method undoes that step. Lastly, we call <code>save_pretrained()</code> but tell that method to use <code>accelerator.save()</code> instead of <code>torch.save()</code>.</p> <p>Once this is done, you should have a model that produces results pretty similar to the one trained with the <code>Trainer</code>. You can check the model we trained using this code at <a href="https://huggingface.co/huggingface-course/bert-finetuned-squad-accelerate" rel="nofollow"><em>huggingface-course/bert-finetuned-squad-accelerate</em></a>. And if you want to test out any tweaks to the training loop, you can directly implement them by editing the code shown above!</p> <h2 class="relative group"><a id="using-the-fine-tuned-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-the-fine-tuned-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using the fine-tuned model</span></h2> <p>We’ve already shown you how you can use the model we fine-tuned on the Model Hub with the inference widget. To use it locally in a <code>pipeline</code>, you just have to specify the model identifier:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline <span class="hljs-comment"># Replace this with your own checkpoint</span> model_checkpoint = <span class="hljs-string">"huggingface-course/bert-finetuned-squad"</span> question_answerer = pipeline(<span class="hljs-string">"question-answering"</span>, model=model_checkpoint) context = <span class="hljs-string">""" 🤗 Transformers is backed by the three most popular deep learning libraries — Jax, PyTorch and TensorFlow — with a seamless integration between them. It's straightforward to train your models with one before loading them for inference with the other. """</span> question = <span class="hljs-string">"Which deep learning libraries back 🤗 Transformers?"</span> question_answerer(question=question, context=context)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'score'</span>: <span class="hljs-number">0.9979003071784973</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">78</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">105</span>, <span class="hljs-string">'answer'</span>: <span class="hljs-string">'Jax, PyTorch and TensorFlow'</span>}</pre></div> <p>Great! Our model is working as well as the default one for this pipeline!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter7/6?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Training a causal language model from scratch</a> <a href="/learn/nlp-course/chapter7/8?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Mastering NLP<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Question answering&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;question-answering&quot;,&quot;url&quot;:&quot;#question-answering&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Preparing the data&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;preparing-the-data&quot;,&quot;url&quot;:&quot;#preparing-the-data&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;The SQuAD dataset&quot;,&quot;id&quot;:&quot;the-squad-dataset&quot;,&quot;url&quot;:&quot;#the-squad-dataset&quot;},{&quot;title&quot;:&quot;Processing the training data&quot;,&quot;id&quot;:&quot;processing-the-training-data&quot;,&quot;url&quot;:&quot;#processing-the-training-data&quot;},{&quot;title&quot;:&quot;Processing the validation data&quot;,&quot;id&quot;:&quot;processing-the-validation-data&quot;,&quot;url&quot;:&quot;#processing-the-validation-data&quot;}]},{&quot;title&quot;:&quot;Fine-tuning the model with the `Trainer` API&quot;,&quot;id&quot;:&quot;fine-tuning-the-model-with-the-trainer-api&quot;,&quot;url&quot;:&quot;#fine-tuning-the-model-with-the-trainer-api&quot;},{&quot;title&quot;:&quot;Fine-tuning the model with Keras&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;fine-tuning-the-model-with-keras&quot;,&quot;url&quot;:&quot;#fine-tuning-the-model-with-keras&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Post-processing&quot;,&quot;id&quot;:&quot;post-processing&quot;,&quot;url&quot;:&quot;#post-processing&quot;},{&quot;title&quot;:&quot;Fine-tuning the model&quot;,&quot;id&quot;:&quot;fine-tuning-the-model&quot;,&quot;url&quot;:&quot;#fine-tuning-the-model&quot;}]},{&quot;title&quot;:&quot;A custom training loop&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;a-custom-training-loop&quot;,&quot;url&quot;:&quot;#a-custom-training-loop&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Preparing everything for training&quot;,&quot;id&quot;:&quot;preparing-everything-for-training&quot;,&quot;url&quot;:&quot;#preparing-everything-for-training&quot;}]},{&quot;title&quot;:&quot;Training loop&quot;,&quot;id&quot;:&quot;training-loop&quot;,&quot;url&quot;:&quot;#training-loop&quot;},{&quot;title&quot;:&quot;Using the fine-tuned model&quot;,&quot;id&quot;:&quot;using-the-fine-tuned-model&quot;,&quot;url&quot;:&quot;#using-the-fine-tuned-model&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#question-answering" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-question-answering"><wbr>Question answering</a> <a href="#preparing-the-data" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preparing-the-data"><wbr>Preparing the data</a> <a href="#the-squad-dataset" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-squad-dataset"><wbr>The S<wbr>QuA<wbr>D dataset</a> <a href="#processing-the-training-data" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-processing-the-training-data"><wbr>Processing the training data</a> <a href="#processing-the-validation-data" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-processing-the-validation-data"><wbr>Processing the validation data</a> <a href="#fine-tuning-the-model-with-the-trainer-api" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-the-model-with-the-trainer-api"><wbr>Fine-tuning the model with the `<wbr>Trainer` API</a> <a href="#post-processing" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-post-processing"><wbr>Post-processing</a> <a href="#fine-tuning-the-model" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fine-tuning-the-model"><wbr>Fine-tuning the model</a> <a href="#a-custom-training-loop" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-a-custom-training-loop"><wbr>A custom training loop</a> <a href="#preparing-everything-for-training" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preparing-everything-for-training"><wbr>Preparing everything for training</a> <a href="#training-loop" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-loop"><wbr>Training loop</a> <a href="#using-the-fine-tuned-model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-the-fine-tuned-model"><wbr>Using the fine-tuned model</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter7/7" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter7/7"); } </script> <iframe name="__privateStripeMetricsController4060" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter7%2F7%3Ffw%3Dpt&amp;title=Question%20answering%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:31.159Z
Mastering NLP - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter7/8?fw=pt
## [](#mastering-nlp)Mastering NLP [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-7-questions) If you’ve made it this far in the course, congratulations — you now have all the knowledge and tools you need to tackle (almost) any NLP task with 🤗 Transformers and the Hugging Face ecosystem! We have seen a lot of different data collators, so we made this little video to help you find which one to use for each task: After completing this lightning tour through the core NLP tasks, you should: - Know which architectures (encoder, decoder, or encoder-decoder) are best suited for each task - Understand the difference between pretraining and fine-tuning a language model - Know how to train Transformer models using either the `Trainer` API and distributed training features of 🤗 Accelerate or TensorFlow and Keras, depending on which track you’ve been following - Understand the meaning and limitations of metrics like ROUGE and BLEU for text generation tasks - Know how to interact with your fine-tuned models, both on the Hub and using the `pipeline` from 🤗 Transformers Despite all this knowledge, there will come a time when you’ll either encounter a difficult bug in your code or have a question about how to solve a particular NLP problem. Fortunately, the Hugging Face community is here to help you! In the final chapter of this part of the course, we’ll explore how you can debug your Transformer models and ask for help effectively.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Mastering NLP - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter7/8"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Mastering NLP - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;mastering-nlp&quot;,&quot;title&quot;:&quot;Mastering NLP&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter7/8&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Mastering NLP&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Mastering NLP</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/2?fw=pt">Token classification </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/3?fw=pt">Fine-tuning a masked language model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/4?fw=pt">Translation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/5?fw=pt">Summarization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/6?fw=pt">Training a causal language model from scratch </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/7?fw=pt">Question answering </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter7/8?fw=pt">Mastering NLP </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="mastering-nlp" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#mastering-nlp"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Mastering NLP</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-7-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>If you’ve made it this far in the course, congratulations — you now have all the knowledge and tools you need to tackle (almost) any NLP task with 🤗 Transformers and the Hugging Face ecosystem!</p> <p>We have seen a lot of different data collators, so we made this little video to help you find which one to use for each task:</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/-RPeakdlHYo" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>After completing this lightning tour through the core NLP tasks, you should:</p> <ul><li>Know which architectures (encoder, decoder, or encoder-decoder) are best suited for each task</li> <li>Understand the difference between pretraining and fine-tuning a language model</li> <li>Know how to train Transformer models using either the <code>Trainer</code> API and distributed training features of 🤗 Accelerate or TensorFlow and Keras, depending on which track you’ve been following</li> <li>Understand the meaning and limitations of metrics like ROUGE and BLEU for text generation tasks</li> <li>Know how to interact with your fine-tuned models, both on the Hub and using the <code>pipeline</code> from 🤗 Transformers</li></ul> <p>Despite all this knowledge, there will come a time when you’ll either encounter a difficult bug in your code or have a question about how to solve a particular NLP problem. Fortunately, the Hugging Face community is here to help you! In the final chapter of this part of the course, we’ll explore how you can debug your Transformer models and ask for help effectively.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter7/7?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Question answering</a> <a href="/learn/nlp-course/chapter7/9?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">End-of-chapter quiz<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;mastering-nlp&quot;,&quot;url&quot;:&quot;#mastering-nlp&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#mastering-nlp" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-mastering-nlp"><wbr>Mastering NLP</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter7/8" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter7/8"); } </script> <iframe name="__privateStripeMetricsController6540" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter7%2F8%3Ffw%3Dpt&amp;title=Mastering%20NLP%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:31.623Z
End-of-chapter quiz - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter7/9?fw=pt
3\. Fine-tuning a pretrained model 4\. Sharing models and tokenizers 5\. The 🤗 Datasets library 6\. The 🤗 Tokenizers library 9\. Building and sharing demos new [Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#end-of-chapter-quiz)End-of-chapter quiz [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-7-questions) Let’s test what you learned in this chapter! ### [](#1.-which-of-the-following-tasks-can-be-framed-as-a-token-classification-problem?)1\. Which of the following tasks can be framed as a token classification problem? ### [](#2.-what-part-of-the-preprocessing-for-token-classification-differs-from-the-other-preprocessing-pipelines?)2\. What part of the preprocessing for token classification differs from the other preprocessing pipelines? ### [](#3.-what-problem-arises-when-we-tokenize-the-words-in-a-token-classification-problem-and-want-to-label-the-tokens?)3\. What problem arises when we tokenize the words in a token classification problem and want to label the tokens? ### [](#4.-what-does-“domain-adaptation”-mean?)4\. What does “domain adaptation” mean? ### [](#5.-what-are-the-labels-in-a-masked-language-modeling-problem?)5\. What are the labels in a masked language modeling problem? ### [](#6.-which-of-these-tasks-can-be-seen-as-a-sequence-to-sequence-problem?)6\. Which of these tasks can be seen as a sequence-to-sequence problem? ### [](#7.-what-is-the-proper-way-to-preprocess-the-data-for-a-sequence-to-sequence-problem?)7\. What is the proper way to preprocess the data for a sequence-to-sequence problem? ### [](#8.-why-is-there-a-specific-subclass-of-<code>trainer</code>-for-sequence-to-sequence-problems?)8\. Why is there a specific subclass of `Trainer` for sequence-to-sequence problems? ### [](#10.-when-should-you-pretrain-a-new-model?)10\. When should you pretrain a new model? ### [](#11.-why-is-it-easy-to-pretrain-a-language-model-on-lots-and-lots-of-texts?)11\. Why is it easy to pretrain a language model on lots and lots of texts? ### [](#12.-what-are-the-main-challenges-when-preprocessing-data-for-a-question-answering-task?)12\. What are the main challenges when preprocessing data for a question answering task? ### [](#13.-how-is-post-processing-usually-done-in-question-answering?)13\. How is post-processing usually done in question answering?
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="End-of-chapter quiz - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter7/9"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>End-of-chapter quiz - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;end-of-chapter-quiz&quot;,&quot;title&quot;:&quot;End-of-chapter quiz&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter7/9&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;End-of-chapter quiz&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">End-of-chapter quiz</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/2?fw=pt">Token classification </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/3?fw=pt">Fine-tuning a masked language model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/4?fw=pt">Translation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/5?fw=pt">Summarization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/6?fw=pt">Training a causal language model from scratch </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/7?fw=pt">Question answering </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter7/8?fw=pt">Mastering NLP </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter7/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="end-of-chapter-quiz" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#end-of-chapter-quiz"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>End-of-chapter quiz</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-7-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>Let’s test what you learned in this chapter!</p> <h3 class="relative group"><a id="1.-which-of-the-following-tasks-can-be-framed-as-a-token-classification-problem?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#1.-which-of-the-following-tasks-can-be-framed-as-a-token-classification-problem?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>1. Which of the following tasks can be framed as a token classification problem?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Find the grammatical components in a sentence.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Find whether a sentence is grammatically correct or not.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Find the persons mentioned in a sentence.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Find the chunk of words in a sentence that answers a question.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="2.-what-part-of-the-preprocessing-for-token-classification-differs-from-the-other-preprocessing-pipelines?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#2.-what-part-of-the-preprocessing-for-token-classification-differs-from-the-other-preprocessing-pipelines?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>2. What part of the preprocessing for token classification differs from the other preprocessing pipelines?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> There is no need to do anything; the texts are already tokenized.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> The texts are given as words, so we only need to apply subword tokenization.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> We use <code>-100</code> to label the special tokens.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> We need to make sure to truncate or pad the labels to the same size as the inputs, when applying truncation/padding.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="3.-what-problem-arises-when-we-tokenize-the-words-in-a-token-classification-problem-and-want-to-label-the-tokens?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#3.-what-problem-arises-when-we-tokenize-the-words-in-a-token-classification-problem-and-want-to-label-the-tokens?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>3. What problem arises when we tokenize the words in a token classification problem and want to label the tokens?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> The tokenizer adds special tokens and we have no labels for them.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Each word can produce several tokens, so we end up with more tokens than we have labels.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> The added tokens have no labels, so there is no problem.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="4.-what-does-“domain-adaptation”-mean?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#4.-what-does-“domain-adaptation”-mean?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>4. What does “domain adaptation” mean?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It's when we run a model on a dataset and get the predictions for each sample in that dataset.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It's when we train a model on a dataset.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It's when we fine-tune a pretrained model on a new dataset, and it gives predictions that are more adapted to that dataset</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> It's when we add misclassified samples to a dataset to make our model more robust.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="5.-what-are-the-labels-in-a-masked-language-modeling-problem?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#5.-what-are-the-labels-in-a-masked-language-modeling-problem?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>5. What are the labels in a masked language modeling problem?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Some of the tokens in the input sentence are randomly masked and the labels are the original input tokens.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Some of the tokens in the input sentence are randomly masked and the labels are the original input tokens, shifted to the left.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Some of the tokens in the input sentence are randomly masked, and the label is whether the sentence is positive or negative.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Some of the tokens in the two input sentences are randomly masked, and the label is whether the two sentences are similar or not.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="6.-which-of-these-tasks-can-be-seen-as-a-sequence-to-sequence-problem?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#6.-which-of-these-tasks-can-be-seen-as-a-sequence-to-sequence-problem?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>6. Which of these tasks can be seen as a sequence-to-sequence problem?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Writing short reviews of long documents</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Answering questions about a document</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Translating a text in Chinese into English</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Fixing the messages sent by my nephew/friend so they're in proper English</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="7.-what-is-the-proper-way-to-preprocess-the-data-for-a-sequence-to-sequence-problem?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#7.-what-is-the-proper-way-to-preprocess-the-data-for-a-sequence-to-sequence-problem?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>7. What is the proper way to preprocess the data for a sequence-to-sequence problem?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> The inputs and targets have to be sent together to the tokenizer with <code>inputs=...</code> and <code>targets=...</code>.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> The inputs and the targets both have to be preprocessed, in two separate calls to the tokenizer.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> As usual, we just have to tokenize the inputs.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> The inputs have to be sent to the tokenizer, and the targets too, but under a special context manager.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="8.-why-is-there-a-specific-subclass-of-<code>trainer</code>-for-sequence-to-sequence-problems?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#8.-why-is-there-a-specific-subclass-of-<code>trainer</code>-for-sequence-to-sequence-problems?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>8. Why is there a specific subclass of <code>Trainer</code> for sequence-to-sequence problems?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Because sequence-to-sequence problems use a custom loss, to ignore the labels set to <code>-100</code></label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Because sequence-to-sequence problems require a special evaluation loop</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Because the targets are texts in sequence-to-sequence problems</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Because we use two models in sequence-to-sequence problems</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="10.-when-should-you-pretrain-a-new-model?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#10.-when-should-you-pretrain-a-new-model?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>10. When should you pretrain a new model?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> When there is no pretrained model available for your specific language</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> When you have lots of data available, even if there is a pretrained model that could work on it</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> When you have concerns about the bias of the pretrained model you are using</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> When the pretrained models available are just not good enough</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="11.-why-is-it-easy-to-pretrain-a-language-model-on-lots-and-lots-of-texts?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#11.-why-is-it-easy-to-pretrain-a-language-model-on-lots-and-lots-of-texts?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>11. Why is it easy to pretrain a language model on lots and lots of texts?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Because there are plenty of texts available on the internet</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Because the pretraining objective does not require humans to label the data</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Because the 🤗 Transformers library only requires a few lines of code to start the training</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="12.-what-are-the-main-challenges-when-preprocessing-data-for-a-question-answering-task?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#12.-what-are-the-main-challenges-when-preprocessing-data-for-a-question-answering-task?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>12. What are the main challenges when preprocessing data for a question answering task?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> You need to tokenize the inputs.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> You need to deal with very long contexts, which give several training features that may or may not have the answer in them.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> You need to tokenize the answers to the question as well as the inputs.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> From the answer span in the text, you have to find the start and end token in the tokenized input.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="13.-how-is-post-processing-usually-done-in-question-answering?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#13.-how-is-post-processing-usually-done-in-question-answering?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>13. How is post-processing usually done in question answering?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> The model gives you the start and end positions of the answer, and you just have to decode the corresponding span of tokens.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> The model gives you the start and end positions of the answer for each feature created by one example, and you just have to decode the corresponding span of tokens in the one that has the best score.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> The model gives you the start and end positions of the answer for each feature created by one example, and you just have to match them to the span in the context for the one that has the best score.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> The model generates an answer, and you just have to decode it.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter7/8?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Mastering NLP</a> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isLoggedIn&quot;:false,&quot;nextChapterUrl&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;,&quot;quizChapter&quot;:7}" data-target="CourseCompleteButton"><div class="relative ml-auto"> <button class="btn btn-lg" disabled=""><svg class="mr-2" width="1em" height="1em" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0 16C0 7.16344 7.16344 0 16 0V0C24.8366 0 32 7.16344 32 16V16C32 24.8366 24.8366 32 16 32V32C7.16344 32 0 24.8366 0 16V16Z" fill="#D9FFD2"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M23.5607 10.9393C24.1464 11.5251 24.1464 12.4749 23.5607 13.0607L14.5 22.1213L8.93934 16.5607C8.35355 15.9749 8.35355 15.0251 8.93934 14.4393C9.52513 13.8536 10.4749 13.8536 11.0607 14.4393L14.5 17.8787L21.4393 10.9393C22.0251 10.3536 22.9749 10.3536 23.5607 10.9393Z" fill="#188B3F"></path></svg> Complete Chapter</button></div></div> <a href="/learn/nlp-course/chapter8/1?fw=pt" class="hidden">Next chapter</a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;id&quot;:&quot;end-of-chapter-quiz&quot;,&quot;url&quot;:&quot;#end-of-chapter-quiz&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#end-of-chapter-quiz" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-end-of-chapter-quiz"><wbr>End-of-chapter quiz</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter7/9" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter7/9"); } </script> <iframe name="__privateStripeMetricsController0660" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter7%2F9%3Ffw%3Dpt&amp;title=End-of-chapter%20quiz%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:31.736Z
Introduction - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter8/1?fw=pt
## [](#introduction)Introduction [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-8-questions) Now that you know how to tackle the most common NLP tasks with 🤗 Transformers, you should be able to get started on your own projects! In this chapter we will explore what to do when you hit a problem. You’ll learn how to successfully debug your code or your training, and how to ask the community for help if you don’t manage to solve the problem by yourself. And if you think you’ve found a bug in one of the Hugging Face libraries, we’ll show you the best way to report it so that the issue is resolved as quickly as possible. More precisely, in this chapter you will learn: - The first thing to do when you get an error - How to ask for help on the [forums](https://discuss.huggingface.co/) - How to debug your training pipeline - How to write a good issue None of this is specifically related to 🤗 Transformers or the Hugging Face ecosystem, of course; the lessons from this chapter are applicable to most open source projects!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Introduction - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter8/1"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Introduction - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;introduction&quot;,&quot;title&quot;:&quot;Introduction&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter8/1&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Introduction&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Introduction</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter8/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/2?fw=pt">What to do when you get an error </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/3?fw=pt">Asking for help on the forums </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/4?fw=pt">Debugging the training pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/5?fw=pt">How to write a good issue </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/6?fw=pt">Part 2 completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/7?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="introduction" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#introduction"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Introduction</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-8-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>Now that you know how to tackle the most common NLP tasks with 🤗 Transformers, you should be able to get started on your own projects! In this chapter we will explore what to do when you hit a problem. You’ll learn how to successfully debug your code or your training, and how to ask the community for help if you don’t manage to solve the problem by yourself. And if you think you’ve found a bug in one of the Hugging Face libraries, we’ll show you the best way to report it so that the issue is resolved as quickly as possible.</p> <p>More precisely, in this chapter you will learn:</p> <ul><li>The first thing to do when you get an error</li> <li>How to ask for help on the <a href="https://discuss.huggingface.co/" rel="nofollow">forums</a></li> <li>How to debug your training pipeline</li> <li>How to write a good issue</li></ul> <p>None of this is specifically related to 🤗 Transformers or the Hugging Face ecosystem, of course; the lessons from this chapter are applicable to most open source projects!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter7/9?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>End-of-chapter quiz</a> <a href="/learn/nlp-course/chapter8/2?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">What to do when you get an error<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;introduction&quot;,&quot;url&quot;:&quot;#introduction&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#introduction" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-introduction"><wbr>Introduction</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter8/1" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter8/1"); } </script> <iframe name="__privateStripeMetricsController1170" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter8%2F1%3Ffw%3Dpt&amp;title=Introduction%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:32.305Z
What to do when you get an error - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter8/2?fw=pt
## [](#what-to-do-when-you-get-an-error)What to do when you get an error [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-8-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter8/section2.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter8/section2.ipynb) In this section we’ll look at some common errors that can occur when you’re trying to generate predictions from your freshly tuned Transformer model. This will prepare you for [section 4](/course/chapter8/section4), where we’ll explore how to debug the training phase itself. We’ve prepared a [template model repository](https://huggingface.co/lewtun/distilbert-base-uncased-finetuned-squad-d5716d28) for this section, and if you want to run the code in this chapter you’ll first need to copy the model to your account on the [Hugging Face Hub](https://huggingface.co/). To do so, first log in by running either the following in a Jupyter notebook: ``` from huggingface_hub import notebook_login notebook_login()``` or the following in your favorite terminal: This will prompt you to enter your username and password, and will save a token under _~/.cache/huggingface/_. Once you’ve logged in, you can copy the template repository with the following function: ``` from distutils.dir_util import copy_tree from huggingface_hub import Repository, snapshot_download, create_repo, get_full_repo_name def copy_repository_template(): template_repo_id = "lewtun/distilbert-base-uncased-finetuned-squad-d5716d28" commit_hash = "be3eaffc28669d7932492681cd5f3e8905e358b4" template_repo_dir = snapshot_download(template_repo_id, revision=commit_hash) model_name = template_repo_id.split("/")[1] create_repo(model_name, exist_ok=True) new_repo_id = get_full_repo_name(model_name) new_repo_dir = model_name repo = Repository(local_dir=new_repo_dir, clone_from=new_repo_id) copy_tree(template_repo_dir, new_repo_dir) repo.push_to_hub()``` Now when you call `copy_repository_template()`, it will create a copy of the template repository under your account. ## [](#debugging-the-pipeline-from-transformers)Debugging the pipeline from 🤗 Transformers To kick off our journey into the wonderful world of debugging Transformer models, consider the following scenario: you’re working with a colleague on a question answering project to help the customers of an e-commerce website find answers about consumer products. Your colleague shoots you a message like: > G’day! I just ran an experiment using the techniques in [Chapter 7](/course/chapter7/7) of the Hugging Face course and got some great results on SQuAD! I think we can use this model as a starting point for our project. The model ID on the Hub is “lewtun/distillbert-base-uncased-finetuned-squad-d5716d28”. Feel free to test it out :) and the first thing you think of is to load the model using the `pipeline` from 🤗 Transformers: ``` from transformers import pipeline model_checkpoint = get_full_repo_name("distillbert-base-uncased-finetuned-squad-d5716d28") reader = pipeline("question-answering", model=model_checkpoint)``` ``` """ OSError: Can't load config for 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28'. Make sure that: - 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28' is a correct model identifier listed on 'https://huggingface.co/models' - or 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28' is the correct path to a directory containing a config.json file """``` Oh no, something seems to have gone wrong! If you’re new to programming, these kind of errors can seem a bit cryptic at first (what even is an `OSError`?!). The error displayed here is just the last part of a much larger error report called a _Python traceback_ (aka stack trace). For example, if you’re running this code on Google Colab, you should see something like the following screenshot: ![A Python traceback.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/traceback.png) There’s a lot of information contained in these reports, so let’s walk through the key parts together. The first thing to note is that tracebacks should be read _from bottom to top_. This might sound weird if you’re used to reading English text from top to bottom, but it reflects the fact that the traceback shows the sequence of function calls that the `pipeline` makes when downloading the model and tokenizer. (Check out [Chapter 2](/course/chapter2) for more details on how the `pipeline` works under the hood.) 🚨 See that blue box around “6 frames” in the traceback from Google Colab? That’s a special feature of Colab, which compresses the traceback into “frames.” If you can’t seem to find the source of an error, make sure you expand the full traceback by clicking on those two little arrows. This means that the last line of the traceback indicates the last error message and gives the name of the exception that was raised. In this case, the exception type is `OSError`, which indicates a system-related error. If we read the accompanying error message, we can see that there seems to be a problem with the model’s _config.json_ file, and we’re given two suggestions to fix it: ``` """ Make sure that: - 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28' is a correct model identifier listed on 'https://huggingface.co/models' - or 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28' is the correct path to a directory containing a config.json file """``` 💡 If you encounter an error message that is difficult to understand, just copy and paste the message into the Google or [Stack Overflow](https://stackoverflow.com/) search bar (yes, really!). There’s a good chance that you’re not the first person to encounter the error, and this is a good way to find solutions that others in the community have posted. For example, searching for `OSError: Can't load config for` on Stack Overflow gives several [hits](https://stackoverflow.com/search?q=OSError%3A+Can%27t+load+config+for+) that could be used as a starting point for solving the problem. The first suggestion is asking us to check whether the model ID is actually correct, so the first order of business is to copy the identifier and paste it into the Hub’s search bar: ![The wrong model name.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/wrong-model-id.png) Hmm, it indeed looks like our colleague’s model is not on the Hub… aha, but there’s a typo in the name of the model! DistilBERT only has one “l” in its name, so let’s fix that and look for “lewtun/distilbert-base-uncased-finetuned-squad-d5716d28” instead: ![The right model name.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/true-model-id.png) Okay, this got a hit. Now let’s try to download the model again with the correct model ID: ``` model_checkpoint = get_full_repo_name("distilbert-base-uncased-finetuned-squad-d5716d28") reader = pipeline("question-answering", model=model_checkpoint)``` ``` """ OSError: Can't load config for 'lewtun/distilbert-base-uncased-finetuned-squad-d5716d28'. Make sure that: - 'lewtun/distilbert-base-uncased-finetuned-squad-d5716d28' is a correct model identifier listed on 'https://huggingface.co/models' - or 'lewtun/distilbert-base-uncased-finetuned-squad-d5716d28' is the correct path to a directory containing a config.json file """``` Argh, foiled again — welcome to the daily life of a machine learning engineer! Since we’ve fixed the model ID, the problem must lie in the repository itself. A quick way to access the contents of a repository on the 🤗 Hub is via the `list_repo_files()` function of the `huggingface_hub` library: ``` from huggingface_hub import list_repo_files list_repo_files(repo_id=model_checkpoint)``` ``` ['.gitattributes', 'README.md', 'pytorch_model.bin', 'special_tokens_map.json', 'tokenizer_config.json', 'training_args.bin', 'vocab.txt']``` Interesting — there doesn’t seem to be a _config.json_ file in the repository! No wonder our `pipeline` couldn’t load the model; our colleague must have forgotten to push this file to the Hub after they fine-tuned it. In this case, the problem seems pretty straightforward to fix: we could ask them to add the file, or, since we can see from the model ID that the pretrained model used was [`distilbert-base-uncased`](https://huggingface.co/distilbert-base-uncased), we can download the config for this model and push it to our repo to see if that resolves the problem. Let’s try that. Using the techniques we learned in [Chapter 2](/course/chapter2), we can download the model’s configuration with the `AutoConfig` class: ``` from transformers import AutoConfig pretrained_checkpoint = "distilbert-base-uncased" config = AutoConfig.from_pretrained(pretrained_checkpoint)``` 🚨 The approach we’re taking here is not foolproof, since our colleague may have tweaked the configuration of `distilbert-base-uncased` before fine-tuning the model. In real life, we’d want to check with them first, but for the purposes of this section we’ll assume they used the default configuration. We can then push this to our model repository with the configuration’s `push_to_hub()` function: ``` config.push_to_hub(model_checkpoint, commit_message="Add config.json")``` Now we can test if this worked by loading the model from the latest commit on the `main` branch: ``` reader = pipeline("question-answering", model=model_checkpoint, revision="main") context = r""" Extractive Question Answering is the task of extracting an answer from a text given a question. An example of a question answering dataset is the SQuAD dataset, which is entirely based on that task. If you would like to fine-tune a model on a SQuAD task, you may leverage the examples/pytorch/question-answering/run_squad.py script. 🤗 Transformers is interoperable with the PyTorch, TensorFlow, and JAX frameworks, so you can use your favourite tools for a wide variety of tasks! """ question = "What is extractive question answering?" reader(question=question, context=context)``` ``` {'score': 0.38669535517692566, 'start': 34, 'end': 95, 'answer': 'the task of extracting an answer from a text given a question'}``` Woohoo, it worked! Let’s recap what you’ve just learned: - The error messages in Python are known as _tracebacks_ and are read from bottom to top. The last line of the error message usually contains the information you need to locate the source of the problem. - If the last line does not contain sufficient information, work your way up the traceback and see if you can identify where in the source code the error occurs. - If none of the error messages can help you debug the problem, try searching online for a solution to a similar issue. - The `huggingface_hub` // 🤗 Hub? library provides a suite of tools that you can use to interact with and debug repositories on the Hub. Now that you know how to debug a pipeline, let’s take a look at a trickier example in the forward pass of the model itself. ## [](#debugging-the-forward-pass-of-your-model)Debugging the forward pass of your model Although the `pipeline` is great for most applications where you need to quickly generate predictions, sometimes you’ll need to access the model’s logits (say, if you have some custom post-processing that you’d like to apply). To see what can go wrong in this case, let’s first grab the model and tokenizer from our `pipeline`: ``` tokenizer = reader.tokenizer model = reader.model``` Next we need a question, so let’s see if our favorite frameworks are supported: ``` question = "Which frameworks can I use?"``` As we saw in [Chapter 7](/course/chapter7), the usual steps we need to take are tokenizing the inputs, extracting the logits of the start and end tokens, and then decoding the answer span: ``` import torch inputs = tokenizer(question, context, add_special_tokens=True) input_ids = inputs["input_ids"][0] outputs = model(**inputs) answer_start_scores = outputs.start_logits answer_end_scores = outputs.end_logits answer_start = torch.argmax(answer_start_scores) answer_end = torch.argmax(answer_end_scores) + 1 answer = tokenizer.convert_tokens_to_string( tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]) ) print(f"Question: {question}") print(f"Answer: {answer}")``` ``` """ --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /var/folders/28/k4cy5q7s2hs92xq7_h89_vgm0000gn/T/ipykernel_75743/2725838073.py in <module> 1 inputs = tokenizer(question, text, add_special_tokens=True) 2 input_ids = inputs["input_ids"] ----> 3 outputs = model(**inputs) 4 answer_start_scores = outputs.start_logits 5 answer_end_scores = outputs.end_logits ~/miniconda3/envs/huggingface/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs) 1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1050 or _global_forward_hooks or _global_forward_pre_hooks): -> 1051 return forward_call(*input, **kwargs) 1052 # Do not call functions when jit is used 1053 full_backward_hooks, non_full_backward_hooks = [], [] ~/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/models/distilbert/modeling_distilbert.py in forward(self, input_ids, attention_mask, head_mask, inputs_embeds, start_positions, end_positions, output_attentions, output_hidden_states, return_dict) 723 return_dict = return_dict if return_dict is not None else self.config.use_return_dict 724 --> 725 distilbert_output = self.distilbert( 726 input_ids=input_ids, 727 attention_mask=attention_mask, ~/miniconda3/envs/huggingface/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs) 1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1050 or _global_forward_hooks or _global_forward_pre_hooks): -> 1051 return forward_call(*input, **kwargs) 1052 # Do not call functions when jit is used 1053 full_backward_hooks, non_full_backward_hooks = [], [] ~/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/models/distilbert/modeling_distilbert.py in forward(self, input_ids, attention_mask, head_mask, inputs_embeds, output_attentions, output_hidden_states, return_dict) 471 raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") 472 elif input_ids is not None: --> 473 input_shape = input_ids.size() 474 elif inputs_embeds is not None: 475 input_shape = inputs_embeds.size()[:-1] AttributeError: 'list' object has no attribute 'size' """``` Oh dear, it looks like we have a bug in our code! But we’re not afraid of a little debugging. You can use the Python debugger in a notebook: or in a terminal: Here, reading the error message tells us that `'list' object has no attribute 'size'`, and we can see a `-->` arrow pointing to the line where the problem was raised in `model(**inputs)`.You can debug this interactively using the Python debugger, but for now we’ll simply print out a slice of `inputs` to see what we have: ``` [101, 2029, 7705, 2015, 2064]``` This certainly looks like an ordinary Python `list`, but let’s double-check the type: ``` type(inputs["input_ids"])``` Yep, that’s a Python `list` for sure. So what went wrong? Recall from [Chapter 2](/course/chapter2) that the `AutoModelForXxx` classes in 🤗 Transformers operate on _tensors_ (either in PyTorch or TensorFlow), and a common operation is to extract the dimensions of a tensor using `Tensor.size()` in, say, PyTorch. Let’s take another look at the traceback, to see which line triggered the exception: ``` ~/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/models/distilbert/modeling_distilbert.py in forward(self, input_ids, attention_mask, head_mask, inputs_embeds, output_attentions, output_hidden_states, return_dict) 471 raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") 472 elif input_ids is not None: --> 473 input_shape = input_ids.size() 474 elif inputs_embeds is not None: 475 input_shape = inputs_embeds.size()[:-1] AttributeError: 'list' object has no attribute 'size'``` It looks like our code tried to call `input_ids.size()`, but this clearly won’t work for a Python `list`, which is just a container. How can we solve this problem? Searching for the error message on Stack Overflow gives quite a few relevant [hits](https://stackoverflow.com/search?q=AttributeError%3A+%27list%27+object+has+no+attribute+%27size%27&s=c15ec54c-63cb-481d-a749-408920073e8f). Clicking on the first one displays a similar question to ours, with the answer shown in the screenshot below: ![An answer from Stack Overflow.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/stack-overflow.png) The answer recommends that we add `return_tensors='pt'` to the tokenizer, so let’s see if that works for us: ``` inputs = tokenizer(question, context, add_special_tokens=True, return_tensors="pt") input_ids = inputs["input_ids"][0] outputs = model(**inputs) answer_start_scores = outputs.start_logits answer_end_scores = outputs.end_logits answer_start = torch.argmax(answer_start_scores) answer_end = torch.argmax(answer_end_scores) + 1 answer = tokenizer.convert_tokens_to_string( tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]) ) print(f"Question: {question}") print(f"Answer: {answer}")``` ``` """ Question: Which frameworks can I use? Answer: pytorch, tensorflow, and jax """``` Nice, it worked! This is a great example of how useful Stack Overflow can be: by identifying a similar problem, we were able to benefit from the experience of others in the community. However, a search like this won’t always yield a relevant answer, so what can you do in such cases? Fortunately, there is a welcoming community of developers on the [Hugging Face forums](https://discuss.huggingface.co/) that can help you out! In the next section, we’ll take a look at how you can craft good forum questions that are likely to get answered.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="What to do when you get an error - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter8/2"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>What to do when you get an error - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;what-to-do-when-you-get-an-error&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;debugging-the-pipeline-from-transformers&quot;,&quot;title&quot;:&quot;Debugging the pipeline from 🤗 Transformers&quot;},{&quot;local&quot;:&quot;debugging-the-forward-pass-of-your-model&quot;,&quot;title&quot;:&quot;Debugging the forward pass of your model&quot;}],&quot;title&quot;:&quot;What to do when you get an error&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter8/2&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;What to do when you get an error&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">What to do when you get an error</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/1?fw=pt">Introduction </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter8/2?fw=pt">What to do when you get an error </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/3?fw=pt">Asking for help on the forums </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/4?fw=pt">Debugging the training pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/5?fw=pt">How to write a good issue </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/6?fw=pt">Part 2 completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/7?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="what-to-do-when-you-get-an-error" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#what-to-do-when-you-get-an-error"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>What to do when you get an error</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-8-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter8/section2.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter8/section2.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>In this section we’ll look at some common errors that can occur when you’re trying to generate predictions from your freshly tuned Transformer model. This will prepare you for <a href="/course/chapter8/section4">section 4</a>, where we’ll explore how to debug the training phase itself.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/DQ-CpJn6Rc4" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>We’ve prepared a <a href="https://huggingface.co/lewtun/distilbert-base-uncased-finetuned-squad-d5716d28" rel="nofollow">template model repository</a> for this section, and if you want to run the code in this chapter you’ll first need to copy the model to your account on the <a href="https://huggingface.co" rel="nofollow">Hugging Face Hub</a>. To do so, first log in by running either the following in a Jupyter notebook:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login notebook_login()</pre></div> <p>or the following in your favorite terminal:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-cli login</pre></div> <p>This will prompt you to enter your username and password, and will save a token under <em>~/.cache/huggingface/</em>. Once you’ve logged in, you can copy the template repository with the following function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> distutils.dir_util <span class="hljs-keyword">import</span> copy_tree <span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> Repository, snapshot_download, create_repo, get_full_repo_name <span class="hljs-keyword">def</span> <span class="hljs-title function_">copy_repository_template</span>(): <span class="hljs-comment"># Clone the repo and extract the local path</span> template_repo_id = <span class="hljs-string">"lewtun/distilbert-base-uncased-finetuned-squad-d5716d28"</span> commit_hash = <span class="hljs-string">"be3eaffc28669d7932492681cd5f3e8905e358b4"</span> template_repo_dir = snapshot_download(template_repo_id, revision=commit_hash) <span class="hljs-comment"># Create an empty repo on the Hub</span> model_name = template_repo_id.split(<span class="hljs-string">"/"</span>)[<span class="hljs-number">1</span>] create_repo(model_name, exist_ok=<span class="hljs-literal">True</span>) <span class="hljs-comment"># Clone the empty repo</span> new_repo_id = get_full_repo_name(model_name) new_repo_dir = model_name repo = Repository(local_dir=new_repo_dir, clone_from=new_repo_id) <span class="hljs-comment"># Copy files</span> copy_tree(template_repo_dir, new_repo_dir) <span class="hljs-comment"># Push to Hub</span> repo.push_to_hub()</pre></div> <p>Now when you call <code>copy_repository_template()</code>, it will create a copy of the template repository under your account.</p> <h2 class="relative group"><a id="debugging-the-pipeline-from-transformers" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#debugging-the-pipeline-from-transformers"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Debugging the pipeline from 🤗 Transformers</span></h2> <p>To kick off our journey into the wonderful world of debugging Transformer models, consider the following scenario: you’re working with a colleague on a question answering project to help the customers of an e-commerce website find answers about consumer products. Your colleague shoots you a message like:</p> <blockquote><p>G’day! I just ran an experiment using the techniques in <a href="/course/chapter7/7">Chapter 7</a> of the Hugging Face course and got some great results on SQuAD! I think we can use this model as a starting point for our project. The model ID on the Hub is “lewtun/distillbert-base-uncased-finetuned-squad-d5716d28”. Feel free to test it out :)</p></blockquote> <p>and the first thing you think of is to load the model using the <code>pipeline</code> from 🤗 Transformers:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline model_checkpoint = get_full_repo_name(<span class="hljs-string">"distillbert-base-uncased-finetuned-squad-d5716d28"</span>) reader = pipeline(<span class="hljs-string">"question-answering"</span>, model=model_checkpoint)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">""" OSError: Can't load config for 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28'. Make sure that: - 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28' is a correct model identifier listed on 'https://huggingface.co/models' - or 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28' is the correct path to a directory containing a config.json file """</span></pre></div> <p>Oh no, something seems to have gone wrong! If you’re new to programming, these kind of errors can seem a bit cryptic at first (what even is an <code>OSError</code>?!). The error displayed here is just the last part of a much larger error report called a <em>Python traceback</em> (aka stack trace). For example, if you’re running this code on Google Colab, you should see something like the following screenshot:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/traceback.png" alt="A Python traceback." width="100%"></div> <p>There’s a lot of information contained in these reports, so let’s walk through the key parts together. The first thing to note is that tracebacks should be read <em>from bottom to top</em>. This might sound weird if you’re used to reading English text from top to bottom, but it reflects the fact that the traceback shows the sequence of function calls that the <code>pipeline</code> makes when downloading the model and tokenizer. (Check out <a href="/course/chapter2">Chapter 2</a> for more details on how the <code>pipeline</code> works under the hood.)</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>🚨 See that blue box around “6 frames” in the traceback from Google Colab? That’s a special feature of Colab, which compresses the traceback into “frames.” If you can’t seem to find the source of an error, make sure you expand the full traceback by clicking on those two little arrows.</p></div> <p>This means that the last line of the traceback indicates the last error message and gives the name of the exception that was raised. In this case, the exception type is <code>OSError</code>, which indicates a system-related error. If we read the accompanying error message, we can see that there seems to be a problem with the model’s <em>config.json</em> file, and we’re given two suggestions to fix it:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">""" Make sure that: - 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28' is a correct model identifier listed on 'https://huggingface.co/models' - or 'lewtun/distillbert-base-uncased-finetuned-squad-d5716d28' is the correct path to a directory containing a config.json file """</span></pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 If you encounter an error message that is difficult to understand, just copy and paste the message into the Google or <a href="https://stackoverflow.com/" rel="nofollow">Stack Overflow</a> search bar (yes, really!). There’s a good chance that you’re not the first person to encounter the error, and this is a good way to find solutions that others in the community have posted. For example, searching for <code>OSError: Can't load config for</code> on Stack Overflow gives several <a href="https://stackoverflow.com/search?q=OSError%3A+Can%27t+load+config+for+" rel="nofollow">hits</a> that could be used as a starting point for solving the problem.</p></div> <p>The first suggestion is asking us to check whether the model ID is actually correct, so the first order of business is to copy the identifier and paste it into the Hub’s search bar:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/wrong-model-id.png" alt="The wrong model name." width="100%"></div> <p>Hmm, it indeed looks like our colleague’s model is not on the Hub… aha, but there’s a typo in the name of the model! DistilBERT only has one “l” in its name, so let’s fix that and look for “lewtun/distilbert-base-uncased-finetuned-squad-d5716d28” instead:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/true-model-id.png" alt="The right model name." width="100%"></div> <p>Okay, this got a hit. Now let’s try to download the model again with the correct model ID:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model_checkpoint = get_full_repo_name(<span class="hljs-string">"distilbert-base-uncased-finetuned-squad-d5716d28"</span>) reader = pipeline(<span class="hljs-string">"question-answering"</span>, model=model_checkpoint)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">""" OSError: Can't load config for 'lewtun/distilbert-base-uncased-finetuned-squad-d5716d28'. Make sure that: - 'lewtun/distilbert-base-uncased-finetuned-squad-d5716d28' is a correct model identifier listed on 'https://huggingface.co/models' - or 'lewtun/distilbert-base-uncased-finetuned-squad-d5716d28' is the correct path to a directory containing a config.json file """</span></pre></div> <p>Argh, foiled again — welcome to the daily life of a machine learning engineer! Since we’ve fixed the model ID, the problem must lie in the repository itself. A quick way to access the contents of a repository on the 🤗 Hub is via the <code>list_repo_files()</code> function of the <code>huggingface_hub</code> library:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> list_repo_files list_repo_files(repo_id=model_checkpoint)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'.gitattributes'</span>, <span class="hljs-string">'README.md'</span>, <span class="hljs-string">'pytorch_model.bin'</span>, <span class="hljs-string">'special_tokens_map.json'</span>, <span class="hljs-string">'tokenizer_config.json'</span>, <span class="hljs-string">'training_args.bin'</span>, <span class="hljs-string">'vocab.txt'</span>]</pre></div> <p>Interesting — there doesn’t seem to be a <em>config.json</em> file in the repository! No wonder our <code>pipeline</code> couldn’t load the model; our colleague must have forgotten to push this file to the Hub after they fine-tuned it. In this case, the problem seems pretty straightforward to fix: we could ask them to add the file, or, since we can see from the model ID that the pretrained model used was <a href="https://huggingface.co/distilbert-base-uncased" rel="nofollow"><code>distilbert-base-uncased</code></a>, we can download the config for this model and push it to our repo to see if that resolves the problem. Let’s try that. Using the techniques we learned in <a href="/course/chapter2">Chapter 2</a>, we can download the model’s configuration with the <code>AutoConfig</code> class:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoConfig pretrained_checkpoint = <span class="hljs-string">"distilbert-base-uncased"</span> config = AutoConfig.from_pretrained(pretrained_checkpoint)</pre></div> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>🚨 The approach we’re taking here is not foolproof, since our colleague may have tweaked the configuration of <code>distilbert-base-uncased</code> before fine-tuning the model. In real life, we’d want to check with them first, but for the purposes of this section we’ll assume they used the default configuration.</p></div> <p>We can then push this to our model repository with the configuration’s <code>push_to_hub()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>config.push_to_hub(model_checkpoint, commit_message=<span class="hljs-string">"Add config.json"</span>)</pre></div> <p>Now we can test if this worked by loading the model from the latest commit on the <code>main</code> branch:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>reader = pipeline(<span class="hljs-string">"question-answering"</span>, model=model_checkpoint, revision=<span class="hljs-string">"main"</span>) context = <span class="hljs-string">r""" Extractive Question Answering is the task of extracting an answer from a text given a question. An example of a question answering dataset is the SQuAD dataset, which is entirely based on that task. If you would like to fine-tune a model on a SQuAD task, you may leverage the examples/pytorch/question-answering/run_squad.py script. 🤗 Transformers is interoperable with the PyTorch, TensorFlow, and JAX frameworks, so you can use your favourite tools for a wide variety of tasks! """</span> question = <span class="hljs-string">"What is extractive question answering?"</span> reader(question=question, context=context)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'score'</span>: <span class="hljs-number">0.38669535517692566</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">34</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">95</span>, <span class="hljs-string">'answer'</span>: <span class="hljs-string">'the task of extracting an answer from a text given a question'</span>}</pre></div> <p>Woohoo, it worked! Let’s recap what you’ve just learned:</p> <ul><li>The error messages in Python are known as <em>tracebacks</em> and are read from bottom to top. The last line of the error message usually contains the information you need to locate the source of the problem.</li> <li>If the last line does not contain sufficient information, work your way up the traceback and see if you can identify where in the source code the error occurs.</li> <li>If none of the error messages can help you debug the problem, try searching online for a solution to a similar issue.</li> <li>The <code>huggingface_hub</code> // 🤗 Hub? library provides a suite of tools that you can use to interact with and debug repositories on the Hub.</li></ul> <p>Now that you know how to debug a pipeline, let’s take a look at a trickier example in the forward pass of the model itself.</p> <h2 class="relative group"><a id="debugging-the-forward-pass-of-your-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#debugging-the-forward-pass-of-your-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Debugging the forward pass of your model</span></h2> <p>Although the <code>pipeline</code> is great for most applications where you need to quickly generate predictions, sometimes you’ll need to access the model’s logits (say, if you have some custom post-processing that you’d like to apply). To see what can go wrong in this case, let’s first grab the model and tokenizer from our <code>pipeline</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer = reader.tokenizer model = reader.model</pre></div> <p>Next we need a question, so let’s see if our favorite frameworks are supported:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>question = <span class="hljs-string">"Which frameworks can I use?"</span></pre></div> <p>As we saw in <a href="/course/chapter7">Chapter 7</a>, the usual steps we need to take are tokenizing the inputs, extracting the logits of the start and end tokens, and then decoding the answer span:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch inputs = tokenizer(question, context, add_special_tokens=<span class="hljs-literal">True</span>) input_ids = inputs[<span class="hljs-string">"input_ids"</span>][<span class="hljs-number">0</span>] outputs = model(**inputs) answer_start_scores = outputs.start_logits answer_end_scores = outputs.end_logits <span class="hljs-comment"># Get the most likely beginning of answer with the argmax of the score</span> answer_start = torch.argmax(answer_start_scores) <span class="hljs-comment"># Get the most likely end of answer with the argmax of the score</span> answer_end = torch.argmax(answer_end_scores) + <span class="hljs-number">1</span> answer = tokenizer.convert_tokens_to_string( tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]) ) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Question: <span class="hljs-subst">{question}</span>"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Answer: <span class="hljs-subst">{answer}</span>"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">""" --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /var/folders/28/k4cy5q7s2hs92xq7_h89_vgm0000gn/T/ipykernel_75743/2725838073.py in &lt;module&gt; 1 inputs = tokenizer(question, text, add_special_tokens=True) 2 input_ids = inputs["input_ids"] ----&gt; 3 outputs = model(**inputs) 4 answer_start_scores = outputs.start_logits 5 answer_end_scores = outputs.end_logits ~/miniconda3/envs/huggingface/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs) 1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1050 or _global_forward_hooks or _global_forward_pre_hooks): -&gt; 1051 return forward_call(*input, **kwargs) 1052 # Do not call functions when jit is used 1053 full_backward_hooks, non_full_backward_hooks = [], [] ~/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/models/distilbert/modeling_distilbert.py in forward(self, input_ids, attention_mask, head_mask, inputs_embeds, start_positions, end_positions, output_attentions, output_hidden_states, return_dict) 723 return_dict = return_dict if return_dict is not None else self.config.use_return_dict 724 --&gt; 725 distilbert_output = self.distilbert( 726 input_ids=input_ids, 727 attention_mask=attention_mask, ~/miniconda3/envs/huggingface/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs) 1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1050 or _global_forward_hooks or _global_forward_pre_hooks): -&gt; 1051 return forward_call(*input, **kwargs) 1052 # Do not call functions when jit is used 1053 full_backward_hooks, non_full_backward_hooks = [], [] ~/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/models/distilbert/modeling_distilbert.py in forward(self, input_ids, attention_mask, head_mask, inputs_embeds, output_attentions, output_hidden_states, return_dict) 471 raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") 472 elif input_ids is not None: --&gt; 473 input_shape = input_ids.size() 474 elif inputs_embeds is not None: 475 input_shape = inputs_embeds.size()[:-1] AttributeError: 'list' object has no attribute 'size' """</span></pre></div> <p>Oh dear, it looks like we have a bug in our code! But we’re not afraid of a little debugging. You can use the Python debugger in a notebook:</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/rSPyvPw0p9k" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>or in a terminal:</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/5PkZ4rbHL6c" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Here, reading the error message tells us that <code>'list' object has no attribute 'size'</code>, and we can see a <code>--&gt;</code> arrow pointing to the line where the problem was raised in <code>model(**inputs)</code>.You can debug this interactively using the Python debugger, but for now we’ll simply print out a slice of <code>inputs</code> to see what we have:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs[<span class="hljs-string">"input_ids"</span>][:<span class="hljs-number">5</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">101</span>, <span class="hljs-number">2029</span>, <span class="hljs-number">7705</span>, <span class="hljs-number">2015</span>, <span class="hljs-number">2064</span>]</pre></div> <p>This certainly looks like an ordinary Python <code>list</code>, but let’s double-check the type:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">type</span>(inputs[<span class="hljs-string">"input_ids"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">list</span></pre></div> <p>Yep, that’s a Python <code>list</code> for sure. So what went wrong? Recall from <a href="/course/chapter2">Chapter 2</a> that the <code>AutoModelForXxx</code> classes in 🤗 Transformers operate on <em>tensors</em> (either in PyTorch or TensorFlow), and a common operation is to extract the dimensions of a tensor using <code>Tensor.size()</code> in, say, PyTorch. Let’s take another look at the traceback, to see which line triggered the exception:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>~<span class="hljs-regexp">/miniconda3/</span>envs<span class="hljs-regexp">/huggingface/</span>lib<span class="hljs-regexp">/python3.8/</span>site-packages<span class="hljs-regexp">/transformers/m</span>odels<span class="hljs-regexp">/distilbert/m</span>odeling_distilbert.py in forward(self, input_ids, attention_mask, head_mask, inputs_embeds, output_attentions, output_hidden_states, return_dict) <span class="hljs-number">471</span> raise ValueError(<span class="hljs-string">"You cannot specify both input_ids and inputs_embeds at the same time"</span>) <span class="hljs-number">472</span> elif input_ids is not None: --&gt; <span class="hljs-number">473</span> input_shape = input_ids.<span class="hljs-keyword">size</span>() <span class="hljs-number">474</span> elif inputs_embeds is not None: <span class="hljs-number">475</span> input_shape = inputs_embeds.<span class="hljs-keyword">size</span>()[:-<span class="hljs-number">1</span>] AttributeError: <span class="hljs-string">'list'</span> object has no attribute <span class="hljs-string">'size'</span></pre></div> <p>It looks like our code tried to call <code>input_ids.size()</code>, but this clearly won’t work for a Python <code>list</code>, which is just a container. How can we solve this problem? Searching for the error message on Stack Overflow gives quite a few relevant <a href="https://stackoverflow.com/search?q=AttributeError%3A+%27list%27+object+has+no+attribute+%27size%27&amp;s=c15ec54c-63cb-481d-a749-408920073e8f" rel="nofollow">hits</a>. Clicking on the first one displays a similar question to ours, with the answer shown in the screenshot below:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/stack-overflow.png" alt="An answer from Stack Overflow." width="100%"></div> <p>The answer recommends that we add <code>return_tensors='pt'</code> to the tokenizer, so let’s see if that works for us:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>inputs = tokenizer(question, context, add_special_tokens=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">"pt"</span>) input_ids = inputs[<span class="hljs-string">"input_ids"</span>][<span class="hljs-number">0</span>] outputs = model(**inputs) answer_start_scores = outputs.start_logits answer_end_scores = outputs.end_logits <span class="hljs-comment"># Get the most likely beginning of answer with the argmax of the score</span> answer_start = torch.argmax(answer_start_scores) <span class="hljs-comment"># Get the most likely end of answer with the argmax of the score</span> answer_end = torch.argmax(answer_end_scores) + <span class="hljs-number">1</span> answer = tokenizer.convert_tokens_to_string( tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]) ) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Question: <span class="hljs-subst">{question}</span>"</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Answer: <span class="hljs-subst">{answer}</span>"</span>)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">""" Question: Which frameworks can I use? Answer: pytorch, tensorflow, and jax """</span></pre></div> <p>Nice, it worked! This is a great example of how useful Stack Overflow can be: by identifying a similar problem, we were able to benefit from the experience of others in the community. However, a search like this won’t always yield a relevant answer, so what can you do in such cases? Fortunately, there is a welcoming community of developers on the <a href="https://discuss.huggingface.co/" rel="nofollow">Hugging Face forums</a> that can help you out! In the next section, we’ll take a look at how you can craft good forum questions that are likely to get answered.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter8/1?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Introduction</a> <a href="/learn/nlp-course/chapter8/3?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Asking for help on the forums<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;what-to-do-when-you-get-an-error&quot;,&quot;url&quot;:&quot;#what-to-do-when-you-get-an-error&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Debugging the pipeline from 🤗 Transformers&quot;,&quot;id&quot;:&quot;debugging-the-pipeline-from-transformers&quot;,&quot;url&quot;:&quot;#debugging-the-pipeline-from-transformers&quot;},{&quot;title&quot;:&quot;Debugging the forward pass of your model&quot;,&quot;id&quot;:&quot;debugging-the-forward-pass-of-your-model&quot;,&quot;url&quot;:&quot;#debugging-the-forward-pass-of-your-model&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#what-to-do-when-you-get-an-error" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-what-to-do-when-you-get-an-error"><wbr>What to do when you get an error</a> <a href="#debugging-the-pipeline-from-transformers" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-debugging-the-pipeline-from-transformers"><wbr>Debugging the pipeline from 🤗 <wbr>Transformers</a> <a href="#debugging-the-forward-pass-of-your-model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-debugging-the-forward-pass-of-your-model"><wbr>Debugging the forward pass of your model</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter8/2" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter8/2"); } </script> <iframe name="__privateStripeMetricsController1900" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter8%2F2%3Ffw%3Dpt&amp;title=What%20to%20do%20when%20you%20get%20an%20error%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:33.485Z
Asking for help on the forums - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter8/3?fw=pt
## [](#asking-for-help-on-the-forums)Asking for help on the forums [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-8-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter8/section3.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter8/section3.ipynb) The [Hugging Face forums](https://discuss.huggingface.co/) are a great place to get help from the open source team and wider Hugging Face community. Here’s what the main page looks like on any given day: ![The Hugging Face forums.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forums.png) On the lefthand side you can see all the categories that the various topics are grouped into, while the righthand side shows the most recent topics. A topic is a post that contains a title, category, and description; it’s quite similar to the GitHub issues format that we saw when creating our own dataset in [Chapter 5](/course/chapter5). As the name suggests, the [Beginners](https://discuss.huggingface.co/c/beginners/5) category is primarily intended for people just starting out with the Hugging Face libraries and ecosystem. Any question on any of the libraries is welcome there, be it to debug some code or to ask for help about how to do something. (That said, if your question concerns one library in particular, you should probably head to the corresponding library category on the forum.) Similarly, the [Intermediate](https://discuss.huggingface.co/c/intermediate/6) and [Research](https://discuss.huggingface.co/c/research/7) categories are for more advanced questions, for example about the libraries or some cool new NLP research that you’d like to discuss. And naturally, we should also mention the [Course](https://discuss.huggingface.co/c/course/20) category, where you can ask any questions you have that are related to the Hugging Face course! Once you have selected a category, you’ll be ready to write your first topic. You can find some [guidelines](https://discuss.huggingface.co/t/how-to-request-support/3128) in the forum on how to do this, and in this section we’ll take a look at some features that make up a good topic. ## [](#writing-a-good-forum-post)Writing a good forum post As a running example, let’s suppose that we’re trying to generate embeddings from Wikipedia articles to create a custom search engine. As usual, we load the tokenizer and model as follows: ``` from transformers import AutoTokenizer, AutoModel model_checkpoint = "distilbert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) model = AutoModel.from_pretrained(model_checkpoint)``` Now suppose we try to embed a whole section of the [Wikipedia article](https://en.wikipedia.org/wiki/Transformers) on Transformers (the franchise, not the library!): ``` text = """ Generation One is a retroactive term for the Transformers characters that appeared between 1984 and 1993. The Transformers began with the 1980s Japanese toy lines Micro Change and Diaclone. They presented robots able to transform into everyday vehicles, electronic items or weapons. Hasbro bought the Micro Change and Diaclone toys, and partnered with Takara. Marvel Comics was hired by Hasbro to create the backstory; editor-in-chief Jim Shooter wrote an overall story, and gave the task of creating the characthers to writer Dennis O'Neil. Unhappy with O'Neil's work (although O'Neil created the name "Optimus Prime"), Shooter chose Bob Budiansky to create the characters. The Transformers mecha were largely designed by Shōji Kawamori, the creator of the Japanese mecha anime franchise Macross (which was adapted into the Robotech franchise in North America). Kawamori came up with the idea of transforming mechs while working on the Diaclone and Macross franchises in the early 1980s (such as the VF-1 Valkyrie in Macross and Robotech), with his Diaclone mechs later providing the basis for Transformers. The primary concept of Generation One is that the heroic Optimus Prime, the villainous Megatron, and their finest soldiers crash land on pre-historic Earth in the Ark and the Nemesis before awakening in 1985, Cybertron hurtling through the Neutral zone as an effect of the war. The Marvel comic was originally part of the main Marvel Universe, with appearances from Spider-Man and Nick Fury, plus some cameos, as well as a visit to the Savage Land. The Transformers TV series began around the same time. Produced by Sunbow Productions and Marvel Productions, later Hasbro Productions, from the start it contradicted Budiansky's backstories. The TV series shows the Autobots looking for new energy sources, and crash landing as the Decepticons attack. Marvel interpreted the Autobots as destroying a rogue asteroid approaching Cybertron. Shockwave is loyal to Megatron in the TV series, keeping Cybertron in a stalemate during his absence, but in the comic book he attempts to take command of the Decepticons. The TV series would also differ wildly from the origins Budiansky had created for the Dinobots, the Decepticon turned Autobot Jetfire (known as Skyfire on TV), the Constructicons (who combine to form Devastator),[19][20] and Omega Supreme. The Marvel comic establishes early on that Prime wields the Creation Matrix, which gives life to machines. In the second season, the two-part episode The Key to Vector Sigma introduced the ancient Vector Sigma computer, which served the same original purpose as the Creation Matrix (giving life to Transformers), and its guardian Alpha Trion. """ inputs = tokenizer(text, return_tensors="pt") logits = model(**inputs).logits``` ``` IndexError: index out of range in self``` Uh-oh, we’ve hit a problem — and the error message is far more cryptic than the ones we saw in [section 2](/course/chapter8/section2)! We can’t make head or tails of the full traceback, so we decide to turn to the Hugging Face forums for help. How might we craft the topic? To get started, we need to click the “New Topic” button at the upper-right corner (note that to create a topic, we’ll need to be logged in): ![Creating a new forum topic.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forums-new-topic.png) This brings up a writing interface where we can input the title of our topic, select a category, and draft the content: ![The interface for creating a forum topic.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forum-topic01.png) Since the error seems to be exclusively about 🤗 Transformers, we’ll select this for the category. Our first attempt at explaining the problem might look something like this: ![Drafting the content for a new forum topic.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forum-topic02.png) Although this topic contains the error message we need help with, there are a few problems with the way it is written: 1. The title is not very descriptive, so anyone browsing the forum won’t be able to tell what the topic is about without reading the body as well. 2. The body doesn’t provide enough information about _where_ the error is coming from and _how_ to reproduce it. 3. The topic tags a few people directly with a somewhat demanding tone. Topics like this one are not likely to get a fast answer (if they get one at all), so let’s look at how we can improve it. We’ll start with the first issue of picking a good title. ### [](#choosing-a-descriptive-title)Choosing a descriptive title If you’re trying to get help with a bug in your code, a good rule of thumb is to include enough information in the title so that others can quickly determine whether they think they can answer your question or not. In our running example, we know the name of the exception that’s being raised and have some hints that it’s triggered in the forward pass of the model, where we call `model(**inputs)`. To communicate this, one possible title could be: > Source of IndexError in the AutoModel forward pass? This title tells the reader _where_ you think the bug is coming from, and if they’ve encountered an `IndexError` before, there’s a good chance they’ll know how to debug it. Of course, the title can be anything you want, and other variations like: > Why does my model produce an IndexError? could also be fine. Now that we’ve got a descriptive title, let’s take a look at improving the body. ### [](#formatting-your-code-snippets)Formatting your code snippets Reading source code is hard enough in an IDE, but it’s even harder when the code is copied and pasted as plain text! Fortunately, the Hugging Face forums support the use of Markdown, so you should always enclose your code blocks with three backticks (\`\`\`) so it’s more easily readable. Let’s do this to prettify the error message — and while we’re at it, let’s make the body a bit more polite than our original version: ![Our revised forum topic, with proper code formatting.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forum-topic03.png) As you can see in the screenshot, enclosing the code blocks in backticks converts the raw text into formatted code, complete with color styling! Also note that single backticks can be used to format inline variables, like we’ve done for `distilbert-base-uncased`. This topic is looking much better, and with a bit of luck we might find someone in the community who can guess what the error is about. However, instead of relying on luck, let’s make life easier by including the traceback in its full gory detail! ### [](#including-the-full-traceback)Including the full traceback Since the last line of the traceback is often enough to debug your own code, it can be tempting to just provide that in your topic to “save space.” Although well intentioned, this actually makes it _harder_ for others to debug the problem since the information that’s higher up in the traceback can be really useful too. So, a good practice is to copy and paste the _whole_ traceback, while making sure that it’s nicely formatted. Since these tracebacks can get rather long, some people prefer to show them after they’ve explained the source code. Let’s do this. Now, our forum topic looks like the following: ![Our example forum topic, with the complete traceback.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forum-topic04.png) This is much more informative, and a careful reader might be able to point out that the problem seems to be due to passing a long input because of this line in the traceback: > Token indices sequence length is longer than the specified maximum sequence length for this model (583 > 512). However, we can make things even easier for them by providing the actual code that triggered the error. Let’s do that now. ### [](#providing-a-reproducible-example)Providing a reproducible example If you’ve ever tried to debug someone else’s code, you’ve probably first tried to recreate the problem they’ve reported so you can start working your way through the traceback to pinpoint the error. It’s no different when it comes to getting (or giving) assistance on the forums, so it really helps if you can provide a small example that reproduces the error. Half the time, simply walking through this exercise will help you figure out what’s going wrong. In any case, the missing piece of our example is to show the _inputs_ that we provided to the model. Doing that gives us something like the following completed example: ![The final version of our forum topic.](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forum-topic05.png) This topic now contains quite a lot of information, and it’s written in a way that is much more likely to attract the attention of the community and get a helpful answer. With these basic guidelines, you can now create great topics to find the answers to your 🤗 Transformers questions!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Asking for help on the forums - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter8/3"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Asking for help on the forums - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;asking-for-help-on-the-forums&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;writing-a-good-forum-post&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;choosing-a-descriptive-title&quot;,&quot;title&quot;:&quot;Choosing a descriptive title&quot;},{&quot;local&quot;:&quot;formatting-your-code-snippets&quot;,&quot;title&quot;:&quot;Formatting your code snippets&quot;},{&quot;local&quot;:&quot;including-the-full-traceback&quot;,&quot;title&quot;:&quot;Including the full traceback&quot;},{&quot;local&quot;:&quot;providing-a-reproducible-example&quot;,&quot;title&quot;:&quot;Providing a reproducible example&quot;}],&quot;title&quot;:&quot;Writing a good forum post&quot;}],&quot;title&quot;:&quot;Asking for help on the forums&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter8/3&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Asking for help on the forums&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Asking for help on the forums</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/2?fw=pt">What to do when you get an error </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter8/3?fw=pt">Asking for help on the forums </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/4?fw=pt">Debugging the training pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/5?fw=pt">How to write a good issue </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/6?fw=pt">Part 2 completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/7?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="asking-for-help-on-the-forums" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#asking-for-help-on-the-forums"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Asking for help on the forums</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-8-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter8/section3.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter8/section3.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/S2EEG3JIt2A" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The <a href="https://discuss.huggingface.co" rel="nofollow">Hugging Face forums</a> are a great place to get help from the open source team and wider Hugging Face community. Here’s what the main page looks like on any given day:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forums.png" alt="The Hugging Face forums." width="100%"></div> <p>On the lefthand side you can see all the categories that the various topics are grouped into, while the righthand side shows the most recent topics. A topic is a post that contains a title, category, and description; it’s quite similar to the GitHub issues format that we saw when creating our own dataset in <a href="/course/chapter5">Chapter 5</a>. As the name suggests, the <a href="https://discuss.huggingface.co/c/beginners/5" rel="nofollow">Beginners</a> category is primarily intended for people just starting out with the Hugging Face libraries and ecosystem. Any question on any of the libraries is welcome there, be it to debug some code or to ask for help about how to do something. (That said, if your question concerns one library in particular, you should probably head to the corresponding library category on the forum.)</p> <p>Similarly, the <a href="https://discuss.huggingface.co/c/intermediate/6" rel="nofollow">Intermediate</a> and <a href="https://discuss.huggingface.co/c/research/7" rel="nofollow">Research</a> categories are for more advanced questions, for example about the libraries or some cool new NLP research that you’d like to discuss.</p> <p>And naturally, we should also mention the <a href="https://discuss.huggingface.co/c/course/20" rel="nofollow">Course</a> category, where you can ask any questions you have that are related to the Hugging Face course!</p> <p>Once you have selected a category, you’ll be ready to write your first topic. You can find some <a href="https://discuss.huggingface.co/t/how-to-request-support/3128" rel="nofollow">guidelines</a> in the forum on how to do this, and in this section we’ll take a look at some features that make up a good topic.</p> <h2 class="relative group"><a id="writing-a-good-forum-post" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#writing-a-good-forum-post"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Writing a good forum post</span></h2> <p>As a running example, let’s suppose that we’re trying to generate embeddings from Wikipedia articles to create a custom search engine. As usual, we load the tokenizer and model as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModel model_checkpoint = <span class="hljs-string">"distilbert-base-uncased"</span> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) model = AutoModel.from_pretrained(model_checkpoint)</pre></div> <p>Now suppose we try to embed a whole section of the <a href="https://en.wikipedia.org/wiki/Transformers" rel="nofollow">Wikipedia article</a> on Transformers (the franchise, not the library!):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>text = <span class="hljs-string">""" Generation One is a retroactive term for the Transformers characters that appeared between 1984 and 1993. The Transformers began with the 1980s Japanese toy lines Micro Change and Diaclone. They presented robots able to transform into everyday vehicles, electronic items or weapons. Hasbro bought the Micro Change and Diaclone toys, and partnered with Takara. Marvel Comics was hired by Hasbro to create the backstory; editor-in-chief Jim Shooter wrote an overall story, and gave the task of creating the characthers to writer Dennis O'Neil. Unhappy with O'Neil's work (although O'Neil created the name "Optimus Prime"), Shooter chose Bob Budiansky to create the characters. The Transformers mecha were largely designed by Shōji Kawamori, the creator of the Japanese mecha anime franchise Macross (which was adapted into the Robotech franchise in North America). Kawamori came up with the idea of transforming mechs while working on the Diaclone and Macross franchises in the early 1980s (such as the VF-1 Valkyrie in Macross and Robotech), with his Diaclone mechs later providing the basis for Transformers. The primary concept of Generation One is that the heroic Optimus Prime, the villainous Megatron, and their finest soldiers crash land on pre-historic Earth in the Ark and the Nemesis before awakening in 1985, Cybertron hurtling through the Neutral zone as an effect of the war. The Marvel comic was originally part of the main Marvel Universe, with appearances from Spider-Man and Nick Fury, plus some cameos, as well as a visit to the Savage Land. The Transformers TV series began around the same time. Produced by Sunbow Productions and Marvel Productions, later Hasbro Productions, from the start it contradicted Budiansky's backstories. The TV series shows the Autobots looking for new energy sources, and crash landing as the Decepticons attack. Marvel interpreted the Autobots as destroying a rogue asteroid approaching Cybertron. Shockwave is loyal to Megatron in the TV series, keeping Cybertron in a stalemate during his absence, but in the comic book he attempts to take command of the Decepticons. The TV series would also differ wildly from the origins Budiansky had created for the Dinobots, the Decepticon turned Autobot Jetfire (known as Skyfire on TV), the Constructicons (who combine to form Devastator),[19][20] and Omega Supreme. The Marvel comic establishes early on that Prime wields the Creation Matrix, which gives life to machines. In the second season, the two-part episode The Key to Vector Sigma introduced the ancient Vector Sigma computer, which served the same original purpose as the Creation Matrix (giving life to Transformers), and its guardian Alpha Trion. """</span> inputs = tokenizer(text, return_tensors=<span class="hljs-string">"pt"</span>) logits = model(**inputs).logits</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>IndexError: index out of <span class="hljs-built_in">range</span> <span class="hljs-keyword">in</span> self</pre></div> <p>Uh-oh, we’ve hit a problem — and the error message is far more cryptic than the ones we saw in <a href="/course/chapter8/section2">section 2</a>! We can’t make head or tails of the full traceback, so we decide to turn to the Hugging Face forums for help. How might we craft the topic?</p> <p>To get started, we need to click the “New Topic” button at the upper-right corner (note that to create a topic, we’ll need to be logged in):</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forums-new-topic.png" alt="Creating a new forum topic." width="100%"></div> <p>This brings up a writing interface where we can input the title of our topic, select a category, and draft the content:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forum-topic01.png" alt="The interface for creating a forum topic." width="100%"></div> <p>Since the error seems to be exclusively about 🤗 Transformers, we’ll select this for the category. Our first attempt at explaining the problem might look something like this:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forum-topic02.png" alt="Drafting the content for a new forum topic." width="100%"></div> <p>Although this topic contains the error message we need help with, there are a few problems with the way it is written:</p> <ol><li>The title is not very descriptive, so anyone browsing the forum won’t be able to tell what the topic is about without reading the body as well.</li> <li>The body doesn’t provide enough information about <em>where</em> the error is coming from and <em>how</em> to reproduce it.</li> <li>The topic tags a few people directly with a somewhat demanding tone.</li></ol> <p>Topics like this one are not likely to get a fast answer (if they get one at all), so let’s look at how we can improve it. We’ll start with the first issue of picking a good title.</p> <h3 class="relative group"><a id="choosing-a-descriptive-title" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#choosing-a-descriptive-title"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Choosing a descriptive title</span></h3> <p>If you’re trying to get help with a bug in your code, a good rule of thumb is to include enough information in the title so that others can quickly determine whether they think they can answer your question or not. In our running example, we know the name of the exception that’s being raised and have some hints that it’s triggered in the forward pass of the model, where we call <code>model(**inputs)</code>. To communicate this, one possible title could be:</p> <blockquote><p>Source of IndexError in the AutoModel forward pass?</p></blockquote> <p>This title tells the reader <em>where</em> you think the bug is coming from, and if they’ve encountered an <code>IndexError</code> before, there’s a good chance they’ll know how to debug it. Of course, the title can be anything you want, and other variations like:</p> <blockquote><p>Why does my model produce an IndexError?</p></blockquote> <p>could also be fine. Now that we’ve got a descriptive title, let’s take a look at improving the body.</p> <h3 class="relative group"><a id="formatting-your-code-snippets" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#formatting-your-code-snippets"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Formatting your code snippets</span></h3> <p>Reading source code is hard enough in an IDE, but it’s even harder when the code is copied and pasted as plain text! Fortunately, the Hugging Face forums support the use of Markdown, so you should always enclose your code blocks with three backticks (```) so it’s more easily readable. Let’s do this to prettify the error message — and while we’re at it, let’s make the body a bit more polite than our original version:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forum-topic03.png" alt="Our revised forum topic, with proper code formatting." width="100%"></div> <p>As you can see in the screenshot, enclosing the code blocks in backticks converts the raw text into formatted code, complete with color styling! Also note that single backticks can be used to format inline variables, like we’ve done for <code>distilbert-base-uncased</code>. This topic is looking much better, and with a bit of luck we might find someone in the community who can guess what the error is about. However, instead of relying on luck, let’s make life easier by including the traceback in its full gory detail!</p> <h3 class="relative group"><a id="including-the-full-traceback" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#including-the-full-traceback"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Including the full traceback</span></h3> <p>Since the last line of the traceback is often enough to debug your own code, it can be tempting to just provide that in your topic to “save space.” Although well intentioned, this actually makes it <em>harder</em> for others to debug the problem since the information that’s higher up in the traceback can be really useful too. So, a good practice is to copy and paste the <em>whole</em> traceback, while making sure that it’s nicely formatted. Since these tracebacks can get rather long, some people prefer to show them after they’ve explained the source code. Let’s do this. Now, our forum topic looks like the following:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forum-topic04.png" alt="Our example forum topic, with the complete traceback." width="100%"></div> <p>This is much more informative, and a careful reader might be able to point out that the problem seems to be due to passing a long input because of this line in the traceback:</p> <blockquote><p>Token indices sequence length is longer than the specified maximum sequence length for this model (583 &gt; 512).</p></blockquote> <p>However, we can make things even easier for them by providing the actual code that triggered the error. Let’s do that now.</p> <h3 class="relative group"><a id="providing-a-reproducible-example" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#providing-a-reproducible-example"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Providing a reproducible example</span></h3> <p>If you’ve ever tried to debug someone else’s code, you’ve probably first tried to recreate the problem they’ve reported so you can start working your way through the traceback to pinpoint the error. It’s no different when it comes to getting (or giving) assistance on the forums, so it really helps if you can provide a small example that reproduces the error. Half the time, simply walking through this exercise will help you figure out what’s going wrong. In any case, the missing piece of our example is to show the <em>inputs</em> that we provided to the model. Doing that gives us something like the following completed example:</p> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter8/forum-topic05.png" alt="The final version of our forum topic." width="100%"></div> <p>This topic now contains quite a lot of information, and it’s written in a way that is much more likely to attract the attention of the community and get a helpful answer. With these basic guidelines, you can now create great topics to find the answers to your 🤗 Transformers questions!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter8/2?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>What to do when you get an error</a> <a href="/learn/nlp-course/chapter8/4?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Debugging the training pipeline<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;asking-for-help-on-the-forums&quot;,&quot;url&quot;:&quot;#asking-for-help-on-the-forums&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Writing a good forum post&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;writing-a-good-forum-post&quot;,&quot;url&quot;:&quot;#writing-a-good-forum-post&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Choosing a descriptive title&quot;,&quot;id&quot;:&quot;choosing-a-descriptive-title&quot;,&quot;url&quot;:&quot;#choosing-a-descriptive-title&quot;},{&quot;title&quot;:&quot;Formatting your code snippets&quot;,&quot;id&quot;:&quot;formatting-your-code-snippets&quot;,&quot;url&quot;:&quot;#formatting-your-code-snippets&quot;},{&quot;title&quot;:&quot;Including the full traceback&quot;,&quot;id&quot;:&quot;including-the-full-traceback&quot;,&quot;url&quot;:&quot;#including-the-full-traceback&quot;},{&quot;title&quot;:&quot;Providing a reproducible example&quot;,&quot;id&quot;:&quot;providing-a-reproducible-example&quot;,&quot;url&quot;:&quot;#providing-a-reproducible-example&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#asking-for-help-on-the-forums" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-asking-for-help-on-the-forums"><wbr>Asking for help on the forums</a> <a href="#writing-a-good-forum-post" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-writing-a-good-forum-post"><wbr>Writing a good forum post</a> <a href="#choosing-a-descriptive-title" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-choosing-a-descriptive-title"><wbr>Choosing a descriptive title</a> <a href="#formatting-your-code-snippets" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-formatting-your-code-snippets"><wbr>Formatting your code snippets</a> <a href="#including-the-full-traceback" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-including-the-full-traceback"><wbr>Including the full traceback</a> <a href="#providing-a-reproducible-example" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-providing-a-reproducible-example"><wbr>Providing a reproducible example</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter8/3" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter8/3"); } </script> <iframe name="__privateStripeMetricsController9690" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter8%2F3%3Ffw%3Dpt&amp;title=Asking%20for%20help%20on%20the%20forums%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:33.589Z
Debugging the training pipeline - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter8/4?fw=pt
[Pytorch](?fw=pt) [TensorFlow](?fw=tf) ## [](#debugging-the-training-pipeline)Debugging the training pipeline [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-8-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter8/section4.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter8/section4.ipynb) You’ve written a beautiful script to train or fine-tune a model on a given task, dutifully following the advice from [Chapter 7](/course/chapter7). But when you launch the command `trainer.train()`, something horrible happens: you get an error 😱! Or worse, everything seems to be fine and the training runs without error, but the resulting model is crappy. In this section, we will show you what you can do to debug these kinds of issues. ## [](#debugging-the-training-pipeline)Debugging the training pipeline The problem when you encounter an error in `trainer.train()` is that it could come from multiple sources, as the `Trainer` usually puts together lots of things. It converts datasets to dataloaders, so the problem could be something wrong in your dataset, or some issue when trying to batch elements of the datasets together. Then it takes a batch of data and feeds it to the model, so the problem could be in the model code. After that, it computes the gradients and performs the optimization step, so the problem could also be in your optimizer. And even if everything goes well for training, something could still go wrong during the evaluation if there is a problem with your metric. The best way to debug an error that arises in `trainer.train()` is to manually go through this whole pipeline to see where things went awry. The error is then often very easy to solve. To demonstrate this, we will use the following script that (tries to) fine-tune a DistilBERT model on the [MNLI dataset](https://huggingface.co/datasets/glue): ``` from datasets import load_dataset import evaluate from transformers import ( AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer, ) raw_datasets = load_dataset("glue", "mnli") model_checkpoint = "distilbert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) def preprocess_function(examples): return tokenizer(examples["premise"], examples["hypothesis"], truncation=True) tokenized_datasets = raw_datasets.map(preprocess_function, batched=True) model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint) args = TrainingArguments( f"distilbert-finetuned-mnli", evaluation_strategy="epoch", save_strategy="epoch", learning_rate=2e-5, num_train_epochs=3, weight_decay=0.01, ) metric = evaluate.load("glue", "mnli") def compute_metrics(eval_pred): predictions, labels = eval_pred return metric.compute(predictions=predictions, references=labels) trainer = Trainer( model, args, train_dataset=raw_datasets["train"], eval_dataset=raw_datasets["validation_matched"], compute_metrics=compute_metrics, ) trainer.train()``` If you try to execute it, you will be met with a rather cryptic error: ``` 'ValueError: You have to specify either input_ids or inputs_embeds'``` ### [](#check-your-data)Check your data This goes without saying, but if your data is corrupted, the `Trainer` is not going to be able to form batches, let alone train your model. So first things first, you need to have a look at what is inside your training set. To avoid countless hours spent trying to fix something that is not the source of the bug, we recommend you use `trainer.train_dataset` for your checks and nothing else. So let’s do that here: ``` {'hypothesis': 'Product and geography are what make cream skimming work. ', 'idx': 0, 'label': 1, 'premise': 'Conceptually cream skimming has two basic dimensions - product and geography.'}``` Do you notice something wrong? This, in conjunction with the error message about `input_ids` missing, should make you realize those are texts, not numbers the model can make sense of. Here, the original error is very misleading because the `Trainer` automatically removes the columns that don’t match the model signature (that is, the arguments expected by the model). That means here, everything apart from the labels was discarded. There was thus no issue with creating batches and then sending them to the model, which in turn complained it didn’t receive the proper input. Why wasn’t the data processed? We did use the `Dataset.map()` method on the datasets to apply the tokenizer on each sample. But if you look closely at the code, you will see that we made a mistake when passing the training and evaluation sets to the `Trainer`. Instead of using `tokenized_datasets` here, we used `raw_datasets` 🤦. So let’s fix this! ``` from datasets import load_dataset import evaluate from transformers import ( AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer, ) raw_datasets = load_dataset("glue", "mnli") model_checkpoint = "distilbert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) def preprocess_function(examples): return tokenizer(examples["premise"], examples["hypothesis"], truncation=True) tokenized_datasets = raw_datasets.map(preprocess_function, batched=True) model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint) args = TrainingArguments( f"distilbert-finetuned-mnli", evaluation_strategy="epoch", save_strategy="epoch", learning_rate=2e-5, num_train_epochs=3, weight_decay=0.01, ) metric = evaluate.load("glue", "mnli") def compute_metrics(eval_pred): predictions, labels = eval_pred return metric.compute(predictions=predictions, references=labels) trainer = Trainer( model, args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation_matched"], compute_metrics=compute_metrics, ) trainer.train()``` This new code will now give a different error (progress!): ``` 'ValueError: expected sequence of length 43 at dim 1 (got 37)'``` Looking at the traceback, we can see the error happens in the data collation step: ``` ~/git/transformers/src/transformers/data/data_collator.py in torch_default_data_collator(features) 105 batch[k] = torch.stack([f[k] for f in features]) 106 else: --> 107 batch[k] = torch.tensor([f[k] for f in features]) 108 109 return batch``` So, we should move to that. Before we do, however, let’s finish inspecting our data, just to be 100% sure it’s correct. One thing you should always do when debugging a training session is have a look at the decoded inputs of your model. We can’t make sense of the numbers that we feed it directly, so we should look at what those numbers represent. In computer vision, for example, that means looking at the decoded pictures of the pixels you pass, in speech it means listening to the decoded audio samples, and for our NLP example here it means using our tokenizer to decode the inputs: ``` tokenizer.decode(trainer.train_dataset[0]["input_ids"])``` ``` '[CLS] conceptually cream skimming has two basic dimensions - product and geography. [SEP] product and geography are what make cream skimming work. [SEP]'``` So that seems correct. You should do this for all the keys in the inputs: ``` trainer.train_dataset[0].keys()``` ``` dict_keys(['attention_mask', 'hypothesis', 'idx', 'input_ids', 'label', 'premise'])``` Note that the keys that don’t correspond to inputs accepted by the model will be automatically discarded, so here we will only keep `input_ids`, `attention_mask`, and `label` (which will be renamed `labels`). To double-check the model signature, you can print the class of your model, then go check its documentation: ``` transformers.models.distilbert.modeling_distilbert.DistilBertForSequenceClassification``` So in our case, we can check the parameters accepted on [this page](https://huggingface.co/transformers/model_doc/distilbert.html#distilbertforsequenceclassification). The `Trainer` will also log the columns it’s discarding. We have checked that the input IDs are correct by decoding them. Next is the `attention_mask`: ``` trainer.train_dataset[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, 1, 1, 1, 1, 1, 1]``` Since we didn’t apply padding in our preprocessing, this seems perfectly natural. To be sure there is no issue with that attention mask, let’s check it is the same length as our input IDs: ``` len(trainer.train_dataset[0]["attention_mask"]) == len( trainer.train_dataset[0]["input_ids"] )``` That’s good! Lastly, let’s check our label: ``` trainer.train_dataset[0]["label"]``` Like the input IDs, this is a number that doesn’t really make sense on its own. As we saw before, the map between integers and label names is stored inside the `names` attribute of the corresponding _feature_ of the dataset: ``` trainer.train_dataset.features["label"].names``` ``` ['entailment', 'neutral', 'contradiction']``` So `1` means `neutral`, which means the two sentences we saw above are not in contradiction, and the first one does not imply the second one. That seems correct! We don’t have token type IDs here, since DistilBERT does not expect them; if you have some in your model, you should also make sure that they properly match where the first and second sentences are in the input. ✏️ **Your turn!** Check that everything seems correct with the second element of the training dataset. We are only doing the check on the training set here, but you should of course double-check the validation and test sets the same way. Now that we know our datasets look good, it’s time to check the next step of the training pipeline. ### [](#from-datasets-to-dataloaders)From datasets to dataloaders The next thing that can go wrong in the training pipeline is when the `Trainer` tries to form batches from the training or validation set. Once you are sure the `Trainer`’s datasets are correct, you can try to manually form a batch by executing the following (replace `train` with `eval` for the validation dataloader): ``` for batch in trainer.get_train_dataloader(): break``` This code creates the training dataloader, then iterates through it, stopping at the first iteration. If the code executes without error, you have the first training batch that you can inspect, and if the code errors out, you know for sure the problem is in the dataloader, as is the case here: ``` ~/git/transformers/src/transformers/data/data_collator.py in torch_default_data_collator(features) 105 batch[k] = torch.stack([f[k] for f in features]) 106 else: --> 107 batch[k] = torch.tensor([f[k] for f in features]) 108 109 return batch ValueError: expected sequence of length 45 at dim 1 (got 76)``` Inspecting the last frame of the traceback should be enough to give you a clue, but let’s do a bit more digging. Most of the problems during batch creation arise because of the collation of examples into a single batch, so the first thing to check when in doubt is what `collate_fn` your `DataLoader` is using: ``` data_collator = trainer.get_train_dataloader().collate_fn data_collator``` ``` <function transformers.data.data_collator.default_data_collator(features: List[InputDataClass], return_tensors='pt') -> Dict[str, Any]>``` So this is the `default_data_collator`, but that’s not what we want in this case. We want to pad our examples to the longest sentence in the batch, which is done by the `DataCollatorWithPadding` collator. And this data collator is supposed to be used by default by the `Trainer`, so why is it not used here? The answer is because we did not pass the `tokenizer` to the `Trainer`, so it couldn’t create the `DataCollatorWithPadding` we want. In practice, you should never hesitate to explicitly pass along the data collator you want to use, to make sure you avoid these kinds of errors. Let’s adapt our code to do exactly that: ``` from datasets import load_dataset import evaluate from transformers import ( AutoTokenizer, AutoModelForSequenceClassification, DataCollatorWithPadding, TrainingArguments, Trainer, ) raw_datasets = load_dataset("glue", "mnli") model_checkpoint = "distilbert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) def preprocess_function(examples): return tokenizer(examples["premise"], examples["hypothesis"], truncation=True) tokenized_datasets = raw_datasets.map(preprocess_function, batched=True) model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint) args = TrainingArguments( f"distilbert-finetuned-mnli", evaluation_strategy="epoch", save_strategy="epoch", learning_rate=2e-5, num_train_epochs=3, weight_decay=0.01, ) metric = evaluate.load("glue", "mnli") def compute_metrics(eval_pred): predictions, labels = eval_pred return metric.compute(predictions=predictions, references=labels) data_collator = DataCollatorWithPadding(tokenizer=tokenizer) trainer = Trainer( model, args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation_matched"], compute_metrics=compute_metrics, data_collator=data_collator, tokenizer=tokenizer, ) trainer.train()``` The good news? We don’t get the same error as before, which is definitely progress. The bad news? We get an infamous CUDA error instead: ``` RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)```` This is bad because CUDA errors are extremely hard to debug in general. We will see in a minute how to solve this, but first let’s finish our analysis of batch creation. If you are sure your data collator is the right one, you should try to apply it on a couple of samples of your dataset: ``` data_collator = trainer.get_train_dataloader().collate_fn batch = data_collator([trainer.train_dataset[i] for i in range(4)])``` This code will fail because the `train_dataset` contains string columns, which the `Trainer` usually removes. You can remove them manually, or if you want to replicate exactly what the `Trainer` is doing behind the scenes, you can call the private `Trainer._remove_unused_columns()` method that does that: ``` data_collator = trainer.get_train_dataloader().collate_fn actual_train_set = trainer._remove_unused_columns(trainer.train_dataset) batch = data_collator([actual_train_set[i] for i in range(4)])``` You should then be able to manually debug what happens inside the data collator if the error persists. Now that we’ve debugged the batch creation process, it’s time to pass one through the model! ### [](#going-through-the-model)Going through the model You should be able to get a batch by executing the following command: ``` for batch in trainer.get_train_dataloader(): break``` If you’re running this code in a notebook, you may get a CUDA error that’s similar to the one we saw earlier, in which case you need to restart your notebook and reexecute the last snippet without the `trainer.train()` line. That’s the second most annoying thing about CUDA errors: they irremediably break your kernel. The most annoying thing about them is the fact that they are hard to debug. Why is that? It has to do with the way GPUs work. They are extremely efficient at executing a lot of operations in parallel, but the drawback is that when one of those instructions results in an error, you don’t know it instantly. It’s only when the program calls a synchronization of the multiple processes on the GPU that it will realize something went wrong, so the error is actually raised at a place that has nothing to do with what created it. For instance, if we look at our previous traceback, the error was raised during the backward pass, but we will see in a minute that it actually stems from something in the forward pass. So how do we debug those errors? The answer is easy: we don’t. Unless your CUDA error is an out-of-memory error (which means there is not enough memory in your GPU), you should always go back to the CPU to debug it. To do this in our case, we just have to put the model back on the CPU and call it on our batch — the batch returned by the `DataLoader` has not been moved to the GPU yet: ``` outputs = trainer.model.cpu()(**batch)``` ``` ~/.pyenv/versions/3.7.9/envs/base/lib/python3.7/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction) 2386 ) 2387 if dim == 2: -> 2388 ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index) 2389 elif dim == 4: 2390 ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index) IndexError: Target 2 is out of bounds.``` So, the picture is getting clearer. Instead of having a CUDA error, we now have an `IndexError` in the loss computation (so nothing to do with the backward pass, as we said earlier). More precisely, we can see that it’s target 2 that creates the error, so this is a very good moment to check the number of labels of our model: ``` trainer.model.config.num_labels``` With two labels, only 0s and 1s are allowed as targets, but according to the error message we got a 2. Getting a 2 is actually normal: if we remember the label names we extracted earlier, there were three, so we have indices 0, 1, and 2 in our dataset. The problem is that we didn’t tell that to our model, which should have been created with three labels. So let’s fix that! ``` from datasets import load_dataset import evaluate from transformers import ( AutoTokenizer, AutoModelForSequenceClassification, DataCollatorWithPadding, TrainingArguments, Trainer, ) raw_datasets = load_dataset("glue", "mnli") model_checkpoint = "distilbert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) def preprocess_function(examples): return tokenizer(examples["premise"], examples["hypothesis"], truncation=True) tokenized_datasets = raw_datasets.map(preprocess_function, batched=True) model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint, num_labels=3) args = TrainingArguments( f"distilbert-finetuned-mnli", evaluation_strategy="epoch", save_strategy="epoch", learning_rate=2e-5, num_train_epochs=3, weight_decay=0.01, ) metric = evaluate.load("glue", "mnli") def compute_metrics(eval_pred): predictions, labels = eval_pred return metric.compute(predictions=predictions, references=labels) data_collator = DataCollatorWithPadding(tokenizer=tokenizer) trainer = Trainer( model, args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation_matched"], compute_metrics=compute_metrics, data_collator=data_collator, tokenizer=tokenizer, )``` We aren’t including the `trainer.train()` line yet, to take the time to check that everything looks good. If we request a batch and pass it to our model, it now works without error! ``` for batch in trainer.get_train_dataloader(): break outputs = trainer.model.cpu()(**batch)``` The next step is then to move back to the GPU and check that everything still works: ``` import torch device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") batch = {k: v.to(device) for k, v in batch.items()} outputs = trainer.model.to(device)(**batch)``` If you still get an error, make sure you restart your notebook and only execute the last version of the script. ### [](#performing-one-optimization-step)Performing one optimization step Now that we know that we can build batches that actually go through the model, we are ready for the next step of the training pipeline: computing the gradients and performing an optimization step. The first part is just a matter of calling the `backward()` method on the loss: ``` loss = outputs.loss loss.backward()``` It’s pretty rare to get an error at this stage, but if you do get one, make sure to go back to the CPU to get a helpful error message. To perform the optimization step, we just need to create the `optimizer` and call its `step()` method: ``` trainer.create_optimizer() trainer.optimizer.step()``` Again, if you’re using the default optimizer in the `Trainer`, you shouldn’t get an error at this stage, but if you have a custom optimizer, there might be some problems to debug here. Don’t forget to go back to the CPU if you get a weird CUDA error at this stage. Speaking of CUDA errors, earlier we mentioned a special case. Let’s have a look at that now. ### [](#dealing-with-cuda-out-of-memory-errors)Dealing with CUDA out-of-memory errors Whenever you get an error message that starts with `RuntimeError: CUDA out of memory`, this indicates that you are out of GPU memory. This is not directly linked to your code, and it can happen with a script that runs perfectly fine. This error means that you tried to put too many things in the internal memory of your GPU, and that resulted in an error. Like with other CUDA errors, you will need to restart your kernel to be in a spot where you can run your training again. To solve this issue, you just need to use less GPU space — something that is often easier said than done. First, make sure you don’t have two models on the GPU at the same time (unless that’s required for your problem, of course). Then, you should probably reduce your batch size, as it directly affects the sizes of all the intermediate outputs of the model and their gradients. If the problem persists, consider using a smaller version of your model. In the next part of the course, we’ll look at more advanced techniques that can help you reduce your memory footprint and let you fine-tune the biggest models. ### [](#evaluating-the-model)Evaluating the model Now that we’ve solved all the issues with our code, everything is perfect and the training should run smoothly, right? Not so fast! If you run the `trainer.train()` command, everything will look good at first, but after a while you will get the following: ``` TypeError: only size-1 arrays can be converted to Python scalars``` You will realize this error appears during the evaluation phase, so this is the last thing we will need to debug. You can run the evaluation loop of the `Trainer` independently form the training like this: ``` TypeError: only size-1 arrays can be converted to Python scalars``` 💡 You should always make sure you can run `trainer.evaluate()` before launching `trainer.train()`, to avoid wasting lots of compute resources before hitting an error. Before attempting to debug a problem in the evaluation loop, you should first make sure that you’ve had a look at the data, are able to form a batch properly, and can run your model on it. We’ve completed all of those steps, so the following code can be executed without error: ``` for batch in trainer.get_eval_dataloader(): break batch = {k: v.to(device) for k, v in batch.items()} with torch.no_grad(): outputs = trainer.model(**batch)``` The error comes later, at the end of the evaluation phase, and if we look at the traceback we see this: ``` ~/git/datasets/src/datasets/metric.py in add_batch(self, predictions, references) 431 """ 432 batch = {"predictions": predictions, "references": references} --> 433 batch = self.info.features.encode_batch(batch) 434 if self.writer is None: 435 self._init_writer()``` This tells us that the error originates in the `datasets/metric.py` module — so this is a problem with our `compute_metrics()` function. It takes a tuple with the logits and the labels as NumPy arrays, so let’s try to feed it that: ``` predictions = outputs.logits.cpu().numpy() labels = batch["labels"].cpu().numpy() compute_metrics((predictions, labels))``` ``` TypeError: only size-1 arrays can be converted to Python scalars``` We get the same error, so the problem definitely lies with that function. If we look back at its code, we see it’s just forwarding the `predictions` and the `labels` to `metric.compute()`. So is there a problem with that method? Not really. Let’s have a quick look at the shapes: ``` predictions.shape, labels.shape``` Our predictions are still logits, not the actual predictions, which is why the metric is returning this (somewhat obscure) error. The fix is pretty easy; we just have to add an argmax in the `compute_metrics()` function: ``` import numpy as np def compute_metrics(eval_pred): predictions, labels = eval_pred predictions = np.argmax(predictions, axis=1) return metric.compute(predictions=predictions, references=labels) compute_metrics((predictions, labels))``` Now our error is fixed! This was the last one, so our script will now train a model properly. For reference, here is the completely fixed script: ``` import numpy as np from datasets import load_dataset import evaluate from transformers import ( AutoTokenizer, AutoModelForSequenceClassification, DataCollatorWithPadding, TrainingArguments, Trainer, ) raw_datasets = load_dataset("glue", "mnli") model_checkpoint = "distilbert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) def preprocess_function(examples): return tokenizer(examples["premise"], examples["hypothesis"], truncation=True) tokenized_datasets = raw_datasets.map(preprocess_function, batched=True) model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint, num_labels=3) args = TrainingArguments( f"distilbert-finetuned-mnli", evaluation_strategy="epoch", save_strategy="epoch", learning_rate=2e-5, num_train_epochs=3, weight_decay=0.01, ) metric = evaluate.load("glue", "mnli") def compute_metrics(eval_pred): predictions, labels = eval_pred predictions = np.argmax(predictions, axis=1) return metric.compute(predictions=predictions, references=labels) data_collator = DataCollatorWithPadding(tokenizer=tokenizer) trainer = Trainer( model, args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation_matched"], compute_metrics=compute_metrics, data_collator=data_collator, tokenizer=tokenizer, ) trainer.train()``` In this instance, there are no more problems, and our script will fine-tune a model that should give reasonable results. But what can we do when the training proceeds without any error, and the model trained does not perform well at all? That’s the hardest part of machine learning, and we’ll show you a few techniques that can help. 💡 If you’re using a manual training loop, the same steps apply to debug your training pipeline, but it’s easier to separate them. Make sure you have not forgotten the `model.eval()` or `model.train()` at the right places, or the `zero_grad()` at each step, however! ## [](#debugging-silent-errors-during-training)Debugging silent errors during training What can we do to debug a training that completes without error but doesn’t get good results? We’ll give you some pointers here, but be aware that this kind of debugging is the hardest part of machine learning, and there is no magical answer. ### [](#check-your-data-again)Check your data (again!) Your model will only learn something if it’s actually possible to learn anything from your data. If there is a bug that corrupts the data or the labels are attributed randomly, it’s very likely you won’t get any model training on your dataset. So always start by double-checking your decoded inputs and labels, and ask yourself the following questions: - Is the decoded data understandable? - Do you agree with the labels? - Is there one label that’s more common than the others? - What should the loss/metric be if the model predicted a random answer/always the same answer? ⚠️ If you are doing distributed training, print samples of your dataset in each process and triple-check that you get the same thing. One common bug is to have some source of randomness in the data creation that makes each process have a different version of the dataset. After looking at your data, go through a few of the model’s predictions and decode them too. If the model is always predicting the same thing, it might be because your dataset is biased toward one category (for classification problems); techniques like oversampling rare classes might help. If the loss/metric you get on your initial model is very different from the loss/metric you would expect for random predictions, double-check the way your loss or metric is computed, as there is probably a bug there. If you are using several losses that you add at the end, make sure they are of the same scale. When you are sure your data is perfect, you can see if the model is capable of training on it with one simple test. ### [](#overfit-your-model-on-one-batch)Overfit your model on one batch Overfitting is usually something we try to avoid when training, as it means the model is not learning to recognize the general features we want it to but is instead just memorizing the training samples. However, trying to train your model on one batch over and over again is a good test to check if the problem as you framed it can be solved by the model you are attempting to train. It will also help you see if your initial learning rate is too high. Doing this once you have defined your `Trainer` is really easy; just grab a batch of training data, then run a small manual training loop only using that batch for something like 20 steps: ``` for batch in trainer.get_train_dataloader(): break batch = {k: v.to(device) for k, v in batch.items()} trainer.create_optimizer() for _ in range(20): outputs = trainer.model(**batch) loss = outputs.loss loss.backward() trainer.optimizer.step() trainer.optimizer.zero_grad()``` 💡 If your training data is unbalanced, make sure to build a batch of training data containing all the labels. The resulting model should have close-to-perfect results on the same `batch`. Let’s compute the metric on the resulting predictions: ``` with torch.no_grad(): outputs = trainer.model(**batch) preds = outputs.logits labels = batch["labels"] compute_metrics((preds.cpu().numpy(), labels.cpu().numpy()))``` 100% accuracy, now this is a nice example of overfitting (meaning that if you try your model on any other sentence, it will very likely give you a wrong answer)! If you don’t manage to have your model obtain perfect results like this, it means there is something wrong with the way you framed the problem or your data, so you should fix that. Only when you manage to pass the overfitting test can you be sure that your model can actually learn something. ⚠️ You will have to recreate your model and your `Trainer` after this test, as the model obtained probably won’t be able to recover and learn something useful on your full dataset. ### [](#dont-tune-anything-until-you-have-a-first-baseline)Don't tune anything until you have a first baseline Hyperparameter tuning is always emphasized as being the hardest part of machine learning, but it’s just the last step to help you gain a little bit on the metric. Most of the time, the default hyperparameters of the `Trainer` will work just fine to give you good results, so don’t launch into a time-consuming and costly hyperparameter search until you have something that beats the baseline you have on your dataset. Once you have a good enough model, you can start tweaking a bit. Don’t try launching a thousand runs with different hyperparameters, but compare a couple of runs with different values for one hyperparameter to get an idea of which has the greatest impact. If you are tweaking the model itself, keep it simple and don’t try anything you can’t reasonably justify. Always make sure you go back to the overfitting test to verify that your change hasn’t had any unintended consequences. ### [](#ask-for-help)Ask for help Hopefully you will have found some advice in this section that helped you solve your issue, but if that’s not the case, remember you can always ask the community on the [forums](https://discuss.huggingface.co/). Here are some additional resources that may prove helpful: - [“Reproducibility as a vehicle for engineering best practices”](https://docs.google.com/presentation/d/1yHLPvPhUs2KGI5ZWo0sU-PKU3GimAk3iTsI38Z-B5Gw/edit#slide=id.p) by Joel Grus - [“Checklist for debugging neural networks”](https://towardsdatascience.com/checklist-for-debugging-neural-networks-d8b2a9434f21) by Cecelia Shao - [“How to unit test machine learning code”](https://medium.com/@keeper6928/how-to-unit-test-machine-learning-code-57cf6fd81765) by Chase Roberts - [“A Recipe for Training Neural Networks”](http://karpathy.github.io/2019/04/25/recipe/) by Andrej Karpathy Of course, not every problem you encounter when training neural nets is your own fault! If you encounter something in the 🤗 Transformers or 🤗 Datasets library that does not seem right, you may have encountered a bug. You should definitely tell us all about it, and in the next section we’ll explain exactly how to do that.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Debugging the training pipeline - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter8/4"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Debugging the training pipeline - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;debugging-the-training-pipeline&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;debugging-the-training-pipeline&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;check-your-data&quot;,&quot;title&quot;:&quot;Check your data&quot;},{&quot;local&quot;:&quot;from-datasets-to-dataloaders&quot;,&quot;title&quot;:&quot;From datasets to dataloaders&quot;},{&quot;local&quot;:&quot;going-through-the-model&quot;,&quot;title&quot;:&quot;Going through the model&quot;},{&quot;local&quot;:&quot;performing-one-optimization-step&quot;,&quot;title&quot;:&quot;Performing one optimization step&quot;},{&quot;local&quot;:&quot;dealing-with-cuda-out-of-memory-errors&quot;,&quot;title&quot;:&quot;Dealing with CUDA out-of-memory errors&quot;},{&quot;local&quot;:&quot;evaluating-the-model&quot;,&quot;title&quot;:&quot;Evaluating the model&quot;}],&quot;title&quot;:&quot;Debugging the training pipeline&quot;},{&quot;local&quot;:&quot;debugging-silent-errors-during-training&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;check-your-data-again&quot;,&quot;title&quot;:&quot;Check your data (again!)&quot;},{&quot;local&quot;:&quot;overfit-your-model-on-one-batch&quot;,&quot;title&quot;:&quot;Overfit your model on one batch&quot;},{&quot;local&quot;:&quot;dont-tune-anything-until-you-have-a-first-baseline&quot;,&quot;title&quot;:&quot;Don't tune anything until you have a first baseline&quot;},{&quot;local&quot;:&quot;ask-for-help&quot;,&quot;title&quot;:&quot;Ask for help&quot;}],&quot;title&quot;:&quot;Debugging silent errors during training&quot;}],&quot;title&quot;:&quot;Debugging the training pipeline&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter8/4&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Debugging the training pipeline&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Debugging the training pipeline</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/2?fw=pt">What to do when you get an error </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/3?fw=pt">Asking for help on the forums </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter8/4?fw=pt">Debugging the training pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/5?fw=pt">How to write a good issue </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/6?fw=pt">Part 2 completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/7?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <div class="bg-white leading-none border border-gray-100 rounded-lg flex p-0.5 w-56 text-sm mb-4"><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-l bg-red-50 dark:bg-transparent text-red-600" href="?fw=pt"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> Pytorch </a><a class="flex justify-center flex-1 py-1.5 px-2.5 focus:outline-none !no-underline rounded-r text-gray-500 filter grayscale" href="?fw=tf"><svg class="mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> TensorFlow </a></div> <h1 class="relative group"><a id="debugging-the-training-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#debugging-the-training-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Debugging the training pipeline</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-8-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter8/section4.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter8/section4.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>You’ve written a beautiful script to train or fine-tune a model on a given task, dutifully following the advice from <a href="/course/chapter7">Chapter 7</a>. But when you launch the command <code>trainer.train()</code>, something horrible happens: you get an error 😱! Or worse, everything seems to be fine and the training runs without error, but the resulting model is crappy. In this section, we will show you what you can do to debug these kinds of issues.</p> <h2 class="relative group"><a id="debugging-the-training-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#debugging-the-training-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Debugging the training pipeline</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/L-WSwUWde1U" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The problem when you encounter an error in <code>trainer.train()</code> is that it could come from multiple sources, as the <code>Trainer</code> usually puts together lots of things. It converts datasets to dataloaders, so the problem could be something wrong in your dataset, or some issue when trying to batch elements of the datasets together. Then it takes a batch of data and feeds it to the model, so the problem could be in the model code. After that, it computes the gradients and performs the optimization step, so the problem could also be in your optimizer. And even if everything goes well for training, something could still go wrong during the evaluation if there is a problem with your metric.</p> <p>The best way to debug an error that arises in <code>trainer.train()</code> is to manually go through this whole pipeline to see where things went awry. The error is then often very easy to solve.</p> <p>To demonstrate this, we will use the following script that (tries to) fine-tune a DistilBERT model on the <a href="https://huggingface.co/datasets/glue" rel="nofollow">MNLI dataset</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-keyword">import</span> evaluate <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> ( AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer, ) raw_datasets = load_dataset(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mnli"</span>) model_checkpoint = <span class="hljs-string">"distilbert-base-uncased"</span> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) <span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_function</span>(<span class="hljs-params">examples</span>): <span class="hljs-keyword">return</span> tokenizer(examples[<span class="hljs-string">"premise"</span>], examples[<span class="hljs-string">"hypothesis"</span>], truncation=<span class="hljs-literal">True</span>) tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>(preprocess_function, batched=<span class="hljs-literal">True</span>) model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint) args = TrainingArguments( <span class="hljs-string">f"distilbert-finetuned-mnli"</span>, evaluation_strategy=<span class="hljs-string">"epoch"</span>, save_strategy=<span class="hljs-string">"epoch"</span>, learning_rate=<span class="hljs-number">2e-5</span>, num_train_epochs=<span class="hljs-number">3</span>, weight_decay=<span class="hljs-number">0.01</span>, ) metric = evaluate.load(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mnli"</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_pred</span>): predictions, labels = eval_pred <span class="hljs-keyword">return</span> metric.compute(predictions=predictions, references=labels) trainer = Trainer( model, args, train_dataset=raw_datasets[<span class="hljs-string">"train"</span>], eval_dataset=raw_datasets[<span class="hljs-string">"validation_matched"</span>], compute_metrics=compute_metrics, ) trainer.train()</pre></div> <p>If you try to execute it, you will be met with a rather cryptic error:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'ValueError: You have to specify either input_ids or inputs_embeds'</span></pre></div> <h3 class="relative group"><a id="check-your-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#check-your-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Check your data</span></h3> <p>This goes without saying, but if your data is corrupted, the <code>Trainer</code> is not going to be able to form batches, let alone train your model. So first things first, you need to have a look at what is inside your training set.</p> <p>To avoid countless hours spent trying to fix something that is not the source of the bug, we recommend you use <code>trainer.train_dataset</code> for your checks and nothing else. So let’s do that here:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.train_dataset[<span class="hljs-number">0</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'hypothesis'</span>: <span class="hljs-string">'Product and geography are what make cream skimming work. '</span>, <span class="hljs-string">'idx'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'label'</span>: <span class="hljs-number">1</span>, <span class="hljs-string">'premise'</span>: <span class="hljs-string">'Conceptually cream skimming has two basic dimensions - product and geography.'</span>}</pre></div> <p>Do you notice something wrong? This, in conjunction with the error message about <code>input_ids</code> missing, should make you realize those are texts, not numbers the model can make sense of. Here, the original error is very misleading because the <code>Trainer</code> automatically removes the columns that don’t match the model signature (that is, the arguments expected by the model). That means here, everything apart from the labels was discarded. There was thus no issue with creating batches and then sending them to the model, which in turn complained it didn’t receive the proper input.</p> <p>Why wasn’t the data processed? We did use the <code>Dataset.map()</code> method on the datasets to apply the tokenizer on each sample. But if you look closely at the code, you will see that we made a mistake when passing the training and evaluation sets to the <code>Trainer</code>. Instead of using <code>tokenized_datasets</code> here, we used <code>raw_datasets</code> 🤦. So let’s fix this!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-keyword">import</span> evaluate <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> ( AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer, ) raw_datasets = load_dataset(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mnli"</span>) model_checkpoint = <span class="hljs-string">"distilbert-base-uncased"</span> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) <span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_function</span>(<span class="hljs-params">examples</span>): <span class="hljs-keyword">return</span> tokenizer(examples[<span class="hljs-string">"premise"</span>], examples[<span class="hljs-string">"hypothesis"</span>], truncation=<span class="hljs-literal">True</span>) tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>(preprocess_function, batched=<span class="hljs-literal">True</span>) model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint) args = TrainingArguments( <span class="hljs-string">f"distilbert-finetuned-mnli"</span>, evaluation_strategy=<span class="hljs-string">"epoch"</span>, save_strategy=<span class="hljs-string">"epoch"</span>, learning_rate=<span class="hljs-number">2e-5</span>, num_train_epochs=<span class="hljs-number">3</span>, weight_decay=<span class="hljs-number">0.01</span>, ) metric = evaluate.load(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mnli"</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_pred</span>): predictions, labels = eval_pred <span class="hljs-keyword">return</span> metric.compute(predictions=predictions, references=labels) trainer = Trainer( model, args, train_dataset=tokenized_datasets[<span class="hljs-string">"train"</span>], eval_dataset=tokenized_datasets[<span class="hljs-string">"validation_matched"</span>], compute_metrics=compute_metrics, ) trainer.train()</pre></div> <p>This new code will now give a different error (progress!):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'ValueError: expected sequence of length 43 at dim 1 (got 37)'</span></pre></div> <p>Looking at the traceback, we can see the error happens in the data collation step:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>~/git/transformers/src/transformers/data/data_collator.py <span class="hljs-keyword">in</span> torch_default_data_collator(features) <span class="hljs-number">105</span> batch[k] = torch.stack([f[k] <span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> features]) <span class="hljs-number">106</span> <span class="hljs-keyword">else</span>: --&gt; <span class="hljs-number">107</span> batch[k] = torch.tensor([f[k] <span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> features]) <span class="hljs-number">108</span> <span class="hljs-number">109</span> <span class="hljs-keyword">return</span> batch</pre></div> <p>So, we should move to that. Before we do, however, let’s finish inspecting our data, just to be 100% sure it’s correct.</p> <p>One thing you should always do when debugging a training session is have a look at the decoded inputs of your model. We can’t make sense of the numbers that we feed it directly, so we should look at what those numbers represent. In computer vision, for example, that means looking at the decoded pictures of the pixels you pass, in speech it means listening to the decoded audio samples, and for our NLP example here it means using our tokenizer to decode the inputs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>tokenizer.decode(trainer.train_dataset[<span class="hljs-number">0</span>][<span class="hljs-string">"input_ids"</span>])</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-string">'[CLS] conceptually cream skimming has two basic dimensions - product and geography. [SEP] product and geography are what make cream skimming work. [SEP]'</span></pre></div> <p>So that seems correct. You should do this for all the keys in the inputs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.train_dataset[<span class="hljs-number">0</span>].keys()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>dict_keys([<span class="hljs-string">'attention_mask'</span>, <span class="hljs-string">'hypothesis'</span>, <span class="hljs-string">'idx'</span>, <span class="hljs-string">'input_ids'</span>, <span class="hljs-string">'label'</span>, <span class="hljs-string">'premise'</span>])</pre></div> <p>Note that the keys that don’t correspond to inputs accepted by the model will be automatically discarded, so here we will only keep <code>input_ids</code>, <code>attention_mask</code>, and <code>label</code> (which will be renamed <code>labels</code>). To double-check the model signature, you can print the class of your model, then go check its documentation:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">type</span>(trainer.model)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>transformers.models.distilbert.modeling_distilbert.DistilBertForSequenceClassification</pre></div> <p>So in our case, we can check the parameters accepted on <a href="https://huggingface.co/transformers/model_doc/distilbert.html#distilbertforsequenceclassification" rel="nofollow">this page</a>. The <code>Trainer</code> will also log the columns it’s discarding.</p> <p>We have checked that the input IDs are correct by decoding them. Next is the <code>attention_mask</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.train_dataset[<span class="hljs-number">0</span>][<span class="hljs-string">"attention_mask"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]</pre></div> <p>Since we didn’t apply padding in our preprocessing, this seems perfectly natural. To be sure there is no issue with that attention mask, let’s check it is the same length as our input IDs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">len</span>(trainer.train_dataset[<span class="hljs-number">0</span>][<span class="hljs-string">"attention_mask"</span>]) == <span class="hljs-built_in">len</span>( trainer.train_dataset[<span class="hljs-number">0</span>][<span class="hljs-string">"input_ids"</span>] )</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-literal">True</span></pre></div> <p>That’s good! Lastly, let’s check our label:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.train_dataset[<span class="hljs-number">0</span>][<span class="hljs-string">"label"</span>]</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">1</span></pre></div> <p>Like the input IDs, this is a number that doesn’t really make sense on its own. As we saw before, the map between integers and label names is stored inside the <code>names</code> attribute of the corresponding <em>feature</em> of the dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.train_dataset.features[<span class="hljs-string">"label"</span>].names</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[<span class="hljs-string">'entailment'</span>, <span class="hljs-string">'neutral'</span>, <span class="hljs-string">'contradiction'</span>]</pre></div> <p>So <code>1</code> means <code>neutral</code>, which means the two sentences we saw above are not in contradiction, and the first one does not imply the second one. That seems correct!</p> <p>We don’t have token type IDs here, since DistilBERT does not expect them; if you have some in your model, you should also make sure that they properly match where the first and second sentences are in the input.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>✏️ <strong>Your turn!</strong> Check that everything seems correct with the second element of the training dataset.</p></div> <p>We are only doing the check on the training set here, but you should of course double-check the validation and test sets the same way.</p> <p>Now that we know our datasets look good, it’s time to check the next step of the training pipeline.</p> <h3 class="relative group"><a id="from-datasets-to-dataloaders" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#from-datasets-to-dataloaders"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>From datasets to dataloaders</span></h3> <p>The next thing that can go wrong in the training pipeline is when the <code>Trainer</code> tries to form batches from the training or validation set. Once you are sure the <code>Trainer</code>’s datasets are correct, you can try to manually form a batch by executing the following (replace <code>train</code> with <code>eval</code> for the validation dataloader):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> trainer.get_train_dataloader(): <span class="hljs-keyword">break</span></pre></div> <p>This code creates the training dataloader, then iterates through it, stopping at the first iteration. If the code executes without error, you have the first training batch that you can inspect, and if the code errors out, you know for sure the problem is in the dataloader, as is the case here:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>~/git/transformers/src/transformers/data/data_collator.py <span class="hljs-keyword">in</span> torch_default_data_collator(features) <span class="hljs-number">105</span> batch[k] = torch.stack([f[k] <span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> features]) <span class="hljs-number">106</span> <span class="hljs-keyword">else</span>: --&gt; <span class="hljs-number">107</span> batch[k] = torch.tensor([f[k] <span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> features]) <span class="hljs-number">108</span> <span class="hljs-number">109</span> <span class="hljs-keyword">return</span> batch ValueError: expected sequence of length <span class="hljs-number">45</span> at dim <span class="hljs-number">1</span> (got <span class="hljs-number">76</span>)</pre></div> <p>Inspecting the last frame of the traceback should be enough to give you a clue, but let’s do a bit more digging. Most of the problems during batch creation arise because of the collation of examples into a single batch, so the first thing to check when in doubt is what <code>collate_fn</code> your <code>DataLoader</code> is using:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>data_collator = trainer.get_train_dataloader().collate_fn data_collator</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>&lt;function transformers.data.data_collator.default_data_collator(features: <span class="hljs-type">List</span>[InputDataClass], return_tensors=<span class="hljs-string">'pt'</span>) -&gt; <span class="hljs-type">Dict</span>[<span class="hljs-built_in">str</span>, <span class="hljs-type">Any</span>]&gt;</pre></div> <p>So this is the <code>default_data_collator</code>, but that’s not what we want in this case. We want to pad our examples to the longest sentence in the batch, which is done by the <code>DataCollatorWithPadding</code> collator. And this data collator is supposed to be used by default by the <code>Trainer</code>, so why is it not used here?</p> <p>The answer is because we did not pass the <code>tokenizer</code> to the <code>Trainer</code>, so it couldn’t create the <code>DataCollatorWithPadding</code> we want. In practice, you should never hesitate to explicitly pass along the data collator you want to use, to make sure you avoid these kinds of errors. Let’s adapt our code to do exactly that:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-keyword">import</span> evaluate <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> ( AutoTokenizer, AutoModelForSequenceClassification, DataCollatorWithPadding, TrainingArguments, Trainer, ) raw_datasets = load_dataset(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mnli"</span>) model_checkpoint = <span class="hljs-string">"distilbert-base-uncased"</span> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) <span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_function</span>(<span class="hljs-params">examples</span>): <span class="hljs-keyword">return</span> tokenizer(examples[<span class="hljs-string">"premise"</span>], examples[<span class="hljs-string">"hypothesis"</span>], truncation=<span class="hljs-literal">True</span>) tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>(preprocess_function, batched=<span class="hljs-literal">True</span>) model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint) args = TrainingArguments( <span class="hljs-string">f"distilbert-finetuned-mnli"</span>, evaluation_strategy=<span class="hljs-string">"epoch"</span>, save_strategy=<span class="hljs-string">"epoch"</span>, learning_rate=<span class="hljs-number">2e-5</span>, num_train_epochs=<span class="hljs-number">3</span>, weight_decay=<span class="hljs-number">0.01</span>, ) metric = evaluate.load(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mnli"</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_pred</span>): predictions, labels = eval_pred <span class="hljs-keyword">return</span> metric.compute(predictions=predictions, references=labels) data_collator = DataCollatorWithPadding(tokenizer=tokenizer) trainer = Trainer( model, args, train_dataset=tokenized_datasets[<span class="hljs-string">"train"</span>], eval_dataset=tokenized_datasets[<span class="hljs-string">"validation_matched"</span>], compute_metrics=compute_metrics, data_collator=data_collator, tokenizer=tokenizer, ) trainer.train()</pre></div> <p>The good news? We don’t get the same error as before, which is definitely progress. The bad news? We get an infamous CUDA error instead:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`</pre></div> <p>This is bad because CUDA errors are extremely hard to debug in general. We will see in a minute how to solve this, but first let’s finish our analysis of batch creation.</p> <p>If you are sure your data collator is the right one, you should try to apply it on a couple of samples of your dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>data_collator = trainer.get_train_dataloader().collate_fn batch = data_collator([trainer.train_dataset[i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">4</span>)])</pre></div> <p>This code will fail because the <code>train_dataset</code> contains string columns, which the <code>Trainer</code> usually removes. You can remove them manually, or if you want to replicate exactly what the <code>Trainer</code> is doing behind the scenes, you can call the private <code>Trainer._remove_unused_columns()</code> method that does that:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>data_collator = trainer.get_train_dataloader().collate_fn actual_train_set = trainer._remove_unused_columns(trainer.train_dataset) batch = data_collator([actual_train_set[i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">4</span>)])</pre></div> <p>You should then be able to manually debug what happens inside the data collator if the error persists.</p> <p>Now that we’ve debugged the batch creation process, it’s time to pass one through the model!</p> <h3 class="relative group"><a id="going-through-the-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#going-through-the-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Going through the model</span></h3> <p>You should be able to get a batch by executing the following command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> trainer.get_train_dataloader(): <span class="hljs-keyword">break</span></pre></div> <p>If you’re running this code in a notebook, you may get a CUDA error that’s similar to the one we saw earlier, in which case you need to restart your notebook and reexecute the last snippet without the <code>trainer.train()</code> line. That’s the second most annoying thing about CUDA errors: they irremediably break your kernel. The most annoying thing about them is the fact that they are hard to debug.</p> <p>Why is that? It has to do with the way GPUs work. They are extremely efficient at executing a lot of operations in parallel, but the drawback is that when one of those instructions results in an error, you don’t know it instantly. It’s only when the program calls a synchronization of the multiple processes on the GPU that it will realize something went wrong, so the error is actually raised at a place that has nothing to do with what created it. For instance, if we look at our previous traceback, the error was raised during the backward pass, but we will see in a minute that it actually stems from something in the forward pass.</p> <p>So how do we debug those errors? The answer is easy: we don’t. Unless your CUDA error is an out-of-memory error (which means there is not enough memory in your GPU), you should always go back to the CPU to debug it.</p> <p>To do this in our case, we just have to put the model back on the CPU and call it on our batch — the batch returned by the <code>DataLoader</code> has not been moved to the GPU yet:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>outputs = trainer.model.cpu()(**batch)</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>~/.pyenv/versions/<span class="hljs-number">3.7</span><span class="hljs-number">.9</span>/envs/base/lib/python3<span class="hljs-number">.7</span>/site-packages/torch/nn/functional.py <span class="hljs-keyword">in</span> nll_loss(<span class="hljs-built_in">input</span>, target, weight, size_average, ignore_index, reduce, reduction) <span class="hljs-number">2386</span> ) <span class="hljs-number">2387</span> <span class="hljs-keyword">if</span> dim == <span class="hljs-number">2</span>: -&gt; <span class="hljs-number">2388</span> ret = torch._C._nn.nll_loss(<span class="hljs-built_in">input</span>, target, weight, _Reduction.get_enum(reduction), ignore_index) <span class="hljs-number">2389</span> <span class="hljs-keyword">elif</span> dim == <span class="hljs-number">4</span>: <span class="hljs-number">2390</span> ret = torch._C._nn.nll_loss2d(<span class="hljs-built_in">input</span>, target, weight, _Reduction.get_enum(reduction), ignore_index) IndexError: Target <span class="hljs-number">2</span> <span class="hljs-keyword">is</span> out of bounds.</pre></div> <p>So, the picture is getting clearer. Instead of having a CUDA error, we now have an <code>IndexError</code> in the loss computation (so nothing to do with the backward pass, as we said earlier). More precisely, we can see that it’s target 2 that creates the error, so this is a very good moment to check the number of labels of our model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.model.config.num_labels</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-number">2</span></pre></div> <p>With two labels, only 0s and 1s are allowed as targets, but according to the error message we got a 2. Getting a 2 is actually normal: if we remember the label names we extracted earlier, there were three, so we have indices 0, 1, and 2 in our dataset. The problem is that we didn’t tell that to our model, which should have been created with three labels. So let’s fix that!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-keyword">import</span> evaluate <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> ( AutoTokenizer, AutoModelForSequenceClassification, DataCollatorWithPadding, TrainingArguments, Trainer, ) raw_datasets = load_dataset(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mnli"</span>) model_checkpoint = <span class="hljs-string">"distilbert-base-uncased"</span> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) <span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_function</span>(<span class="hljs-params">examples</span>): <span class="hljs-keyword">return</span> tokenizer(examples[<span class="hljs-string">"premise"</span>], examples[<span class="hljs-string">"hypothesis"</span>], truncation=<span class="hljs-literal">True</span>) tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>(preprocess_function, batched=<span class="hljs-literal">True</span>) model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint, num_labels=<span class="hljs-number">3</span>) args = TrainingArguments( <span class="hljs-string">f"distilbert-finetuned-mnli"</span>, evaluation_strategy=<span class="hljs-string">"epoch"</span>, save_strategy=<span class="hljs-string">"epoch"</span>, learning_rate=<span class="hljs-number">2e-5</span>, num_train_epochs=<span class="hljs-number">3</span>, weight_decay=<span class="hljs-number">0.01</span>, ) metric = evaluate.load(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mnli"</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_pred</span>): predictions, labels = eval_pred <span class="hljs-keyword">return</span> metric.compute(predictions=predictions, references=labels) data_collator = DataCollatorWithPadding(tokenizer=tokenizer) trainer = Trainer( model, args, train_dataset=tokenized_datasets[<span class="hljs-string">"train"</span>], eval_dataset=tokenized_datasets[<span class="hljs-string">"validation_matched"</span>], compute_metrics=compute_metrics, data_collator=data_collator, tokenizer=tokenizer, )</pre></div> <p>We aren’t including the <code>trainer.train()</code> line yet, to take the time to check that everything looks good. If we request a batch and pass it to our model, it now works without error!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> trainer.get_train_dataloader(): <span class="hljs-keyword">break</span> outputs = trainer.model.cpu()(**batch)</pre></div> <p>The next step is then to move back to the GPU and check that everything still works:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch device = torch.device(<span class="hljs-string">"cuda"</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">"cpu"</span>) batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()} outputs = trainer.model.to(device)(**batch)</pre></div> <p>If you still get an error, make sure you restart your notebook and only execute the last version of the script.</p> <h3 class="relative group"><a id="performing-one-optimization-step" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#performing-one-optimization-step"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Performing one optimization step</span></h3> <p>Now that we know that we can build batches that actually go through the model, we are ready for the next step of the training pipeline: computing the gradients and performing an optimization step.</p> <p>The first part is just a matter of calling the <code>backward()</code> method on the loss:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>loss = outputs.loss loss.backward()</pre></div> <p>It’s pretty rare to get an error at this stage, but if you do get one, make sure to go back to the CPU to get a helpful error message.</p> <p>To perform the optimization step, we just need to create the <code>optimizer</code> and call its <code>step()</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.create_optimizer() trainer.optimizer.step()</pre></div> <p>Again, if you’re using the default optimizer in the <code>Trainer</code>, you shouldn’t get an error at this stage, but if you have a custom optimizer, there might be some problems to debug here. Don’t forget to go back to the CPU if you get a weird CUDA error at this stage. Speaking of CUDA errors, earlier we mentioned a special case. Let’s have a look at that now.</p> <h3 class="relative group"><a id="dealing-with-cuda-out-of-memory-errors" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#dealing-with-cuda-out-of-memory-errors"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Dealing with CUDA out-of-memory errors</span></h3> <p>Whenever you get an error message that starts with <code>RuntimeError: CUDA out of memory</code>, this indicates that you are out of GPU memory. This is not directly linked to your code, and it can happen with a script that runs perfectly fine. This error means that you tried to put too many things in the internal memory of your GPU, and that resulted in an error. Like with other CUDA errors, you will need to restart your kernel to be in a spot where you can run your training again.</p> <p>To solve this issue, you just need to use less GPU space — something that is often easier said than done. First, make sure you don’t have two models on the GPU at the same time (unless that’s required for your problem, of course). Then, you should probably reduce your batch size, as it directly affects the sizes of all the intermediate outputs of the model and their gradients. If the problem persists, consider using a smaller version of your model.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>In the next part of the course, we’ll look at more advanced techniques that can help you reduce your memory footprint and let you fine-tune the biggest models.</p></div> <h3 class="relative group"><a id="evaluating-the-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#evaluating-the-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Evaluating the model</span></h3> <p>Now that we’ve solved all the issues with our code, everything is perfect and the training should run smoothly, right? Not so fast! If you run the <code>trainer.train()</code> command, everything will look good at first, but after a while you will get the following:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># This will take a long time and error out, so you shouldn't run this cell</span> trainer.train()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>TypeError: only size-<span class="hljs-number">1</span> arrays can be converted to Python scalars</pre></div> <p>You will realize this error appears during the evaluation phase, so this is the last thing we will need to debug.</p> <p>You can run the evaluation loop of the <code>Trainer</code> independently form the training like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>trainer.evaluate()</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>TypeError: only size-<span class="hljs-number">1</span> arrays can be converted to Python scalars</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 You should always make sure you can run <code>trainer.evaluate()</code> before launching <code>trainer.train()</code>, to avoid wasting lots of compute resources before hitting an error.</p></div> <p>Before attempting to debug a problem in the evaluation loop, you should first make sure that you’ve had a look at the data, are able to form a batch properly, and can run your model on it. We’ve completed all of those steps, so the following code can be executed without error:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> trainer.get_eval_dataloader(): <span class="hljs-keyword">break</span> batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()} <span class="hljs-keyword">with</span> torch.no_grad(): outputs = trainer.model(**batch)</pre></div> <p>The error comes later, at the end of the evaluation phase, and if we look at the traceback we see this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>~/git/datasets/src/datasets/metric.py <span class="hljs-keyword">in</span> add_batch(self, predictions, references) <span class="hljs-number">431</span> <span class="hljs-string">""" 432 batch = {"predictions": predictions, "references": references} --&gt; 433 batch = self.info.features.encode_batch(batch) 434 if self.writer is None: 435 self._init_writer()</span></pre></div> <p>This tells us that the error originates in the <code>datasets/metric.py</code> module — so this is a problem with our <code>compute_metrics()</code> function. It takes a tuple with the logits and the labels as NumPy arrays, so let’s try to feed it that:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>predictions = outputs.logits.cpu().numpy() labels = batch[<span class="hljs-string">"labels"</span>].cpu().numpy() compute_metrics((predictions, labels))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>TypeError: only size-<span class="hljs-number">1</span> arrays can be converted to Python scalars</pre></div> <p>We get the same error, so the problem definitely lies with that function. If we look back at its code, we see it’s just forwarding the <code>predictions</code> and the <code>labels</code> to <code>metric.compute()</code>. So is there a problem with that method? Not really. Let’s have a quick look at the shapes:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>predictions.shape, labels.shape</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>((<span class="hljs-number">8</span>, <span class="hljs-number">3</span>), (<span class="hljs-number">8</span>,))</pre></div> <p>Our predictions are still logits, not the actual predictions, which is why the metric is returning this (somewhat obscure) error. The fix is pretty easy; we just have to add an argmax in the <code>compute_metrics()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_pred</span>): predictions, labels = eval_pred predictions = np.argmax(predictions, axis=<span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> metric.compute(predictions=predictions, references=labels) compute_metrics((predictions, labels))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'accuracy'</span>: <span class="hljs-number">0.625</span>}</pre></div> <p>Now our error is fixed! This was the last one, so our script will now train a model properly.</p> <p>For reference, here is the completely fixed script:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-keyword">import</span> evaluate <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> ( AutoTokenizer, AutoModelForSequenceClassification, DataCollatorWithPadding, TrainingArguments, Trainer, ) raw_datasets = load_dataset(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mnli"</span>) model_checkpoint = <span class="hljs-string">"distilbert-base-uncased"</span> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) <span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_function</span>(<span class="hljs-params">examples</span>): <span class="hljs-keyword">return</span> tokenizer(examples[<span class="hljs-string">"premise"</span>], examples[<span class="hljs-string">"hypothesis"</span>], truncation=<span class="hljs-literal">True</span>) tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>(preprocess_function, batched=<span class="hljs-literal">True</span>) model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint, num_labels=<span class="hljs-number">3</span>) args = TrainingArguments( <span class="hljs-string">f"distilbert-finetuned-mnli"</span>, evaluation_strategy=<span class="hljs-string">"epoch"</span>, save_strategy=<span class="hljs-string">"epoch"</span>, learning_rate=<span class="hljs-number">2e-5</span>, num_train_epochs=<span class="hljs-number">3</span>, weight_decay=<span class="hljs-number">0.01</span>, ) metric = evaluate.load(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"mnli"</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_pred</span>): predictions, labels = eval_pred predictions = np.argmax(predictions, axis=<span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> metric.compute(predictions=predictions, references=labels) data_collator = DataCollatorWithPadding(tokenizer=tokenizer) trainer = Trainer( model, args, train_dataset=tokenized_datasets[<span class="hljs-string">"train"</span>], eval_dataset=tokenized_datasets[<span class="hljs-string">"validation_matched"</span>], compute_metrics=compute_metrics, data_collator=data_collator, tokenizer=tokenizer, ) trainer.train()</pre></div> <p>In this instance, there are no more problems, and our script will fine-tune a model that should give reasonable results. But what can we do when the training proceeds without any error, and the model trained does not perform well at all? That’s the hardest part of machine learning, and we’ll show you a few techniques that can help.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 If you’re using a manual training loop, the same steps apply to debug your training pipeline, but it’s easier to separate them. Make sure you have not forgotten the <code>model.eval()</code> or <code>model.train()</code> at the right places, or the <code>zero_grad()</code> at each step, however!</p></div> <h2 class="relative group"><a id="debugging-silent-errors-during-training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#debugging-silent-errors-during-training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Debugging silent errors during training</span></h2> <p>What can we do to debug a training that completes without error but doesn’t get good results? We’ll give you some pointers here, but be aware that this kind of debugging is the hardest part of machine learning, and there is no magical answer.</p> <h3 class="relative group"><a id="check-your-data-again" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#check-your-data-again"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Check your data (again!)</span></h3> <p>Your model will only learn something if it’s actually possible to learn anything from your data. If there is a bug that corrupts the data or the labels are attributed randomly, it’s very likely you won’t get any model training on your dataset. So always start by double-checking your decoded inputs and labels, and ask yourself the following questions:</p> <ul><li>Is the decoded data understandable?</li> <li>Do you agree with the labels?</li> <li>Is there one label that’s more common than the others?</li> <li>What should the loss/metric be if the model predicted a random answer/always the same answer?</li></ul> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>⚠️ If you are doing distributed training, print samples of your dataset in each process and triple-check that you get the same thing. One common bug is to have some source of randomness in the data creation that makes each process have a different version of the dataset.</p></div> <p>After looking at your data, go through a few of the model’s predictions and decode them too. If the model is always predicting the same thing, it might be because your dataset is biased toward one category (for classification problems); techniques like oversampling rare classes might help.</p> <p>If the loss/metric you get on your initial model is very different from the loss/metric you would expect for random predictions, double-check the way your loss or metric is computed, as there is probably a bug there. If you are using several losses that you add at the end, make sure they are of the same scale.</p> <p>When you are sure your data is perfect, you can see if the model is capable of training on it with one simple test.</p> <h3 class="relative group"><a id="overfit-your-model-on-one-batch" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#overfit-your-model-on-one-batch"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Overfit your model on one batch</span></h3> <p>Overfitting is usually something we try to avoid when training, as it means the model is not learning to recognize the general features we want it to but is instead just memorizing the training samples. However, trying to train your model on one batch over and over again is a good test to check if the problem as you framed it can be solved by the model you are attempting to train. It will also help you see if your initial learning rate is too high.</p> <p>Doing this once you have defined your <code>Trainer</code> is really easy; just grab a batch of training data, then run a small manual training loop only using that batch for something like 20 steps:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> trainer.get_train_dataloader(): <span class="hljs-keyword">break</span> batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()} trainer.create_optimizer() <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">20</span>): outputs = trainer.model(**batch) loss = outputs.loss loss.backward() trainer.optimizer.step() trainer.optimizer.zero_grad()</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>💡 If your training data is unbalanced, make sure to build a batch of training data containing all the labels.</p></div> <p>The resulting model should have close-to-perfect results on the same <code>batch</code>. Let’s compute the metric on the resulting predictions:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">with</span> torch.no_grad(): outputs = trainer.model(**batch) preds = outputs.logits labels = batch[<span class="hljs-string">"labels"</span>] compute_metrics((preds.cpu().numpy(), labels.cpu().numpy()))</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>{<span class="hljs-string">'accuracy'</span>: <span class="hljs-number">1.0</span>}</pre></div> <p>100% accuracy, now this is a nice example of overfitting (meaning that if you try your model on any other sentence, it will very likely give you a wrong answer)!</p> <p>If you don’t manage to have your model obtain perfect results like this, it means there is something wrong with the way you framed the problem or your data, so you should fix that. Only when you manage to pass the overfitting test can you be sure that your model can actually learn something.</p> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>⚠️ You will have to recreate your model and your <code>Trainer</code> after this test, as the model obtained probably won’t be able to recover and learn something useful on your full dataset.</p></div> <h3 class="relative group"><a id="dont-tune-anything-until-you-have-a-first-baseline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#dont-tune-anything-until-you-have-a-first-baseline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Don't tune anything until you have a first baseline</span></h3> <p>Hyperparameter tuning is always emphasized as being the hardest part of machine learning, but it’s just the last step to help you gain a little bit on the metric. Most of the time, the default hyperparameters of the <code>Trainer</code> will work just fine to give you good results, so don’t launch into a time-consuming and costly hyperparameter search until you have something that beats the baseline you have on your dataset.</p> <p>Once you have a good enough model, you can start tweaking a bit. Don’t try launching a thousand runs with different hyperparameters, but compare a couple of runs with different values for one hyperparameter to get an idea of which has the greatest impact.</p> <p>If you are tweaking the model itself, keep it simple and don’t try anything you can’t reasonably justify. Always make sure you go back to the overfitting test to verify that your change hasn’t had any unintended consequences.</p> <h3 class="relative group"><a id="ask-for-help" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#ask-for-help"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Ask for help</span></h3> <p>Hopefully you will have found some advice in this section that helped you solve your issue, but if that’s not the case, remember you can always ask the community on the <a href="https://discuss.huggingface.co/" rel="nofollow">forums</a>.</p> <p>Here are some additional resources that may prove helpful:</p> <ul><li><a href="https://docs.google.com/presentation/d/1yHLPvPhUs2KGI5ZWo0sU-PKU3GimAk3iTsI38Z-B5Gw/edit#slide=id.p" rel="nofollow">“Reproducibility as a vehicle for engineering best practices”</a> by Joel Grus</li> <li><a href="https://towardsdatascience.com/checklist-for-debugging-neural-networks-d8b2a9434f21" rel="nofollow">“Checklist for debugging neural networks”</a> by Cecelia Shao</li> <li><a href="https://medium.com/@keeper6928/how-to-unit-test-machine-learning-code-57cf6fd81765" rel="nofollow">“How to unit test machine learning code”</a> by Chase Roberts</li> <li><a href="http://karpathy.github.io/2019/04/25/recipe/" rel="nofollow">“A Recipe for Training Neural Networks”</a> by Andrej Karpathy</li></ul> <p>Of course, not every problem you encounter when training neural nets is your own fault! If you encounter something in the 🤗 Transformers or 🤗 Datasets library that does not seem right, you may have encountered a bug. You should definitely tell us all about it, and in the next section we’ll explain exactly how to do that.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter8/3?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Asking for help on the forums</a> <a href="/learn/nlp-course/chapter8/5?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">How to write a good issue<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;debugging-the-training-pipeline&quot;,&quot;url&quot;:&quot;#debugging-the-training-pipeline&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;debugging-the-training-pipeline&quot;,&quot;url&quot;:&quot;#debugging-the-training-pipeline&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Check your data&quot;,&quot;id&quot;:&quot;check-your-data&quot;,&quot;url&quot;:&quot;#check-your-data&quot;},{&quot;title&quot;:&quot;From datasets to dataloaders&quot;,&quot;id&quot;:&quot;from-datasets-to-dataloaders&quot;,&quot;url&quot;:&quot;#from-datasets-to-dataloaders&quot;},{&quot;title&quot;:&quot;Going through the model&quot;,&quot;id&quot;:&quot;going-through-the-model&quot;,&quot;url&quot;:&quot;#going-through-the-model&quot;},{&quot;title&quot;:&quot;Performing one optimization step&quot;,&quot;id&quot;:&quot;performing-one-optimization-step&quot;,&quot;url&quot;:&quot;#performing-one-optimization-step&quot;},{&quot;title&quot;:&quot;Dealing with CUDA out-of-memory errors&quot;,&quot;id&quot;:&quot;dealing-with-cuda-out-of-memory-errors&quot;,&quot;url&quot;:&quot;#dealing-with-cuda-out-of-memory-errors&quot;},{&quot;title&quot;:&quot;Evaluating the model&quot;,&quot;id&quot;:&quot;evaluating-the-model&quot;,&quot;url&quot;:&quot;#evaluating-the-model&quot;}]},{&quot;title&quot;:&quot;Debugging silent errors during training&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;debugging-silent-errors-during-training&quot;,&quot;url&quot;:&quot;#debugging-silent-errors-during-training&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Check your data (again!)&quot;,&quot;id&quot;:&quot;check-your-data-again&quot;,&quot;url&quot;:&quot;#check-your-data-again&quot;},{&quot;title&quot;:&quot;Overfit your model on one batch&quot;,&quot;id&quot;:&quot;overfit-your-model-on-one-batch&quot;,&quot;url&quot;:&quot;#overfit-your-model-on-one-batch&quot;},{&quot;title&quot;:&quot;Don't tune anything until you have a first baseline&quot;,&quot;id&quot;:&quot;dont-tune-anything-until-you-have-a-first-baseline&quot;,&quot;url&quot;:&quot;#dont-tune-anything-until-you-have-a-first-baseline&quot;},{&quot;title&quot;:&quot;Ask for help&quot;,&quot;id&quot;:&quot;ask-for-help&quot;,&quot;url&quot;:&quot;#ask-for-help&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#debugging-the-training-pipeline" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-debugging-the-training-pipeline"><wbr>Debugging the training pipeline</a> <a href="#debugging-the-training-pipeline" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-debugging-the-training-pipeline"><wbr>Debugging the training pipeline</a> <a href="#check-your-data" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-check-your-data"><wbr>Check your data</a> <a href="#from-datasets-to-dataloaders" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-from-datasets-to-dataloaders"><wbr>From datasets to dataloaders</a> <a href="#going-through-the-model" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-going-through-the-model"><wbr>Going through the model</a> <a href="#performing-one-optimization-step" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-performing-one-optimization-step"><wbr>Performing one optimization step</a> <a href="#dealing-with-cuda-out-of-memory-errors" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-dealing-with-cuda-out-of-memory-errors"><wbr>Dealing with CUD<wbr>A out-of-memory errors</a> <a href="#evaluating-the-model" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-evaluating-the-model"><wbr>Evaluating the model</a> <a href="#debugging-silent-errors-during-training" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-debugging-silent-errors-during-training"><wbr>Debugging silent errors during training</a> <a href="#check-your-data-again" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-check-your-data-again"><wbr>Check your data (again!)</a> <a href="#overfit-your-model-on-one-batch" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-overfit-your-model-on-one-batch"><wbr>Overfit your model on one batch</a> <a href="#dont-tune-anything-until-you-have-a-first-baseline" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-dont-tune-anything-until-you-have-a-first-baseline"><wbr>Don't tune anything until you have a first baseline</a> <a href="#ask-for-help" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-ask-for-help"><wbr>Ask for help</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter8/4" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter8/4"); } </script> <iframe name="__privateStripeMetricsController4080" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter8%2F4%3Ffw%3Dpt&amp;title=Debugging%20the%20training%20pipeline%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:33.870Z
How to write a good issue - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter8/5?fw=pt
## [](#how-to-write-a-good-issue)How to write a good issue [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-8-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter8/section5.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter8/section5.ipynb) When you encounter something that doesn’t seem right with one of the Hugging Face libraries, you should definitely let us know so we can fix it (the same goes for any open source library, for that matter). If you are not completely certain whether the bug lies in your own code or one of our libraries, the first place to check is the [forums](https://discuss.huggingface.co/). The community will help you figure this out, and the Hugging Face team also closely watches the discussions there. When you are sure you have a bug in your hand, the first step is to build a minimal reproducible example. ## [](#creating-a-minimal-reproducible-example)Creating a minimal reproducible example It’s very important to isolate the piece of code that produces the bug, as no one in the Hugging Face team is a magician (yet), and they can’t fix what they can’t see. A minimal reproducible example should, as the name indicates, be reproducible. This means that it should not rely on any external files or data you may have. Try to replace the data you are using with some dummy values that look like your real ones and still produce the same error. 🚨 Many issues in the 🤗 Transformers repository are unsolved because the data used to reproduce them is not accessible. Once you have something that is self-contained, you can try to reduce it into even less lines of code, building what we call a _minimal reproducible example_. While this requires a bit more work on your side, you will almost be guaranteed to get help and a fix if you provide a nice, short bug reproducer. If you feel comfortable enough, go inspect the source code where your bug happens. You might find a solution to your problem (in which case you can even suggest a pull request to fix it), but more generally, this can help the maintainers better understand the source when they read your report. ## [](#filling-out-the-issue-template)Filling out the issue template When you file your issue, you will notice there is a template to fill out. We will follow the one for [🤗 Transformers issues](https://github.com/huggingface/transformers/issues/new/choose) here, but the same kind of information will be required if you report an issue in another repository. Don’t leave the template blank: taking the time to fill it in will maximize your chances of getting an answer and solving your problem. In general, when filing an issue, always stay courteous. This is an open source project, so you are using free software, and no one has any obligation to help you. You may include what you feel is justified criticism in your issue, but then the maintainers may very well take it badly and not be in a rush help you. Make sure you read the [code of conduct](https://github.com/huggingface/transformers/blob/master/CODE_OF_CONDUCT.md) of the project. ### [](#including-your-environment-information)Including your environment information 🤗 Transformers provides a utility to get all the information we need about your environment. Just type the following in your terminal: and you should get something like this: ``` Copy-and-paste the text below in your GitHub issue and FILL OUT the two last points. - `transformers` version: 4.12.0.dev0 - Platform: Linux-5.10.61-1-MANJARO-x86_64-with-arch-Manjaro-Linux - Python version: 3.7.9 - PyTorch version (GPU?): 1.8.1+cu111 (True) - Tensorflow version (GPU?): 2.5.0 (True) - Flax version (CPU?/GPU?/TPU?): 0.3.4 (cpu) - Jax version: 0.2.13 - JaxLib version: 0.1.65 - Using GPU in script?: <fill in> - Using distributed or parallel set-up in script?: <fill in>``` You can also add a `!` at the beginning of the `transformers-cli env` command to execute it from a notebook cell, and then copy and paste the result at the beginning of your issue. ### [](#tagging-people)Tagging people Tagging people by typing an `@` followed by their GitHub handle will send them a notification so they will see your issue and might reply quicker. Use this with moderation, because the people you tag might not appreciate being notified if it’s something they have no direct link to. If you have looked at the source files related to your bug, you should tag the last person that made changes at the line you think is responsible for your problem (you can find this information by looking at said line on GitHub, selecting it, then clicking “View git blame”). Otherwise, the template offers suggestions of people to tag. In general, never tag more than three people! ### [](#including-a-reproducible-example)Including a reproducible example If you have managed to create a self-contained example that produces the bug, now is the time to include it! Type a line with three backticks followed by `python`, like this: then paste in your minimal reproducible example and type a new line with three backticks. This will ensure your code is properly formatted. If you didn’t manage to create a reproducible example, explain in clear steps how you got to your issue. Include a link to a Google Colab notebook where you got the error if you can. The more information you share, the better able the maintainers will be to reply to you. In all cases, you should copy and paste the whole error message you are getting. If you’re working in Colab, remember that some of the frames may be automatically collapsed in the stack trace, so make sure you expand them before copying. Like with the code sample, put that error message between two lines with three backticks, so it’s properly formatted. ### [](#describing-the-expected-behavior[[describing-the-expected-behavior]])Describing the expected behavior\[\[describing-the-expected-behavior\]\] Explain in a few lines what you expected to get, so that the maintainers get a full grasp of the problem. This part is generally pretty obvious, so it should fit in one sentence, but in some cases you may have a lot to say. ## [](#and-then-what?[[and-then-what]])And then what?\[\[and-then-what\]\] Once your issue is filed, make sure to quickly check everything looks okay. You can edit the issue if you made a mistake, or even change its title if you realize the problem is different from what you initially thought. There is no point pinging people if you don’t get an answer. If no one helps you in a few days, it’s likely that no one could make sense of your problem. Don’t hesitate to go back to the reproducible example. Can you make it shorter and more to the point? If you don’t get an answer in a week, you can leave a message gently asking for help, especially if you’ve edited your issue to include more information on the problem.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="How to write a good issue - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter8/5"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>How to write a good issue - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;how-to-write-a-good-issue&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;creating-a-minimal-reproducible-example&quot;,&quot;title&quot;:&quot;Creating a minimal reproducible example&quot;},{&quot;local&quot;:&quot;filling-out-the-issue-template&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;including-your-environment-information&quot;,&quot;title&quot;:&quot;Including your environment information&quot;},{&quot;local&quot;:&quot;tagging-people&quot;,&quot;title&quot;:&quot;Tagging people&quot;},{&quot;local&quot;:&quot;including-a-reproducible-example&quot;,&quot;title&quot;:&quot;Including a reproducible example&quot;}],&quot;title&quot;:&quot;Filling out the issue template&quot;}],&quot;title&quot;:&quot;How to write a good issue&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter8/5&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;How to write a good issue&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">How to write a good issue</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/2?fw=pt">What to do when you get an error </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/3?fw=pt">Asking for help on the forums </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/4?fw=pt">Debugging the training pipeline </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter8/5?fw=pt">How to write a good issue </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/6?fw=pt">Part 2 completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/7?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="how-to-write-a-good-issue" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#how-to-write-a-good-issue"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>How to write a good issue</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-8-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter8/section5.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter8/section5.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>When you encounter something that doesn’t seem right with one of the Hugging Face libraries, you should definitely let us know so we can fix it (the same goes for any open source library, for that matter). If you are not completely certain whether the bug lies in your own code or one of our libraries, the first place to check is the <a href="https://discuss.huggingface.co/" rel="nofollow">forums</a>. The community will help you figure this out, and the Hugging Face team also closely watches the discussions there.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/_PAli-V4wj0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>When you are sure you have a bug in your hand, the first step is to build a minimal reproducible example.</p> <h2 class="relative group"><a id="creating-a-minimal-reproducible-example" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#creating-a-minimal-reproducible-example"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Creating a minimal reproducible example</span></h2> <p>It’s very important to isolate the piece of code that produces the bug, as no one in the Hugging Face team is a magician (yet), and they can’t fix what they can’t see. A minimal reproducible example should, as the name indicates, be reproducible. This means that it should not rely on any external files or data you may have. Try to replace the data you are using with some dummy values that look like your real ones and still produce the same error.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>🚨 Many issues in the 🤗 Transformers repository are unsolved because the data used to reproduce them is not accessible.</p></div> <p>Once you have something that is self-contained, you can try to reduce it into even less lines of code, building what we call a <em>minimal reproducible example</em>. While this requires a bit more work on your side, you will almost be guaranteed to get help and a fix if you provide a nice, short bug reproducer.</p> <p>If you feel comfortable enough, go inspect the source code where your bug happens. You might find a solution to your problem (in which case you can even suggest a pull request to fix it), but more generally, this can help the maintainers better understand the source when they read your report.</p> <h2 class="relative group"><a id="filling-out-the-issue-template" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#filling-out-the-issue-template"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Filling out the issue template</span></h2> <p>When you file your issue, you will notice there is a template to fill out. We will follow the one for <a href="https://github.com/huggingface/transformers/issues/new/choose" rel="nofollow">🤗 Transformers issues</a> here, but the same kind of information will be required if you report an issue in another repository. Don’t leave the template blank: taking the time to fill it in will maximize your chances of getting an answer and solving your problem.</p> <p>In general, when filing an issue, always stay courteous. This is an open source project, so you are using free software, and no one has any obligation to help you. You may include what you feel is justified criticism in your issue, but then the maintainers may very well take it badly and not be in a rush help you. Make sure you read the <a href="https://github.com/huggingface/transformers/blob/master/CODE_OF_CONDUCT.md" rel="nofollow">code of conduct</a> of the project.</p> <h3 class="relative group"><a id="including-your-environment-information" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#including-your-environment-information"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Including your environment information</span></h3> <p>🤗 Transformers provides a utility to get all the information we need about your environment. Just type the following in your terminal:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>transformers-<span class="hljs-keyword">cli</span> env</pre></div> <p>and you should get something like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">Copy</span>-<span class="hljs-keyword">and</span>-paste the <span class="hljs-type">text</span> below <span class="hljs-keyword">in</span> your GitHub issue <span class="hljs-keyword">and</span> FILL <span class="hljs-keyword">OUT</span> the two last points. - `transformers` <span class="hljs-keyword">version</span>: <span class="hljs-number">4.12</span><span class="hljs-number">.0</span>.dev0 - Platform: Linux<span class="hljs-number">-5.10</span><span class="hljs-number">.61</span><span class="hljs-number">-1</span>-MANJARO-x86_64-<span class="hljs-keyword">with</span>-arch-Manjaro-Linux - Python <span class="hljs-keyword">version</span>: <span class="hljs-number">3.7</span><span class="hljs-number">.9</span> - PyTorch version (GPU?): <span class="hljs-number">1.8</span><span class="hljs-number">.1</span>+cu111 (<span class="hljs-keyword">True</span>) - Tensorflow version (GPU?): <span class="hljs-number">2.5</span><span class="hljs-number">.0</span> (<span class="hljs-keyword">True</span>) - Flax version (CPU?/GPU?/TPU?): <span class="hljs-number">0.3</span><span class="hljs-number">.4</span> (cpu) - Jax <span class="hljs-keyword">version</span>: <span class="hljs-number">0.2</span><span class="hljs-number">.13</span> - JaxLib <span class="hljs-keyword">version</span>: <span class="hljs-number">0.1</span><span class="hljs-number">.65</span> - <span class="hljs-keyword">Using</span> GPU <span class="hljs-keyword">in</span> script?: &lt;fill <span class="hljs-keyword">in</span>&gt; - <span class="hljs-keyword">Using</span> distributed <span class="hljs-keyword">or</span> parallel <span class="hljs-keyword">set</span>-up <span class="hljs-keyword">in</span> script?: &lt;fill <span class="hljs-keyword">in</span>&gt;</pre></div> <p>You can also add a <code>!</code> at the beginning of the <code>transformers-cli env</code> command to execute it from a notebook cell, and then copy and paste the result at the beginning of your issue.</p> <h3 class="relative group"><a id="tagging-people" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tagging-people"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Tagging people</span></h3> <p>Tagging people by typing an <code>@</code> followed by their GitHub handle will send them a notification so they will see your issue and might reply quicker. Use this with moderation, because the people you tag might not appreciate being notified if it’s something they have no direct link to. If you have looked at the source files related to your bug, you should tag the last person that made changes at the line you think is responsible for your problem (you can find this information by looking at said line on GitHub, selecting it, then clicking “View git blame”).</p> <p>Otherwise, the template offers suggestions of people to tag. In general, never tag more than three people!</p> <h3 class="relative group"><a id="including-a-reproducible-example" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#including-a-reproducible-example"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Including a reproducible example</span></h3> <p>If you have managed to create a self-contained example that produces the bug, now is the time to include it! Type a line with three backticks followed by <code>python</code>, like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>```python</pre></div> <p>then paste in your minimal reproducible example and type a new line with three backticks. This will ensure your code is properly formatted.</p> <p>If you didn’t manage to create a reproducible example, explain in clear steps how you got to your issue. Include a link to a Google Colab notebook where you got the error if you can. The more information you share, the better able the maintainers will be to reply to you.</p> <p>In all cases, you should copy and paste the whole error message you are getting. If you’re working in Colab, remember that some of the frames may be automatically collapsed in the stack trace, so make sure you expand them before copying. Like with the code sample, put that error message between two lines with three backticks, so it’s properly formatted.</p> <h3 class="relative group"><a id="describing-the-expected-behavior[[describing-the-expected-behavior]]" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#describing-the-expected-behavior[[describing-the-expected-behavior]]"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Describing the expected behavior[[describing-the-expected-behavior]]</span></h3> <p>Explain in a few lines what you expected to get, so that the maintainers get a full grasp of the problem. This part is generally pretty obvious, so it should fit in one sentence, but in some cases you may have a lot to say.</p> <h2 class="relative group"><a id="and-then-what?[[and-then-what]]" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#and-then-what?[[and-then-what]]"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>And then what?[[and-then-what]]</span></h2> <p>Once your issue is filed, make sure to quickly check everything looks okay. You can edit the issue if you made a mistake, or even change its title if you realize the problem is different from what you initially thought.</p> <p>There is no point pinging people if you don’t get an answer. If no one helps you in a few days, it’s likely that no one could make sense of your problem. Don’t hesitate to go back to the reproducible example. Can you make it shorter and more to the point? If you don’t get an answer in a week, you can leave a message gently asking for help, especially if you’ve edited your issue to include more information on the problem.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter8/4?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Debugging the training pipeline</a> <a href="/learn/nlp-course/chapter8/6?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Part 2 completed!<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;how-to-write-a-good-issue&quot;,&quot;url&quot;:&quot;#how-to-write-a-good-issue&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Creating a minimal reproducible example&quot;,&quot;id&quot;:&quot;creating-a-minimal-reproducible-example&quot;,&quot;url&quot;:&quot;#creating-a-minimal-reproducible-example&quot;},{&quot;title&quot;:&quot;Filling out the issue template&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;filling-out-the-issue-template&quot;,&quot;url&quot;:&quot;#filling-out-the-issue-template&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Including your environment information&quot;,&quot;id&quot;:&quot;including-your-environment-information&quot;,&quot;url&quot;:&quot;#including-your-environment-information&quot;},{&quot;title&quot;:&quot;Tagging people&quot;,&quot;id&quot;:&quot;tagging-people&quot;,&quot;url&quot;:&quot;#tagging-people&quot;},{&quot;title&quot;:&quot;Including a reproducible example&quot;,&quot;id&quot;:&quot;including-a-reproducible-example&quot;,&quot;url&quot;:&quot;#including-a-reproducible-example&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#how-to-write-a-good-issue" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-how-to-write-a-good-issue"><wbr>How to write a good issue</a> <a href="#creating-a-minimal-reproducible-example" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-creating-a-minimal-reproducible-example"><wbr>Creating a minimal reproducible example</a> <a href="#filling-out-the-issue-template" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-filling-out-the-issue-template"><wbr>Filling out the issue template</a> <a href="#including-your-environment-information" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-including-your-environment-information"><wbr>Including your environment information</a> <a href="#tagging-people" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tagging-people"><wbr>Tagging people</a> <a href="#including-a-reproducible-example" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-including-a-reproducible-example"><wbr>Including a reproducible example</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter8/5" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter8/5"); } </script> <iframe name="__privateStripeMetricsController6730" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter8%2F5%3Ffw%3Dpt&amp;title=How%20to%20write%20a%20good%20issue%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:34.096Z
Part 2 completed! - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter8/6?fw=pt
## [](#part-2-completed)Part 2 completed! [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-8-questions) Congratulations, you’ve made it through the second part of the course! We’re actively working on the third one, so subscribe to our [newsletter](https://huggingface.curated.co/) to make sure you don’t miss its release. You should now be able to tackle a range of NLP tasks, and fine-tune or pretrain a model on them. Don’t forget to share your results with the community on the [Model Hub](https://huggingface.co/models). We can’t wait to see what you will build with the knowledge that you’ve gained!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Part 2 completed! - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter8/6"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Part 2 completed! - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;part-2-completed&quot;,&quot;title&quot;:&quot;Part 2 completed!&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter8/6&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Part 2 completed!&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Part 2 completed!</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/2?fw=pt">What to do when you get an error </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/3?fw=pt">Asking for help on the forums </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/4?fw=pt">Debugging the training pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/5?fw=pt">How to write a good issue </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter8/6?fw=pt">Part 2 completed! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/7?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="part-2-completed" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#part-2-completed"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Part 2 completed!</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-8-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>Congratulations, you’ve made it through the second part of the course! We’re actively working on the third one, so subscribe to our <a href="https://huggingface.curated.co/" rel="nofollow">newsletter</a> to make sure you don’t miss its release.</p> <p>You should now be able to tackle a range of NLP tasks, and fine-tune or pretrain a model on them. Don’t forget to share your results with the community on the <a href="https://huggingface.co/models" rel="nofollow">Model Hub</a>.</p> <p>We can’t wait to see what you will build with the knowledge that you’ve gained!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter8/5?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>How to write a good issue</a> <a href="/learn/nlp-course/chapter8/7?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">End-of-chapter quiz<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;part-2-completed&quot;,&quot;url&quot;:&quot;#part-2-completed&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#part-2-completed" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-part-2-completed"><wbr>Part 2 completed!</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter8/6" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter8/6"); } </script> <iframe name="__privateStripeMetricsController1140" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter8%2F6%3Ffw%3Dpt&amp;title=Part%202%20completed!%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:34.374Z
End-of-chapter quiz - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter8/7?fw=pt
## [](#end-of-chapter-quiz)End-of-chapter quiz [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-8-questions) Let’s test what you learned in this chapter! ### [](#1.-in-which-order-should-you-read-a-python-traceback?)1\. In which order should you read a Python traceback? ### [](#2.-what-is-a-minimal-reproducible-example?)2\. What is a minimal reproducible example? ### [](#3.-suppose-you-try-to-run-the-following-code,-which-throws-an-error:)3\. Suppose you try to run the following code, which throws an error: ``` from transformers import GPT3ForSequenceClassification # ImportError: cannot import name 'GPT3ForSequenceClassification' from 'transformers' (/Users/lewtun/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/__init__.py) # --------------------------------------------------------------------------- # ImportError Traceback (most recent call last) # /var/folders/28/k4cy5q7s2hs92xq7_h89_vgm0000gn/T/ipykernel_30848/333858878.py in <module> # ----> 1 from transformers import GPT3ForSequenceClassification # ImportError: cannot import name 'GPT3ForSequenceClassification' from 'transformers' (/Users/lewtun/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/__init__.py)``` Which of the following might be a good choice for the title of a forum topic to ask for help? ### [](#4.-suppose-you’ve-tried-to-run-<code>trainer.train()</code>-and-are-faced-with-a-cryptic-error-that-doesn’t-tell-you-exactly-where-the-error-is-coming-from.-which-of-the-following-is-the-first-place-you-should-look-for-errors-in-your-training-pipeline?)4\. Suppose you’ve tried to run `trainer.train()` and are faced with a cryptic error that doesn’t tell you exactly where the error is coming from. Which of the following is the first place you should look for errors in your training pipeline? ### [](#5.-what-is-the-best-way-to-debug-a-cuda-error?)5\. What is the best way to debug a CUDA error? ### [](#6.-what-is-the-best-way-to-get-an-issue-on-github-fixed?)6\. What is the best way to get an issue on GitHub fixed? ### [](#7.-why-is-overfitting-to-one-batch-usually-a-good-debugging-technique?)7\. Why is overfitting to one batch usually a good debugging technique? ### [](#8.-why-is-it-a-good-idea-to-include-details-on-your-compute-environment-with-<code>transformers-cli-env</code>-when-creating-a-new-issue-in-the-🤗-transformers-repo?)8\. Why is it a good idea to include details on your compute environment with `transformers-cli env` when creating a new issue in the 🤗 Transformers repo?
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="End-of-chapter quiz - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter8/7"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>End-of-chapter quiz - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;end-of-chapter-quiz&quot;,&quot;title&quot;:&quot;End-of-chapter quiz&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter8/7&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;End-of-chapter quiz&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">End-of-chapter quiz</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/1?fw=pt">Introduction </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/2?fw=pt">What to do when you get an error </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/3?fw=pt">Asking for help on the forums </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/4?fw=pt">Debugging the training pipeline </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/5?fw=pt">How to write a good issue </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter8/6?fw=pt">Part 2 completed! </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter8/7?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="end-of-chapter-quiz" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#end-of-chapter-quiz"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>End-of-chapter quiz</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-8-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>Let’s test what you learned in this chapter!</p> <h3 class="relative group"><a id="1.-in-which-order-should-you-read-a-python-traceback?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#1.-in-which-order-should-you-read-a-python-traceback?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>1. In which order should you read a Python traceback?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> From top to bottom</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> From bottom to top</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="2.-what-is-a-minimal-reproducible-example?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#2.-what-is-a-minimal-reproducible-example?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>2. What is a minimal reproducible example?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> A simple implementation of a Transformer architecture from a research article</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> A compact and self-contained block of code that can be run without any external dependencies on private files or data</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> A screenshot of the Python traceback</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> A notebook that contains your whole analysis, including parts unrelated to the error</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="3.-suppose-you-try-to-run-the-following-code,-which-throws-an-error:" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#3.-suppose-you-try-to-run-the-following-code,-which-throws-an-error:"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>3. Suppose you try to run the following code, which throws an error:</span></h3> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> GPT3ForSequenceClassification <span class="hljs-comment"># ImportError: cannot import name 'GPT3ForSequenceClassification' from 'transformers' (/Users/lewtun/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/__init__.py)</span> <span class="hljs-comment"># ---------------------------------------------------------------------------</span> <span class="hljs-comment"># ImportError Traceback (most recent call last)</span> <span class="hljs-comment"># /var/folders/28/k4cy5q7s2hs92xq7_h89_vgm0000gn/T/ipykernel_30848/333858878.py in &lt;module&gt;</span> <span class="hljs-comment"># ----&gt; 1 from transformers import GPT3ForSequenceClassification</span> <span class="hljs-comment"># ImportError: cannot import name 'GPT3ForSequenceClassification' from 'transformers' (/Users/lewtun/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/__init__.py)</span></pre></div> <p>Which of the following might be a good choice for the title of a forum topic to ask for help?</p> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> <code>ImportError: cannot import name 'GPT3ForSequenceClassification' from 'transformers' (/Users/lewtun/miniconda3/envs/huggingface/lib/python3.8/site-packages/transformers/__init__.py)</code></label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Problem with <code>from transformers import GPT3ForSequenceClassification</code></label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Why can't I import <code>GPT3ForSequenceClassification</code>?</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Is GPT-3 supported in 🤗 Transformers?</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="4.-suppose-you’ve-tried-to-run-<code>trainer.train()</code>-and-are-faced-with-a-cryptic-error-that-doesn’t-tell-you-exactly-where-the-error-is-coming-from.-which-of-the-following-is-the-first-place-you-should-look-for-errors-in-your-training-pipeline?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#4.-suppose-you’ve-tried-to-run-<code>trainer.train()</code>-and-are-faced-with-a-cryptic-error-that-doesn’t-tell-you-exactly-where-the-error-is-coming-from.-which-of-the-following-is-the-first-place-you-should-look-for-errors-in-your-training-pipeline?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>4. Suppose you’ve tried to run <code>trainer.train()</code> and are faced with a cryptic error that doesn’t tell you exactly where the error is coming from. Which of the following is the first place you should look for errors in your training pipeline?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> The optimization step where we compute gradients and perform backpropagation</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> The evaluation step where we compute metrics</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> The datasets</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> The dataloaders</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="5.-what-is-the-best-way-to-debug-a-cuda-error?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#5.-what-is-the-best-way-to-debug-a-cuda-error?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>5. What is the best way to debug a CUDA error?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Post the error message on the forums or GitHub.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Execute the same code on the CPU.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Read the traceback to find out what caused the error.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Reduce the batch size.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="4"> Restart the Jupyter kernel.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="6.-what-is-the-best-way-to-get-an-issue-on-github-fixed?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#6.-what-is-the-best-way-to-get-an-issue-on-github-fixed?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>6. What is the best way to get an issue on GitHub fixed?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Post a full reproducible example of the bug.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Ask every day for an update.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Inspect the source code around the bug and try to find the reason why it happens. Post the results in the issue.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="7.-why-is-overfitting-to-one-batch-usually-a-good-debugging-technique?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#7.-why-is-overfitting-to-one-batch-usually-a-good-debugging-technique?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>7. Why is overfitting to one batch usually a good debugging technique?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It isn't; overfitting is always bad and should be avoided.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It allows us to verify that the model is able to reduce the loss to zero.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It allows us to verify that the tensor shapes of our inputs and labels are correct.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="8.-why-is-it-a-good-idea-to-include-details-on-your-compute-environment-with-<code>transformers-cli-env</code>-when-creating-a-new-issue-in-the-🤗-transformers-repo?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#8.-why-is-it-a-good-idea-to-include-details-on-your-compute-environment-with-<code>transformers-cli-env</code>-when-creating-a-new-issue-in-the-🤗-transformers-repo?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>8. Why is it a good idea to include details on your compute environment with <code>transformers-cli env</code> when creating a new issue in the 🤗 Transformers repo?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> It allows the maintainers to understand which version of the library you're using.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> It allows the maintainers to know whether you're running code on Windows, macOS, or Linux.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> It allows the maintainers to know whether you're running code on a GPU or CPU.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter8/6?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Part 2 completed!</a> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isLoggedIn&quot;:false,&quot;nextChapterUrl&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;,&quot;quizChapter&quot;:8}" data-target="CourseCompleteButton"><div class="relative ml-auto"> <button class="btn btn-lg" disabled=""><svg class="mr-2" width="1em" height="1em" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0 16C0 7.16344 7.16344 0 16 0V0C24.8366 0 32 7.16344 32 16V16C32 24.8366 24.8366 32 16 32V32C7.16344 32 0 24.8366 0 16V16Z" fill="#D9FFD2"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M23.5607 10.9393C24.1464 11.5251 24.1464 12.4749 23.5607 13.0607L14.5 22.1213L8.93934 16.5607C8.35355 15.9749 8.35355 15.0251 8.93934 14.4393C9.52513 13.8536 10.4749 13.8536 11.0607 14.4393L14.5 17.8787L21.4393 10.9393C22.0251 10.3536 22.9749 10.3536 23.5607 10.9393Z" fill="#188B3F"></path></svg> Complete Chapter</button></div></div> <a href="/learn/nlp-course/chapter9/1?fw=pt" class="hidden">Next chapter</a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;id&quot;:&quot;end-of-chapter-quiz&quot;,&quot;url&quot;:&quot;#end-of-chapter-quiz&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#end-of-chapter-quiz" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-end-of-chapter-quiz"><wbr>End-of-chapter quiz</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter8/7" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter8/7"); } </script> <iframe name="__privateStripeMetricsController3550" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter8%2F7%3Ffw%3Dpt&amp;title=End-of-chapter%20quiz%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:34.509Z
Introduction to Gradio - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter9/1?fw=pt
## [](#introduction-to-gradio)Introduction to Gradio [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-9-questions) In this chapter we will be learning about how to build **interactive demos** for your machine learning models. Why build a demo or a GUI for your machine learning model in the first place? Demos allow: - **Machine learning developers** to easily present their work to a wide audience including non-technical teams or customers - **Researchers** to more easily reproduce machine learning models and behavior - **Quality testers** or **end users** to more easily identify and debug failure points of models - **Diverse users** to discover algorithmic biases in models We’ll be using the Gradio library to build demos for our models. Gradio allows you to build, customize, and share web-based demos for any machine learning model, entirely in Python. Here are some examples of machine learning demos built with Gradio: - A **sketch recognition** model that takes in a sketch and outputs labels of what it thinks is being drawn: - An extractive **question answering** model that takes in a context paragraph and a quest and outputs a response and a probability score (we discussed this kind of model [in Chapter 7](/course/chapter7/7)): - A **background removal** model that takes in an image and outputs the image with the background removed: This chapter is broken down into sections which include both _concepts_ and _applications_. After you learn the concept in each section, you’ll apply it to build a particular kind of demo, ranging from image classification to speech recognition. By the time you finish this chapter, you’ll be able to build these demos (and many more!) in just a few lines of Python code. 👀 Check out [Hugging Face Spaces](https://huggingface.co/spaces) to see many recent examples of machine learning demos built by the machine learning community!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Introduction to Gradio - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter9/1"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Introduction to Gradio - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;introduction-to-gradio&quot;,&quot;title&quot;:&quot;Introduction to Gradio&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter9/1&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Introduction to Gradio&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Introduction to Gradio</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter9/1?fw=pt">Introduction to Gradio </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/2?fw=pt">Building your first demo </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/3?fw=pt">Understanding the Interface class </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/4?fw=pt">Sharing demos with others </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/5?fw=pt">Integrations with the Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/6?fw=pt">Advanced Interface features </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/7?fw=pt">Introduction to Blocks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/8?fw=pt">Gradio, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="introduction-to-gradio" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#introduction-to-gradio"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Introduction to Gradio</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-9-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>In this chapter we will be learning about how to build <strong>interactive demos</strong> for your machine learning models.</p> <p>Why build a demo or a GUI for your machine learning model in the first place? Demos allow:</p> <ul><li><strong>Machine learning developers</strong> to easily present their work to a wide audience including non-technical teams or customers</li> <li><strong>Researchers</strong> to more easily reproduce machine learning models and behavior</li> <li><strong>Quality testers</strong> or <strong>end users</strong> to more easily identify and debug failure points of models</li> <li><strong>Diverse users</strong> to discover algorithmic biases in models</li></ul> <p>We’ll be using the Gradio library to build demos for our models. Gradio allows you to build, customize, and share web-based demos for any machine learning model, entirely in Python.</p> <p>Here are some examples of machine learning demos built with Gradio:</p> <ul><li>A <strong>sketch recognition</strong> model that takes in a sketch and outputs labels of what it thinks is being drawn:</li></ul> <iframe src="https://course-demos-draw2.hf.space" frameborder="0" height="450" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <ul><li>An extractive <strong>question answering</strong> model that takes in a context paragraph and a quest and outputs a response and a probability score (we discussed this kind of model <a href="/course/chapter7/7">in Chapter 7</a>):</li></ul> <iframe src="https://course-demos-question-answering-simple.hf.space" frameborder="0" height="640" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <ul><li>A <strong>background removal</strong> model that takes in an image and outputs the image with the background removed:</li></ul> <iframe src="https://course-demos-remove-bg-original.hf.space" frameborder="0" height="640" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>This chapter is broken down into sections which include both <em>concepts</em> and <em>applications</em>. After you learn the concept in each section, you’ll apply it to build a particular kind of demo, ranging from image classification to speech recognition. By the time you finish this chapter, you’ll be able to build these demos (and many more!) in just a few lines of Python code.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400">👀 Check out <a href="https://huggingface.co/spaces" target="_blank">Hugging Face Spaces</a> to see many recent examples of machine learning demos built by the machine learning community!</div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter8/7?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>End-of-chapter quiz</a> <a href="/learn/nlp-course/chapter9/2?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Building your first demo<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;introduction-to-gradio&quot;,&quot;url&quot;:&quot;#introduction-to-gradio&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#introduction-to-gradio" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-introduction-to-gradio"><wbr>Introduction to <wbr>Gradio</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter9/1" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter9/1"); } </script> <iframe name="__privateStripeMetricsController2580" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter9%2F1%3Ffw%3Dpt&amp;title=Introduction%20to%20Gradio%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:36.312Z
Understanding the Interface class - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter9/3?fw=pt
## [](#understanding-the-interface-class)Understanding the Interface class [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-9-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section3.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section3.ipynb) In this section, we will take a closer look at the `Interface` class, and understand the main parameters used to create one. ## [](#how-to-create-an-interface)How to create an Interface You’ll notice that the `Interface` class has 3 required parameters: `Interface(fn, inputs, outputs, ...)` These parameters are: - `fn`: the prediction function that is wrapped by the Gradio interface. This function can take one or more parameters and return one or more values - `inputs`: the input component type(s). Gradio provides many pre-built components such as`"image"` or `"mic"`. - `outputs`: the output component type(s). Again, Gradio provides many pre-built components e.g. `"image"` or `"label"`. For a complete list of components, [see the Gradio docs](https://gradio.app/docs) . Each pre-built component can be customized by instantiating the class corresponding to the component. For example, as we saw in the [previous section](/course/chapter9/2), instead of passing in `"textbox"` to the `inputs` parameter, you can pass in a `Textbox(lines=7, label="Prompt")` component to create a textbox with 7 lines and a label. Let’s take a look at another example, this time with an `Audio` component. ## [](#a-simple-example-with-audio)A simple example with audio As mentioned earlier, Gradio provides many different inputs and outputs. So let’s build an `Interface` that works with audio. In this example, we’ll build an audio-to-audio function that takes an audio file and simply reverses it. We will use for the input the `Audio` component. When using the `Audio` component, you can specify whether you want the `source` of the audio to be a file that the user uploads or a microphone that the user records their voice with. In this case, let’s set it to a `"microphone"`. Just for fun, we’ll add a label to our `Audio` that says “Speak here…“. In addition, we’d like to receive the audio as a numpy array so that we can easily “reverse” it. So we’ll set the `"type"` to be `"numpy"`, which passes the input data as a tuple of (`sample_rate`, `data`) into our function. We will also use the `Audio` output component which can automatically render a tuple with a sample rate and numpy array of data as a playable audio file. In this case, we do not need to do any customization, so we will use the string shortcut `"audio"`. ``` import numpy as np import gradio as gr def reverse_audio(audio): sr, data = audio reversed_audio = (sr, np.flipud(data)) return reversed_audio mic = gr.Audio(source="microphone", type="numpy", label="Speak here...") gr.Interface(reverse_audio, mic, "audio").launch()``` The code above will produce an interface like the one below (if your browser doesn’t ask you for microphone permissions, [open the demo in a separate tab](https://huggingface.co/spaces/course-demos/audio-reverse).) You should now be able to record your voice and hear yourself speaking in reverse - spooky 👻! ## [](#handling-multiple-inputs-and-outputs)Handling multiple inputs and outputs Let’s say we had a more complicated function, with multiple inputs and outputs. In the example below, we have a function that takes a dropdown index, a slider value, and number, and returns an audio sample of a musical tone. Take a look how we pass a list of input and output components, and see if you can follow along what’s happening. The key here is that when you pass: - a list of input components, each component corresponds to a parameter in order. - a list of output coponents, each component corresponds to a returned value. The code snippet below shows how three input components line up with the three arguments of the `generate_tone()` function: ``` import numpy as np import gradio as gr notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"] def generate_tone(note, octave, duration): sr = 48000 a4_freq, tones_from_a4 = 440, 12 * (octave - 4) + (note - 9) frequency = a4_freq * 2 ** (tones_from_a4 / 12) duration = int(duration) audio = np.linspace(0, duration, duration * sr) audio = (20000 * np.sin(audio * (2 * np.pi * frequency))).astype(np.int16) return (sr, audio) gr.Interface( generate_tone, [ gr.Dropdown(notes, type="index"), gr.Slider(minimum=4, maximum=6, step=1), gr.Textbox(type="number", value=1, label="Duration in seconds"), ], "audio", ).launch()``` ### [](#the-launch-method)The `launch()` method So far, we have used the `launch()` method to launch the interface, but we haven’t really discussed what it does. By default, the `launch()` method will launch the demo in a web server that is running locally. If you are running your code in a Jupyter or Colab notebook, then Gradio will embed the demo GUI in the notebook so you can easily use it. You can customize the behavior of `launch()` through different parameters: - `inline` - whether to display the interface inline on Python notebooks. - `inbrowser` - whether to automatically launch the interface in a new tab on the default browser. - `share` - whether to create a publicly shareable link from your computer for the interface. Kind of like a Google Drive link! We’ll cover the `share` parameter in a lot more detail in the next section! ## [](#lets-apply-it)✏️ Let's apply it! Let’s build an interface that allows you to demo a **speech-recognition** model. To make it interesting, we will accept _either_ a mic input or an uploaded file. As usual, we’ll load our speech recognition model using the `pipeline()` function from 🤗 Transformers. If you need a quick refresher, you can go back to [that section in Chapter 1](/course/chapter1/3). Next, we’ll implement a `transcribe_audio()` function that processes the audio and returns the transcription. Finally, we’ll wrap this function in an `Interface` with the `Audio` components for the inputs and just text for the output. Altogether, the code for this application is the following: ``` from transformers import pipeline import gradio as gr model = pipeline("automatic-speech-recognition") def transcribe_audio(mic=None, file=None): if mic is not None: audio = mic elif file is not None: audio = file else: return "You must either provide a mic recording or a file" transcription = model(audio)["text"] return transcription gr.Interface( fn=transcribe_audio, inputs=[ gr.Audio(source="microphone", type="filepath", optional=True), gr.Audio(source="upload", type="filepath", optional=True), ], outputs="text", ).launch()``` If your browser doesn’t ask you for microphone permissions, [open the demo in a separate tab](https://huggingface.co/spaces/course-demos/audio-reverse). That’s it! You can now use this interface to transcribe audio. Notice here that by passing in the `optional` parameter as `True`, we allow the user to either provide a microphone or an audio file (or neither, but that will return an error message). Keep going to see how to share your interface with others!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Understanding the Interface class - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter9/3"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Understanding the Interface class - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;understanding-the-interface-class&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;how-to-create-an-interface&quot;,&quot;title&quot;:&quot;How to create an Interface&quot;},{&quot;local&quot;:&quot;a-simple-example-with-audio&quot;,&quot;title&quot;:&quot;A simple example with audio&quot;},{&quot;local&quot;:&quot;handling-multiple-inputs-and-outputs&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;the-launch-method&quot;,&quot;title&quot;:&quot;The `launch()` method&quot;}],&quot;title&quot;:&quot;Handling multiple inputs and outputs&quot;},{&quot;local&quot;:&quot;lets-apply-it&quot;,&quot;title&quot;:&quot;✏️ Let's apply it!&quot;}],&quot;title&quot;:&quot;Understanding the Interface class&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter9/3&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Understanding the Interface class&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Understanding the Interface class</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/1?fw=pt">Introduction to Gradio </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/2?fw=pt">Building your first demo </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter9/3?fw=pt">Understanding the Interface class </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/4?fw=pt">Sharing demos with others </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/5?fw=pt">Integrations with the Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/6?fw=pt">Advanced Interface features </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/7?fw=pt">Introduction to Blocks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/8?fw=pt">Gradio, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="understanding-the-interface-class" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#understanding-the-interface-class"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Understanding the Interface class</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-9-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section3.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section3.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>In this section, we will take a closer look at the <code>Interface</code> class, and understand the main parameters used to create one.</p> <h2 class="relative group"><a id="how-to-create-an-interface" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#how-to-create-an-interface"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>How to create an Interface</span></h2> <p>You’ll notice that the <code>Interface</code> class has 3 required parameters:</p> <p><code>Interface(fn, inputs, outputs, ...)</code></p> <p>These parameters are:</p> <ul><li><code>fn</code>: the prediction function that is wrapped by the Gradio interface. This function can take one or more parameters and return one or more values</li> <li><code>inputs</code>: the input component type(s). Gradio provides many pre-built components such as<code>"image"</code> or <code>"mic"</code>.</li> <li><code>outputs</code>: the output component type(s). Again, Gradio provides many pre-built components e.g. <code>"image"</code> or <code>"label"</code>.</li></ul> <p>For a complete list of components, <a href="https://gradio.app/docs" rel="nofollow">see the Gradio docs </a>. Each pre-built component can be customized by instantiating the class corresponding to the component.</p> <p>For example, as we saw in the <a href="/course/chapter9/2">previous section</a>, instead of passing in <code>"textbox"</code> to the <code>inputs</code> parameter, you can pass in a <code>Textbox(lines=7, label="Prompt")</code> component to create a textbox with 7 lines and a label.</p> <p>Let’s take a look at another example, this time with an <code>Audio</code> component.</p> <h2 class="relative group"><a id="a-simple-example-with-audio" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#a-simple-example-with-audio"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>A simple example with audio</span></h2> <p>As mentioned earlier, Gradio provides many different inputs and outputs. So let’s build an <code>Interface</code> that works with audio.</p> <p>In this example, we’ll build an audio-to-audio function that takes an audio file and simply reverses it.</p> <p>We will use for the input the <code>Audio</code> component. When using the <code>Audio</code> component, you can specify whether you want the <code>source</code> of the audio to be a file that the user uploads or a microphone that the user records their voice with. In this case, let’s set it to a <code>"microphone"</code>. Just for fun, we’ll add a label to our <code>Audio</code> that says “Speak here…“.</p> <p>In addition, we’d like to receive the audio as a numpy array so that we can easily “reverse” it. So we’ll set the <code>"type"</code> to be <code>"numpy"</code>, which passes the input data as a tuple of (<code>sample_rate</code>, <code>data</code>) into our function.</p> <p>We will also use the <code>Audio</code> output component which can automatically render a tuple with a sample rate and numpy array of data as a playable audio file. In this case, we do not need to do any customization, so we will use the string shortcut <code>"audio"</code>.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr <span class="hljs-keyword">def</span> <span class="hljs-title function_">reverse_audio</span>(<span class="hljs-params">audio</span>): sr, data = audio reversed_audio = (sr, np.flipud(data)) <span class="hljs-keyword">return</span> reversed_audio mic = gr.Audio(source=<span class="hljs-string">"microphone"</span>, <span class="hljs-built_in">type</span>=<span class="hljs-string">"numpy"</span>, label=<span class="hljs-string">"Speak here..."</span>) gr.Interface(reverse_audio, mic, <span class="hljs-string">"audio"</span>).launch()</pre></div> <p>The code above will produce an interface like the one below (if your browser doesn’t ask you for microphone permissions, <a href="https://huggingface.co/spaces/course-demos/audio-reverse" target="_blank">open the demo in a separate tab</a>.)</p> <iframe src="https://course-demos-audio-reverse.hf.space" frameborder="0" height="250" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>You should now be able to record your voice and hear yourself speaking in reverse - spooky 👻!</p> <h2 class="relative group"><a id="handling-multiple-inputs-and-outputs" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#handling-multiple-inputs-and-outputs"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Handling multiple inputs and outputs</span></h2> <p>Let’s say we had a more complicated function, with multiple inputs and outputs. In the example below, we have a function that takes a dropdown index, a slider value, and number, and returns an audio sample of a musical tone.</p> <p>Take a look how we pass a list of input and output components, and see if you can follow along what’s happening.</p> <p>The key here is that when you pass:</p> <ul><li>a list of input components, each component corresponds to a parameter in order.</li> <li>a list of output coponents, each component corresponds to a returned value.</li></ul> <p>The code snippet below shows how three input components line up with the three arguments of the <code>generate_tone()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr notes = [<span class="hljs-string">"C"</span>, <span class="hljs-string">"C#"</span>, <span class="hljs-string">"D"</span>, <span class="hljs-string">"D#"</span>, <span class="hljs-string">"E"</span>, <span class="hljs-string">"F"</span>, <span class="hljs-string">"F#"</span>, <span class="hljs-string">"G"</span>, <span class="hljs-string">"G#"</span>, <span class="hljs-string">"A"</span>, <span class="hljs-string">"A#"</span>, <span class="hljs-string">"B"</span>] <span class="hljs-keyword">def</span> <span class="hljs-title function_">generate_tone</span>(<span class="hljs-params">note, octave, duration</span>): sr = <span class="hljs-number">48000</span> a4_freq, tones_from_a4 = <span class="hljs-number">440</span>, <span class="hljs-number">12</span> * (octave - <span class="hljs-number">4</span>) + (note - <span class="hljs-number">9</span>) frequency = a4_freq * <span class="hljs-number">2</span> ** (tones_from_a4 / <span class="hljs-number">12</span>) duration = <span class="hljs-built_in">int</span>(duration) audio = np.linspace(<span class="hljs-number">0</span>, duration, duration * sr) audio = (<span class="hljs-number">20000</span> * np.sin(audio * (<span class="hljs-number">2</span> * np.pi * frequency))).astype(np.int16) <span class="hljs-keyword">return</span> (sr, audio) gr.Interface( generate_tone, [ gr.Dropdown(notes, <span class="hljs-built_in">type</span>=<span class="hljs-string">"index"</span>), gr.Slider(minimum=<span class="hljs-number">4</span>, maximum=<span class="hljs-number">6</span>, step=<span class="hljs-number">1</span>), gr.Textbox(<span class="hljs-built_in">type</span>=<span class="hljs-string">"number"</span>, value=<span class="hljs-number">1</span>, label=<span class="hljs-string">"Duration in seconds"</span>), ], <span class="hljs-string">"audio"</span>, ).launch()</pre></div> <iframe src="https://course-demos-generate-tone.hf.space" frameborder="0" height="450" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <h3 class="relative group"><a id="the-launch-method" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#the-launch-method"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>The <code>launch()</code> method</span></h3> <p>So far, we have used the <code>launch()</code> method to launch the interface, but we haven’t really discussed what it does.</p> <p>By default, the <code>launch()</code> method will launch the demo in a web server that is running locally. If you are running your code in a Jupyter or Colab notebook, then Gradio will embed the demo GUI in the notebook so you can easily use it.</p> <p>You can customize the behavior of <code>launch()</code> through different parameters:</p> <ul><li><code>inline</code> - whether to display the interface inline on Python notebooks.</li> <li><code>inbrowser</code> - whether to automatically launch the interface in a new tab on the default browser.</li> <li><code>share</code> - whether to create a publicly shareable link from your computer for the interface. Kind of like a Google Drive link!</li></ul> <p>We’ll cover the <code>share</code> parameter in a lot more detail in the next section!</p> <h2 class="relative group"><a id="lets-apply-it" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#lets-apply-it"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>✏️ Let's apply it!</span></h2> <p>Let’s build an interface that allows you to demo a <strong>speech-recognition</strong> model. To make it interesting, we will accept <em>either</em> a mic input or an uploaded file.</p> <p>As usual, we’ll load our speech recognition model using the <code>pipeline()</code> function from 🤗 Transformers. If you need a quick refresher, you can go back to <a href="/course/chapter1/3">that section in Chapter 1</a>. Next, we’ll implement a <code>transcribe_audio()</code> function that processes the audio and returns the transcription. Finally, we’ll wrap this function in an <code>Interface</code> with the <code>Audio</code> components for the inputs and just text for the output. Altogether, the code for this application is the following:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline <span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr model = pipeline(<span class="hljs-string">"automatic-speech-recognition"</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">transcribe_audio</span>(<span class="hljs-params">mic=<span class="hljs-literal">None</span>, file=<span class="hljs-literal">None</span></span>): <span class="hljs-keyword">if</span> mic <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>: audio = mic <span class="hljs-keyword">elif</span> file <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>: audio = file <span class="hljs-keyword">else</span>: <span class="hljs-keyword">return</span> <span class="hljs-string">"You must either provide a mic recording or a file"</span> transcription = model(audio)[<span class="hljs-string">"text"</span>] <span class="hljs-keyword">return</span> transcription gr.Interface( fn=transcribe_audio, inputs=[ gr.Audio(source=<span class="hljs-string">"microphone"</span>, <span class="hljs-built_in">type</span>=<span class="hljs-string">"filepath"</span>, optional=<span class="hljs-literal">True</span>), gr.Audio(source=<span class="hljs-string">"upload"</span>, <span class="hljs-built_in">type</span>=<span class="hljs-string">"filepath"</span>, optional=<span class="hljs-literal">True</span>), ], outputs=<span class="hljs-string">"text"</span>, ).launch()</pre></div> <p>If your browser doesn’t ask you for microphone permissions, <a href="https://huggingface.co/spaces/course-demos/audio-reverse" target="_blank">open the demo in a separate tab</a>.</p> <iframe src="https://course-demos-asr.hf.space" frameborder="0" height="550" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>That’s it! You can now use this interface to transcribe audio. Notice here that by passing in the <code>optional</code> parameter as <code>True</code>, we allow the user to either provide a microphone or an audio file (or neither, but that will return an error message).</p> <p>Keep going to see how to share your interface with others!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter9/2?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Building your first demo</a> <a href="/learn/nlp-course/chapter9/4?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Sharing demos with others<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;understanding-the-interface-class&quot;,&quot;url&quot;:&quot;#understanding-the-interface-class&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to create an Interface&quot;,&quot;id&quot;:&quot;how-to-create-an-interface&quot;,&quot;url&quot;:&quot;#how-to-create-an-interface&quot;},{&quot;title&quot;:&quot;A simple example with audio&quot;,&quot;id&quot;:&quot;a-simple-example-with-audio&quot;,&quot;url&quot;:&quot;#a-simple-example-with-audio&quot;},{&quot;title&quot;:&quot;Handling multiple inputs and outputs&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;handling-multiple-inputs-and-outputs&quot;,&quot;url&quot;:&quot;#handling-multiple-inputs-and-outputs&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;The `launch()` method&quot;,&quot;id&quot;:&quot;the-launch-method&quot;,&quot;url&quot;:&quot;#the-launch-method&quot;}]},{&quot;title&quot;:&quot;✏️ Let's apply it!&quot;,&quot;id&quot;:&quot;lets-apply-it&quot;,&quot;url&quot;:&quot;#lets-apply-it&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#understanding-the-interface-class" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-understanding-the-interface-class"><wbr>Understanding the <wbr>Interface class</a> <a href="#how-to-create-an-interface" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-how-to-create-an-interface"><wbr>How to create an <wbr>Interface</a> <a href="#a-simple-example-with-audio" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-a-simple-example-with-audio"><wbr>A simple example with audio</a> <a href="#handling-multiple-inputs-and-outputs" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-handling-multiple-inputs-and-outputs"><wbr>Handling multiple inputs and outputs</a> <a href="#the-launch-method" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-the-launch-method"><wbr>The `launch()` method</a> <a href="#lets-apply-it" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-lets-apply-it">✏️ <wbr>Let's apply it!</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter9/3" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter9/3"); } </script> <iframe name="__privateStripeMetricsController3630" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter9%2F3%3Ffw%3Dpt&amp;title=Understanding%20the%20Interface%20class%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:37.994Z
Sharing demos with others - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter9/4?fw=pt
## [](#sharing-demos-with-others)Sharing demos with others [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-9-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section4.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section4.ipynb) Now that you’ve built a demo, you’ll probably want to share it with others. Gradio demos can be shared in two ways: using a **_temporary share link_** or **_permanent hosting on Spaces_**. We’ll cover both of these approaches shortly. But before you share your demo, you may want to polish it up 💅. ### [](#polishing-your-gradio-demo)Polishing your Gradio demo: ![Overview of a gradio interface](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter9/gradio-demo-overview.png) ![Overview of a gradio interface](https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter9/gradio-demo-overview-dark.png) To add additional content to your demo, the `Interface` class supports some optional parameters: - `title`: you can give a title to your demo, which appears _above_ the input and output components. - `description`: you can give a description (in text, Markdown, or HTML) for the interface, which appears above the input and output components and below the title. - `article`: you can also write an expanded article (in text, Markdown, or HTML) explaining the interface. If provided, it appears _below_ the input and output components. - `theme`: don’t like the default colors? Set the theme to use one of `default`, `huggingface`, `grass`, `peach`. You can also add the `dark-` prefix, e.g. `dark-peach` for dark theme (or just `dark` for the default dark theme). - `examples`: to make your demo _way easier to use_, you can provide some example inputs for the function. These appear below the UI components and can be used to populate the interface. These should be provided as a nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component. - `live`: if you want to make your demo “live”, meaning that your model reruns every time the input changes, you can set `live=True`. This makes sense to use with quick models (we’ll see an example at the end of this section) Using the options above, we end up with a more complete interface. Run the code below so you can chat with Rick and Morty: ``` title = "Ask Rick a Question" description = """ The bot was trained to answer questions based on Rick and Morty dialogues. Ask Rick anything! <img src="https://huggingface.co/spaces/course-demos/Rick_and_Morty_QA/resolve/main/rick.png" width=200px> """ article = "Check out [the original Rick and Morty Bot](https://huggingface.co/spaces/kingabzpro/Rick_and_Morty_Bot) that this demo is based off of." gr.Interface( fn=predict, inputs="textbox", outputs="text", title=title, description=description, article=article, examples=[["What are you doing?"], ["Where should we time travel to?"]], ).launch()``` Using the options above, we end up with a more complete interface. Try the interface below: ### [](#sharing-your-demo-with-temporary-links)Sharing your demo with temporary links Now that we have a working demo of our machine learning model, let's learn how to easily share a link to our interface. Interfaces can be easily shared publicly by setting \`share=True\` in the \`launch()\` method: ``` gr.Interface(classify_image, "image", "label").launch(share=True)``` This generates a public, shareable link that you can send to anybody! When you send this link, the user on the other side can try out the model in their browser for up to 72 hours. Because the processing happens on your device (as long as your device stays on!), you don’t have to worry about packaging any dependencies. If you’re working out of a Google Colab notebook, a share link is always automatically created. It usually looks something like this: **XXXXX.gradio.app**. Although the link is served through a Gradio link, we are only a proxy for your local server, and do not store any data sent through the interfaces. Keep in mind, however, that these links are publicly accessible, meaning that anyone can use your model for prediction! Therefore, make sure not to expose any sensitive information through the functions you write, or allow any critical changes to occur on your device. If you set `share=False` (the default), only a local link is created. ### [](#hosting-your-demo-on-hugging-face-spaces)Hosting your demo on Hugging Face Spaces A share link that you can pass around to collegues is cool, but how can you permanently host your demo and have it exist in its own “space” on the internet? Hugging Face Spaces provides the infrastructure to permanently host your Gradio model on the internet, **for free**! Spaces allows you to create and push to a (public or private) repo, where your Gradio interface code will exist in an `app.py` file. [Read a step-by-step tutorial](https://huggingface.co/blog/gradio-spaces) to get started, or watch an example video below. ## [](#lets-apply-it)✏️ Let's apply it! Using what we just learned in the sections so far, let’s create the sketch recognition demo we saw in [section one of this chapter](/course/chapter9/1). Let’s add some customization to our interface and set `share=True` to create a public link we can pass around. We can load the labels from [class\_names.txt](https://huggingface.co/spaces/dawood/Sketch-Recognition/blob/main/class_names.txt) and load the pre-trained pytorch model from [pytorch\_model.bin](https://huggingface.co/spaces/dawood/Sketch-Recognition/blob/main/pytorch_model.bin). Download these files by following the link and clicking download on the top left corner of the file preview. Let’s take a look at the code below to see how we use these files to load our model and create a `predict()` function: ``` from pathlib import Path import torch import gradio as gr from torch import nn LABELS = Path("class_names.txt").read_text().splitlines() model = nn.Sequential( nn.Conv2d(1, 32, 3, padding="same"), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, padding="same"), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, padding="same"), nn.ReLU(), nn.MaxPool2d(2), nn.Flatten(), nn.Linear(1152, 256), nn.ReLU(), nn.Linear(256, len(LABELS)), ) state_dict = torch.load("pytorch_model.bin", map_location="cpu") model.load_state_dict(state_dict, strict=False) model.eval() def predict(im): x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.0 with torch.no_grad(): out = model(x) probabilities = torch.nn.functional.softmax(out[0], dim=0) values, indices = torch.topk(probabilities, 5) return {LABELS[i]: v.item() for i, v in zip(indices, values)}``` Now that we have a `predict()` function. The next step is to define and launch our gradio interface: ``` interface = gr.Interface( predict, inputs="sketchpad", outputs="label", theme="huggingface", title="Sketch Recognition", description="Who wants to play Pictionary? Draw a common object like a shovel or a laptop, and the algorithm will guess in real time!", article="<p style='text-align: center'>Sketch Recognition | Demo Model</p>", live=True, ) interface.launch(share=True)``` Notice the `live=True` parameter in `Interface`, which means that the sketch demo makes a prediction every time someone draws on the sketchpad (no submit button!). Furthermore, we also set the `share=True` argument in the `launch()` method. This will create a public link that you can send to anyone! When you send this link, the user on the other side can try out the sketch recognition model. To reiterate, you could also host the model on Hugging Face Spaces, which is how we are able to embed the demo above. Next up, we’ll cover other ways that Gradio can be used with the Hugging Face ecosystem!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Sharing demos with others - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter9/4"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Sharing demos with others - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;sharing-demos-with-others&quot;,&quot;sections&quot;:[{&quot;local&quot;:null,&quot;sections&quot;:[{&quot;local&quot;:&quot;polishing-your-gradio-demo&quot;,&quot;title&quot;:&quot;Polishing your Gradio demo:&quot;},{&quot;local&quot;:&quot;sharing-your-demo-with-temporary-links&quot;,&quot;title&quot;:&quot;Sharing your demo with temporary links&quot;},{&quot;local&quot;:&quot;hosting-your-demo-on-hugging-face-spaces&quot;,&quot;title&quot;:&quot;Hosting your demo on Hugging Face Spaces&quot;}],&quot;title&quot;:null},{&quot;local&quot;:&quot;lets-apply-it&quot;,&quot;title&quot;:&quot;✏️ Let's apply it!&quot;}],&quot;title&quot;:&quot;Sharing demos with others&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter9/4&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Sharing demos with others&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Sharing demos with others</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/1?fw=pt">Introduction to Gradio </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/2?fw=pt">Building your first demo </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/3?fw=pt">Understanding the Interface class </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter9/4?fw=pt">Sharing demos with others </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/5?fw=pt">Integrations with the Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/6?fw=pt">Advanced Interface features </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/7?fw=pt">Introduction to Blocks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/8?fw=pt">Gradio, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="sharing-demos-with-others" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#sharing-demos-with-others"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Sharing demos with others</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-9-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section4.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section4.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>Now that you’ve built a demo, you’ll probably want to share it with others. Gradio demos can be shared in two ways: using a <strong><em>temporary share link</em></strong> or <strong><em>permanent hosting on Spaces</em></strong>.</p> <p>We’ll cover both of these approaches shortly. But before you share your demo, you may want to polish it up 💅.</p> <h3 class="relative group"><a id="polishing-your-gradio-demo" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#polishing-your-gradio-demo"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Polishing your Gradio demo:</span></h3> <div class="flex justify-center"><img class="block dark:hidden" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter9/gradio-demo-overview.png" alt="Overview of a gradio interface"> <img class="hidden dark:block" src="https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/en/chapter9/gradio-demo-overview-dark.png" alt="Overview of a gradio interface"></div> <p>To add additional content to your demo, the <code>Interface</code> class supports some optional parameters:</p> <ul><li><code>title</code>: you can give a title to your demo, which appears <em>above</em> the input and output components.</li> <li><code>description</code>: you can give a description (in text, Markdown, or HTML) for the interface, which appears above the input and output components and below the title.</li> <li><code>article</code>: you can also write an expanded article (in text, Markdown, or HTML) explaining the interface. If provided, it appears <em>below</em> the input and output components.</li> <li><code>theme</code>: don’t like the default colors? Set the theme to use one of <code>default</code>, <code>huggingface</code>, <code>grass</code>, <code>peach</code>. You can also add the <code>dark-</code> prefix, e.g. <code>dark-peach</code> for dark theme (or just <code>dark</code> for the default dark theme).</li> <li><code>examples</code>: to make your demo <em>way easier to use</em>, you can provide some example inputs for the function. These appear below the UI components and can be used to populate the interface. These should be provided as a nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component.</li> <li><code>live</code>: if you want to make your demo “live”, meaning that your model reruns every time the input changes, you can set <code>live=True</code>. This makes sense to use with quick models (we’ll see an example at the end of this section) Using the options above, we end up with a more complete interface. Run the code below so you can chat with Rick and Morty:</li></ul> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>title = <span class="hljs-string">"Ask Rick a Question"</span> description = <span class="hljs-string">""" The bot was trained to answer questions based on Rick and Morty dialogues. Ask Rick anything! &lt;img src="https://huggingface.co/spaces/course-demos/Rick_and_Morty_QA/resolve/main/rick.png" width=200px&gt; """</span> article = <span class="hljs-string">"Check out [the original Rick and Morty Bot](https://huggingface.co/spaces/kingabzpro/Rick_and_Morty_Bot) that this demo is based off of."</span> gr.Interface( fn=predict, inputs=<span class="hljs-string">"textbox"</span>, outputs=<span class="hljs-string">"text"</span>, title=title, description=description, article=article, examples=[[<span class="hljs-string">"What are you doing?"</span>], [<span class="hljs-string">"Where should we time travel to?"</span>]], ).launch()</pre></div> <p>Using the options above, we end up with a more complete interface. Try the interface below:</p> <iframe src="https://course-demos-Rick-and-Morty-QA.hf.space" frameborder="0" height="800" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <h3 class="relative group"><a id="sharing-your-demo-with-temporary-links" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#sharing-your-demo-with-temporary-links"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Sharing your demo with temporary links</span></h3> Now that we have a working demo of our machine learning model, let's learn how to easily share a link to our interface. Interfaces can be easily shared publicly by setting `share=True` in the `launch()` method: <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>gr.Interface(classify_image, <span class="hljs-string">"image"</span>, <span class="hljs-string">"label"</span>).launch(share=<span class="hljs-literal">True</span>)</pre></div> <p>This generates a public, shareable link that you can send to anybody! When you send this link, the user on the other side can try out the model in their browser for up to 72 hours. Because the processing happens on your device (as long as your device stays on!), you don’t have to worry about packaging any dependencies. If you’re working out of a Google Colab notebook, a share link is always automatically created. It usually looks something like this: <strong>XXXXX.gradio.app</strong>. Although the link is served through a Gradio link, we are only a proxy for your local server, and do not store any data sent through the interfaces.</p> <p>Keep in mind, however, that these links are publicly accessible, meaning that anyone can use your model for prediction! Therefore, make sure not to expose any sensitive information through the functions you write, or allow any critical changes to occur on your device. If you set <code>share=False</code> (the default), only a local link is created.</p> <h3 class="relative group"><a id="hosting-your-demo-on-hugging-face-spaces" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#hosting-your-demo-on-hugging-face-spaces"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Hosting your demo on Hugging Face Spaces</span></h3> <p>A share link that you can pass around to collegues is cool, but how can you permanently host your demo and have it exist in its own “space” on the internet?</p> <p>Hugging Face Spaces provides the infrastructure to permanently host your Gradio model on the internet, <strong>for free</strong>! Spaces allows you to create and push to a (public or private) repo, where your Gradio interface code will exist in an <code>app.py</code> file. <a href="https://huggingface.co/blog/gradio-spaces" rel="nofollow">Read a step-by-step tutorial</a> to get started, or watch an example video below.</p> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/LS9Y2wDVI0k" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <h2 class="relative group"><a id="lets-apply-it" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#lets-apply-it"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>✏️ Let's apply it!</span></h2> <p>Using what we just learned in the sections so far, let’s create the sketch recognition demo we saw in <a href="/course/chapter9/1">section one of this chapter</a>. Let’s add some customization to our interface and set <code>share=True</code> to create a public link we can pass around.</p> <p>We can load the labels from <a href="https://huggingface.co/spaces/dawood/Sketch-Recognition/blob/main/class_names.txt" rel="nofollow">class_names.txt</a> and load the pre-trained pytorch model from <a href="https://huggingface.co/spaces/dawood/Sketch-Recognition/blob/main/pytorch_model.bin" rel="nofollow">pytorch_model.bin</a>. Download these files by following the link and clicking download on the top left corner of the file preview. Let’s take a look at the code below to see how we use these files to load our model and create a <code>predict()</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path <span class="hljs-keyword">import</span> torch <span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr <span class="hljs-keyword">from</span> torch <span class="hljs-keyword">import</span> nn LABELS = Path(<span class="hljs-string">"class_names.txt"</span>).read_text().splitlines() model = nn.Sequential( nn.Conv2d(<span class="hljs-number">1</span>, <span class="hljs-number">32</span>, <span class="hljs-number">3</span>, padding=<span class="hljs-string">"same"</span>), nn.ReLU(), nn.MaxPool2d(<span class="hljs-number">2</span>), nn.Conv2d(<span class="hljs-number">32</span>, <span class="hljs-number">64</span>, <span class="hljs-number">3</span>, padding=<span class="hljs-string">"same"</span>), nn.ReLU(), nn.MaxPool2d(<span class="hljs-number">2</span>), nn.Conv2d(<span class="hljs-number">64</span>, <span class="hljs-number">128</span>, <span class="hljs-number">3</span>, padding=<span class="hljs-string">"same"</span>), nn.ReLU(), nn.MaxPool2d(<span class="hljs-number">2</span>), nn.Flatten(), nn.Linear(<span class="hljs-number">1152</span>, <span class="hljs-number">256</span>), nn.ReLU(), nn.Linear(<span class="hljs-number">256</span>, <span class="hljs-built_in">len</span>(LABELS)), ) state_dict = torch.load(<span class="hljs-string">"pytorch_model.bin"</span>, map_location=<span class="hljs-string">"cpu"</span>) model.load_state_dict(state_dict, strict=<span class="hljs-literal">False</span>) model.<span class="hljs-built_in">eval</span>() <span class="hljs-keyword">def</span> <span class="hljs-title function_">predict</span>(<span class="hljs-params">im</span>): x = torch.tensor(im, dtype=torch.float32).unsqueeze(<span class="hljs-number">0</span>).unsqueeze(<span class="hljs-number">0</span>) / <span class="hljs-number">255.0</span> <span class="hljs-keyword">with</span> torch.no_grad(): out = model(x) probabilities = torch.nn.functional.softmax(out[<span class="hljs-number">0</span>], dim=<span class="hljs-number">0</span>) values, indices = torch.topk(probabilities, <span class="hljs-number">5</span>) <span class="hljs-keyword">return</span> {LABELS[i]: v.item() <span class="hljs-keyword">for</span> i, v <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(indices, values)}</pre></div> <p>Now that we have a <code>predict()</code> function. The next step is to define and launch our gradio interface:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>interface = gr.Interface( predict, inputs=<span class="hljs-string">"sketchpad"</span>, outputs=<span class="hljs-string">"label"</span>, theme=<span class="hljs-string">"huggingface"</span>, title=<span class="hljs-string">"Sketch Recognition"</span>, description=<span class="hljs-string">"Who wants to play Pictionary? Draw a common object like a shovel or a laptop, and the algorithm will guess in real time!"</span>, article=<span class="hljs-string">"&lt;p style='text-align: center'&gt;Sketch Recognition | Demo Model&lt;/p&gt;"</span>, live=<span class="hljs-literal">True</span>, ) interface.launch(share=<span class="hljs-literal">True</span>)</pre></div> <iframe src="https://course-demos-Sketch-Recognition.hf.space" frameborder="0" height="650" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>Notice the <code>live=True</code> parameter in <code>Interface</code>, which means that the sketch demo makes a prediction every time someone draws on the sketchpad (no submit button!).</p> <p>Furthermore, we also set the <code>share=True</code> argument in the <code>launch()</code> method. This will create a public link that you can send to anyone! When you send this link, the user on the other side can try out the sketch recognition model. To reiterate, you could also host the model on Hugging Face Spaces, which is how we are able to embed the demo above.</p> <p>Next up, we’ll cover other ways that Gradio can be used with the Hugging Face ecosystem!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter9/3?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Understanding the Interface class</a> <a href="/learn/nlp-course/chapter9/5?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Integrations with the Hugging Face Hub<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;sharing-demos-with-others&quot;,&quot;url&quot;:&quot;#sharing-demos-with-others&quot;,&quot;sections&quot;:[{&quot;title&quot;:null,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Polishing your Gradio demo:&quot;,&quot;id&quot;:&quot;polishing-your-gradio-demo&quot;,&quot;url&quot;:&quot;#polishing-your-gradio-demo&quot;},{&quot;title&quot;:&quot;Sharing your demo with temporary links&quot;,&quot;id&quot;:&quot;sharing-your-demo-with-temporary-links&quot;,&quot;url&quot;:&quot;#sharing-your-demo-with-temporary-links&quot;},{&quot;title&quot;:&quot;Hosting your demo on Hugging Face Spaces&quot;,&quot;id&quot;:&quot;hosting-your-demo-on-hugging-face-spaces&quot;,&quot;url&quot;:&quot;#hosting-your-demo-on-hugging-face-spaces&quot;}]},{&quot;title&quot;:&quot;✏️ Let's apply it!&quot;,&quot;id&quot;:&quot;lets-apply-it&quot;,&quot;url&quot;:&quot;#lets-apply-it&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#sharing-demos-with-others" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-sharing-demos-with-others"><wbr>Sharing demos with others</a> <a href="#polishing-your-gradio-demo" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-polishing-your-gradio-demo"><wbr>Polishing your <wbr>Gradio demo:</a> <a href="#sharing-your-demo-with-temporary-links" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-sharing-your-demo-with-temporary-links"><wbr>Sharing your demo with temporary links</a> <a href="#hosting-your-demo-on-hugging-face-spaces" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-hosting-your-demo-on-hugging-face-spaces"><wbr>Hosting your demo on <wbr>Hugging <wbr>Face <wbr>Spaces</a> <a href="#lets-apply-it" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-lets-apply-it">✏️ <wbr>Let's apply it!</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter9/4" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter9/4"); } </script> <iframe name="__privateStripeMetricsController1770" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter9%2F4%3Ffw%3Dpt&amp;title=Sharing%20demos%20with%20others%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:38.118Z
Building your first demo - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter9/2?fw=pt
## [](#building-your-first-demo)Building your first demo [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-9-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section2.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section2.ipynb) Let’s start by installing Gradio! Since it is a Python package, simply run: `$ pip install gradio` You can run Gradio anywhere, be it from your favourite Python IDE, to Jupyter notebooks or even in Google Colab 🤯! So install Gradio wherever you run Python! Let’s get started with a simple “Hello World” example to get familiar with the Gradio syntax: ``` import gradio as gr def greet(name): return "Hello " + name demo = gr.Interface(fn=greet, inputs="text", outputs="text") demo.launch()``` Let’s walk through the code above: - First, we define a function called `greet()`. In this case, it is a simple function that adds “Hello” before your name, but it can be _any_ Python function in general. For example, in machine learning applications, this function would _call a model to make a prediction_ on an input and return the output. - Then, we create a Gradio `Interface` with three arguments, `fn`, `inputs`, and `outputs`. These arguments define the prediction function, as well as the _type_ of input and output components we would like. In our case, both components are simple text boxes. - We then call the `launch()` method on the `Interface` that we created. If you run this code, the interface below will appear automatically within a Jupyter/Colab notebook, or pop in a browser on **[http://localhost:7860](http://localhost:7860/)** if running from a script. Try using this GUI right now with your own name or some other input! You’ll notice that in this GUI, Gradio automatically inferred the name of the input parameter (`name`) and applied it as a label on top of the textbox. What if you’d like to change that? Or if you’d like to customize the textbox in some other way? In that case, you can instantiate a class object representing the input component. Take a look at the example below: ``` import gradio as gr def greet(name): return "Hello " + name textbox = gr.Textbox(label="Type your name here:", placeholder="John Doe", lines=2) gr.Interface(fn=greet, inputs=textbox, outputs="text").launch()``` Here, we’ve created an input textbox with a label, a placeholder, and a set number of lines. You could do the same for the output textbox, but we’ll leave that for now. We’ve seen that with just a few lines of code, Gradio lets you create a simple interface around any function with any kind of inputs or outputs. In this section, we’ve started with a simple textbox, but in the next sections, we’ll cover other kinds of inputs and outputs. Let’s now take a look at including some NLP in a Gradio application. ## [](#including-model-predictions)🤖 Including model predictions Let’s now build a simple interface that allows you to demo a **text-generation** model like GPT-2. We’ll load our model using the `pipeline()` function from 🤗 Transformers. If you need a quick refresher, you can go back to [that section in Chapter 1](/course/chapter1/3#text-generation). First, we define a prediction function that takes in a text prompt and returns the text completion: ``` from transformers import pipeline model = pipeline("text-generation") def predict(prompt): completion = model(prompt)[0]["generated_text"] return completion``` This function completes prompts that you provide, and you can run it with your own input prompts to see how it works. Here is an example (you might get a different completion): ``` predict("My favorite programming language is")``` ``` >> My favorite programming language is Haskell. I really enjoyed the Haskell language, but it doesn't have all the features that can be applied to any other language. For example, all it does is compile to a byte array.``` Now that we have a function for generating predictions, we can create and launch an `Interface` in the same way we did earlier: ``` import gradio as gr gr.Interface(fn=predict, inputs="text", outputs="text").launch()``` That’s it! You can now use this interface to generate text using the GPT-2 model as shown below 🤯. Keep reading to see how to build other kinds of demos with Gradio!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Building your first demo - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter9/2"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Building your first demo - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;building-your-first-demo&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;including-model-predictions&quot;,&quot;title&quot;:&quot;🤖 Including model predictions&quot;}],&quot;title&quot;:&quot;Building your first demo&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter9/2&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Building your first demo&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Building your first demo</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/1?fw=pt">Introduction to Gradio </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter9/2?fw=pt">Building your first demo </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/3?fw=pt">Understanding the Interface class </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/4?fw=pt">Sharing demos with others </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/5?fw=pt">Integrations with the Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/6?fw=pt">Advanced Interface features </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/7?fw=pt">Introduction to Blocks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/8?fw=pt">Gradio, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="building-your-first-demo" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#building-your-first-demo"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Building your first demo</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-9-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section2.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section2.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>Let’s start by installing Gradio! Since it is a Python package, simply run:</p> <p><code>$ pip install gradio</code></p> <p>You can run Gradio anywhere, be it from your favourite Python IDE, to Jupyter notebooks or even in Google Colab 🤯! So install Gradio wherever you run Python!</p> <p>Let’s get started with a simple “Hello World” example to get familiar with the Gradio syntax:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr <span class="hljs-keyword">def</span> <span class="hljs-title function_">greet</span>(<span class="hljs-params">name</span>): <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello "</span> + name demo = gr.Interface(fn=greet, inputs=<span class="hljs-string">"text"</span>, outputs=<span class="hljs-string">"text"</span>) demo.launch()</pre></div> <p>Let’s walk through the code above:</p> <ul><li>First, we define a function called <code>greet()</code>. In this case, it is a simple function that adds “Hello” before your name, but it can be <em>any</em> Python function in general. For example, in machine learning applications, this function would <em>call a model to make a prediction</em> on an input and return the output.</li> <li>Then, we create a Gradio <code>Interface</code> with three arguments, <code>fn</code>, <code>inputs</code>, and <code>outputs</code>. These arguments define the prediction function, as well as the <em>type</em> of input and output components we would like. In our case, both components are simple text boxes.</li> <li>We then call the <code>launch()</code> method on the <code>Interface</code> that we created.</li></ul> <p>If you run this code, the interface below will appear automatically within a Jupyter/Colab notebook, or pop in a browser on <strong><a href="http://localhost:7860/" rel="nofollow">http://localhost:7860</a></strong> if running from a script.</p> <iframe src="https://course-demos-hello-world.hf.space" frameborder="0" height="250" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>Try using this GUI right now with your own name or some other input!</p> <p>You’ll notice that in this GUI, Gradio automatically inferred the name of the input parameter (<code>name</code>) and applied it as a label on top of the textbox. What if you’d like to change that? Or if you’d like to customize the textbox in some other way? In that case, you can instantiate a class object representing the input component.</p> <p>Take a look at the example below:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr <span class="hljs-keyword">def</span> <span class="hljs-title function_">greet</span>(<span class="hljs-params">name</span>): <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello "</span> + name <span class="hljs-comment"># We instantiate the Textbox class</span> textbox = gr.Textbox(label=<span class="hljs-string">"Type your name here:"</span>, placeholder=<span class="hljs-string">"John Doe"</span>, lines=<span class="hljs-number">2</span>) gr.Interface(fn=greet, inputs=textbox, outputs=<span class="hljs-string">"text"</span>).launch()</pre></div> <iframe src="https://course-demos-hello-world-custom.hf.space" frameborder="0" height="300" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>Here, we’ve created an input textbox with a label, a placeholder, and a set number of lines. You could do the same for the output textbox, but we’ll leave that for now.</p> <p>We’ve seen that with just a few lines of code, Gradio lets you create a simple interface around any function with any kind of inputs or outputs. In this section, we’ve started with a simple textbox, but in the next sections, we’ll cover other kinds of inputs and outputs. Let’s now take a look at including some NLP in a Gradio application.</p> <h2 class="relative group"><a id="including-model-predictions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#including-model-predictions"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>🤖 Including model predictions</span></h2> <p>Let’s now build a simple interface that allows you to demo a <strong>text-generation</strong> model like GPT-2.</p> <p>We’ll load our model using the <code>pipeline()</code> function from 🤗 Transformers. If you need a quick refresher, you can go back to <a href="/course/chapter1/3#text-generation">that section in Chapter 1</a>.</p> <p>First, we define a prediction function that takes in a text prompt and returns the text completion:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline model = pipeline(<span class="hljs-string">"text-generation"</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">predict</span>(<span class="hljs-params">prompt</span>): completion = model(prompt)[<span class="hljs-number">0</span>][<span class="hljs-string">"generated_text"</span>] <span class="hljs-keyword">return</span> completion</pre></div> <p>This function completes prompts that you provide, and you can run it with your own input prompts to see how it works. Here is an example (you might get a different completion):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-function"><span class="hljs-title">predict</span><span class="hljs-params">(<span class="hljs-string">"My favorite programming language is"</span>)</span></span></pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>&gt;&gt; My favorite programming language <span class="hljs-keyword">is</span> Haskell. I really enjoyed <span class="hljs-keyword">the</span> Haskell language, <span class="hljs-keyword">but</span> <span class="hljs-keyword">it</span> doesn't have all <span class="hljs-keyword">the</span> features <span class="hljs-keyword">that</span> can be applied <span class="hljs-keyword">to</span> any other language. For example, all <span class="hljs-keyword">it</span> <span class="hljs-keyword">does</span> <span class="hljs-keyword">is</span> compile <span class="hljs-keyword">to</span> a byte array.</pre></div> <p>Now that we have a function for generating predictions, we can create and launch an <code>Interface</code> in the same way we did earlier:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr gr.Interface(fn=predict, inputs=<span class="hljs-string">"text"</span>, outputs=<span class="hljs-string">"text"</span>).launch()</pre></div> <p>That’s it! You can now use this interface to generate text using the GPT-2 model as shown below 🤯.</p> <iframe src="https://course-demos-gpt-2.hf.space" frameborder="0" height="300" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>Keep reading to see how to build other kinds of demos with Gradio!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter9/1?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Introduction to Gradio</a> <a href="/learn/nlp-course/chapter9/3?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Understanding the Interface class<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;building-your-first-demo&quot;,&quot;url&quot;:&quot;#building-your-first-demo&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤖 Including model predictions&quot;,&quot;id&quot;:&quot;including-model-predictions&quot;,&quot;url&quot;:&quot;#including-model-predictions&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#building-your-first-demo" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-building-your-first-demo"><wbr>Building your first demo</a> <a href="#including-model-predictions" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-including-model-predictions">🤖 <wbr>Including model predictions</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter9/2" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter9/2"); } </script> <iframe name="__privateStripeMetricsController7190" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter9%2F2%3Ffw%3Dpt&amp;title=Building%20your%20first%20demo%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:38.176Z
Integrations with the Hugging Face Hub - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter9/5?fw=pt
## [](#integrations-with-the-hugging-face-hub)Integrations with the Hugging Face Hub [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-9-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section5.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section5.ipynb) To make your life even easier, Gradio integrates directly with Hugging Face Hub and Hugging Face Spaces. You can load demos from the Hub and Spaces with only _one line of code_. ### [](#loading-models-from-the-hugging-face-hub)Loading models from the Hugging Face Hub To start with, choose one of the thousands of models Hugging Face offers through the Hub, as described in \[Chapter 4\](/course/chapter4/2). Using the special `Interface.load()` method, you pass `"model/"` (or, equivalently, `"huggingface/"`) followed by the model name. For example, here is the code to build a demo for [GPT-J](https://huggingface.co/EleutherAI/gpt-j-6B), a large language model, add a couple of example inputs: ``` import gradio as gr title = "GPT-J-6B" description = "Gradio Demo for GPT-J 6B, a transformer model trained using Ben Wang's Mesh Transformer JAX. 'GPT-J' refers to the class of model, while '6B' represents the number of trainable parameters. To use it, simply add your text, or click one of the examples to load them. Read more at the links below." article = "<p style='text-align: center'><a href='https://github.com/kingoflolz/mesh-transformer-jax' target='_blank'>GPT-J-6B: A 6 Billion Parameter Autoregressive Language Model</a></p>" gr.Interface.load( "huggingface/EleutherAI/gpt-j-6B", inputs=gr.Textbox(lines=5, label="Input Text"), title=title, description=description, article=article, ).launch()``` The code above will produce the interface below: Loading a model in this way uses Hugging Face’s [Inference API](https://huggingface.co/inference-api), instead of loading the model in memory. This is ideal for huge models like GPT-J or T0pp which require lots of RAM. ### [](#loading-from-hugging-face-spaces)Loading from Hugging Face Spaces To load any Space from the Hugging Face Hub and recreate it locally, you can pass \`spaces/\` to the \`Interface\`, followed by the name of the Space. Remember the demo from section 1 that removes the background of an image? Let’s load it from Hugging Face Spaces: ``` gr.Interface.load("spaces/abidlabs/remove-bg").launch()``` One of the cool things about loading demos from the Hub or Spaces is that you customize them by overriding any of the parameters. Here, we add a title and get it to work with a webcam instead: ``` gr.Interface.load( "spaces/abidlabs/remove-bg", inputs="webcam", title="Remove your webcam background!" ).launch()``` Now that we’ve explored a few ways to integrate Gradio with the Hugging Face Hub, let’s take a look at some advanced features of the `Interface` class. That’s the topic of the next section!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Integrations with the Hugging Face Hub - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter9/5"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Integrations with the Hugging Face Hub - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;integrations-with-the-hugging-face-hub&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;loading-models-from-the-hugging-face-hub&quot;,&quot;title&quot;:&quot;Loading models from the Hugging Face Hub&quot;},{&quot;local&quot;:&quot;loading-from-hugging-face-spaces&quot;,&quot;title&quot;:&quot;Loading from Hugging Face Spaces&quot;}],&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter9/5&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Integrations with the Hugging Face Hub</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/1?fw=pt">Introduction to Gradio </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/2?fw=pt">Building your first demo </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/3?fw=pt">Understanding the Interface class </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/4?fw=pt">Sharing demos with others </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter9/5?fw=pt">Integrations with the Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/6?fw=pt">Advanced Interface features </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/7?fw=pt">Introduction to Blocks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/8?fw=pt">Gradio, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="integrations-with-the-hugging-face-hub" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#integrations-with-the-hugging-face-hub"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Integrations with the Hugging Face Hub</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-9-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section5.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section5.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>To make your life even easier, Gradio integrates directly with Hugging Face Hub and Hugging Face Spaces. You can load demos from the Hub and Spaces with only <em>one line of code</em>.</p> <h3 class="relative group"><a id="loading-models-from-the-hugging-face-hub" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-models-from-the-hugging-face-hub"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Loading models from the Hugging Face Hub</span></h3> To start with, choose one of the thousands of models Hugging Face offers through the Hub, as described in [Chapter 4](/course/chapter4/2). <p>Using the special <code>Interface.load()</code> method, you pass <code>"model/"</code> (or, equivalently, <code>"huggingface/"</code>) followed by the model name. For example, here is the code to build a demo for <a href="https://huggingface.co/EleutherAI/gpt-j-6B" rel="nofollow">GPT-J</a>, a large language model, add a couple of example inputs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr title = <span class="hljs-string">"GPT-J-6B"</span> description = <span class="hljs-string">"Gradio Demo for GPT-J 6B, a transformer model trained using Ben Wang's Mesh Transformer JAX. 'GPT-J' refers to the class of model, while '6B' represents the number of trainable parameters. To use it, simply add your text, or click one of the examples to load them. Read more at the links below."</span> article = <span class="hljs-string">"&lt;p style='text-align: center'&gt;&lt;a href='https://github.com/kingoflolz/mesh-transformer-jax' target='_blank'&gt;GPT-J-6B: A 6 Billion Parameter Autoregressive Language Model&lt;/a&gt;&lt;/p&gt;"</span> gr.Interface.load( <span class="hljs-string">"huggingface/EleutherAI/gpt-j-6B"</span>, inputs=gr.Textbox(lines=<span class="hljs-number">5</span>, label=<span class="hljs-string">"Input Text"</span>), title=title, description=description, article=article, ).launch()</pre></div> <p>The code above will produce the interface below:</p> <iframe src="https://course-demos-gpt-j-6B.hf.space" frameborder="0" height="750" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>Loading a model in this way uses Hugging Face’s <a href="https://huggingface.co/inference-api" rel="nofollow">Inference API</a>, instead of loading the model in memory. This is ideal for huge models like GPT-J or T0pp which require lots of RAM.</p> <h3 class="relative group"><a id="loading-from-hugging-face-spaces" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-from-hugging-face-spaces"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Loading from Hugging Face Spaces</span></h3> To load any Space from the Hugging Face Hub and recreate it locally, you can pass `spaces/` to the `Interface`, followed by the name of the Space. <p>Remember the demo from section 1 that removes the background of an image? Let’s load it from Hugging Face Spaces:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>gr.Interface.load(<span class="hljs-string">"spaces/abidlabs/remove-bg"</span>).launch()</pre></div> <iframe src="https://course-demos-remove-bg-original.hf.space" frameborder="0" height="650" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>One of the cool things about loading demos from the Hub or Spaces is that you customize them by overriding any of the parameters. Here, we add a title and get it to work with a webcam instead:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>gr.Interface.load( <span class="hljs-string">"spaces/abidlabs/remove-bg"</span>, inputs=<span class="hljs-string">"webcam"</span>, title=<span class="hljs-string">"Remove your webcam background!"</span> ).launch()</pre></div> <iframe src="https://course-demos-Remove-bg.hf.space" frameborder="0" height="550" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>Now that we’ve explored a few ways to integrate Gradio with the Hugging Face Hub, let’s take a look at some advanced features of the <code>Interface</code> class. That’s the topic of the next section!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter9/4?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Sharing demos with others</a> <a href="/learn/nlp-course/chapter9/6?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Advanced Interface features<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;integrations-with-the-hugging-face-hub&quot;,&quot;url&quot;:&quot;#integrations-with-the-hugging-face-hub&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Loading models from the Hugging Face Hub&quot;,&quot;id&quot;:&quot;loading-models-from-the-hugging-face-hub&quot;,&quot;url&quot;:&quot;#loading-models-from-the-hugging-face-hub&quot;},{&quot;title&quot;:&quot;Loading from Hugging Face Spaces&quot;,&quot;id&quot;:&quot;loading-from-hugging-face-spaces&quot;,&quot;url&quot;:&quot;#loading-from-hugging-face-spaces&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#integrations-with-the-hugging-face-hub" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-integrations-with-the-hugging-face-hub"><wbr>Integrations with the <wbr>Hugging <wbr>Face <wbr>Hub</a> <a href="#loading-models-from-the-hugging-face-hub" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-loading-models-from-the-hugging-face-hub"><wbr>Loading models from the <wbr>Hugging <wbr>Face <wbr>Hub</a> <a href="#loading-from-hugging-face-spaces" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-loading-from-hugging-face-spaces"><wbr>Loading from <wbr>Hugging <wbr>Face <wbr>Spaces</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter9/5" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter9/5"); } </script> <iframe name="__privateStripeMetricsController5480" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter9%2F5%3Ffw%3Dpt&amp;title=Integrations%20with%20the%20Hugging%20Face%20Hub%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:38.256Z
Gradio, check! - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter9/8?fw=pt
## [](#gradio-check)Gradio, check! [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-9-questions) This wraps up the chapter on building cool ML demos with Gradio - we hope you enjoyed it! To recap, in this chapter we learned: - How to create Gradio demos with the high-level `Interface` API, and how to configure different input and output modalities. - Different ways to share Gradio demos, through temporary links and hosting on [Hugging Face Spaces](https://huggingface.co/spaces). - How to integrate Gradio demos with models and Spaces on the Hugging Face Hub. - Advanced features like storing state in a demo or providing authentication. - How to have full control of the data flow and layout of your demo with Gradio Blocks. If you’d like to test your understanding of the concepts covered in this chapter, check out the quiz in the next section! ## [](#where-to-next)Where to next? If you want to learn more about Gradio you can - Take a look at [Demos](https://github.com/gradio-app/gradio/tree/main/demo) in the repo, there are quite a lot of examples there. - See the [Guides](https://gradio.app/guides/) page, where you can find guides about cool and advanced features. - Check the [Docs](https://gradio.app/docs/) page to learn the details.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Gradio, check! - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter9/8"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Gradio, check! - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;gradio-check&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;where-to-next&quot;,&quot;title&quot;:&quot;Where to next?&quot;}],&quot;title&quot;:&quot;Gradio, check!&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter9/8&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Gradio, check!&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Gradio, check!</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/1?fw=pt">Introduction to Gradio </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/2?fw=pt">Building your first demo </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/3?fw=pt">Understanding the Interface class </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/4?fw=pt">Sharing demos with others </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/5?fw=pt">Integrations with the Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/6?fw=pt">Advanced Interface features </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/7?fw=pt">Introduction to Blocks </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter9/8?fw=pt">Gradio, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="gradio-check" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#gradio-check"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Gradio, check!</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-9-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>This wraps up the chapter on building cool ML demos with Gradio - we hope you enjoyed it! To recap, in this chapter we learned:</p> <ul><li>How to create Gradio demos with the high-level <code>Interface</code> API, and how to configure different input and output modalities.</li> <li>Different ways to share Gradio demos, through temporary links and hosting on <a href="https://huggingface.co/spaces" rel="nofollow">Hugging Face Spaces</a>.</li> <li>How to integrate Gradio demos with models and Spaces on the Hugging Face Hub.</li> <li>Advanced features like storing state in a demo or providing authentication.</li> <li>How to have full control of the data flow and layout of your demo with Gradio Blocks.</li></ul> <p>If you’d like to test your understanding of the concepts covered in this chapter, check out the quiz in the next section!</p> <h2 class="relative group"><a id="where-to-next" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#where-to-next"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Where to next?</span></h2> <p>If you want to learn more about Gradio you can</p> <ul><li>Take a look at <a href="https://github.com/gradio-app/gradio/tree/main/demo" rel="nofollow">Demos</a> in the repo, there are quite a lot of examples there.</li> <li>See the <a href="https://gradio.app/guides/" rel="nofollow">Guides</a> page, where you can find guides about cool and advanced features.</li> <li>Check the <a href="https://gradio.app/docs/" rel="nofollow">Docs</a> page to learn the details.</li></ul> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter9/7?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Introduction to Blocks</a> <a href="/learn/nlp-course/chapter9/9?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">End-of-chapter quiz<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;gradio-check&quot;,&quot;url&quot;:&quot;#gradio-check&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Where to next?&quot;,&quot;id&quot;:&quot;where-to-next&quot;,&quot;url&quot;:&quot;#where-to-next&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#gradio-check" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-gradio-check"><wbr>Gradio, check!</a> <a href="#where-to-next" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-where-to-next"><wbr>Where to next?</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter9/8" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter9/8"); } </script> <iframe name="__privateStripeMetricsController1190" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter9%2F8%3Ffw%3Dpt&amp;title=Gradio%2C%20check!%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:39.247Z
End-of-chapter quiz - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter9/9?fw=pt
## [](#end-of-chapter-quiz)End-of-chapter quiz [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-9-questions) Let’s test what you learned in this chapter! ### [](#1.-what-can-you-use-gradio-to-do?)1\. What can you use Gradio to do? ### [](#2.-gradio-only-works-with-pytorch-models)2\. Gradio ONLY works with PyTorch models ### [](#3.-where-can-you-launch-a-gradio-demo-from?)3\. Where can you launch a Gradio demo from? ### [](#4.-gradio-is-designed-primarily-for-nlp-models)4\. Gradio is designed primarily for NLP models ### [](#5.-which-of-the-following-features-are-supported-by-gradio?)5\. Which of the following features are supported by Gradio? ### [](#6.-which-of-the-following-are-valid-ways-of-loading-a-hugging-face-model-from-hub-or-spaces?)6\. Which of the following are valid ways of loading a Hugging Face model from Hub or Spaces? ### [](#7.-select-all-the-steps-necessary-for-adding-state-to-your-gradio-interface)7\. Select all the steps necessary for adding state to your Gradio interface ### [](#8.-which-of-the-following-are-components-included-in-the-gradio-library?)8\. Which of the following are components included in the Gradio library? ### [](#9.-what-does-gradio-<code>blocks</code>-allow-you-to-do?)9\. What does Gradio `Blocks` allow you to do? ### 10\. You can share a public link to a `Blocks` demo and host a `Blocks` demo on Hugging Face spaces.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="End-of-chapter quiz - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter9/9"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>End-of-chapter quiz - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;end-of-chapter-quiz&quot;,&quot;title&quot;:&quot;End-of-chapter quiz&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter9/9&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;End-of-chapter quiz&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">End-of-chapter quiz</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/1?fw=pt">Introduction to Gradio </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/2?fw=pt">Building your first demo </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/3?fw=pt">Understanding the Interface class </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/4?fw=pt">Sharing demos with others </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/5?fw=pt">Integrations with the Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/6?fw=pt">Advanced Interface features </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/7?fw=pt">Introduction to Blocks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/8?fw=pt">Gradio, check! </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter9/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="end-of-chapter-quiz" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#end-of-chapter-quiz"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>End-of-chapter quiz</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-9-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> </div> <p>Let’s test what you learned in this chapter!</p> <h3 class="relative group"><a id="1.-what-can-you-use-gradio-to-do?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#1.-what-can-you-use-gradio-to-do?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>1. What can you use Gradio to do?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Create a demo for your machine learning model</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Share your machine learning model with others</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Debug your model</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Train your model</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="2.-gradio-only-works-with-pytorch-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#2.-gradio-only-works-with-pytorch-models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>2. Gradio ONLY works with PyTorch models</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> True</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> False</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="3.-where-can-you-launch-a-gradio-demo-from?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#3.-where-can-you-launch-a-gradio-demo-from?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>3. Where can you launch a Gradio demo from?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Standard python IDEs</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Google Colab notebooks</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Jupyter notebooks</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="4.-gradio-is-designed-primarily-for-nlp-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#4.-gradio-is-designed-primarily-for-nlp-models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>4. Gradio is designed primarily for NLP models</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> True</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> False</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="5.-which-of-the-following-features-are-supported-by-gradio?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#5.-which-of-the-following-features-are-supported-by-gradio?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>5. Which of the following features are supported by Gradio?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Multiple inputs and outputs</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> State for data persistance</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Username and passwords authentication</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Automatic analytics for who uses your gradio demo</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="4"> Loading a model from Hugging Face's model hub or Hugging Face Spaces</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="6.-which-of-the-following-are-valid-ways-of-loading-a-hugging-face-model-from-hub-or-spaces?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#6.-which-of-the-following-are-valid-ways-of-loading-a-hugging-face-model-from-hub-or-spaces?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>6. Which of the following are valid ways of loading a Hugging Face model from Hub or Spaces?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> gr.Interface.load('huggingface/{user}/{model_name}')</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> gr.Interface.load('model/{user}/{model_name}')</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> gr.Interface.load('demos/{user}/{model_name}')</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> gr.Interface.load('spaces/{user}/{model_name}')</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="7.-select-all-the-steps-necessary-for-adding-state-to-your-gradio-interface" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#7.-select-all-the-steps-necessary-for-adding-state-to-your-gradio-interface"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>7. Select all the steps necessary for adding state to your Gradio interface</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Pass in an extra parameter into your prediction function, which represents the state of the interface.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> At the end of the prediction function, return the updated value of the state as an extra return value.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Add the state input and state output components when creating your Interface</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="8.-which-of-the-following-are-components-included-in-the-gradio-library?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#8.-which-of-the-following-are-components-included-in-the-gradio-library?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>8. Which of the following are components included in the Gradio library?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Textbox.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Graph.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Image.</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Audio.</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="9.-what-does-gradio-<code>blocks</code>-allow-you-to-do?" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#9.-what-does-gradio-<code>blocks</code>-allow-you-to-do?"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>9. What does Gradio <code>Blocks</code> allow you to do?</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> Combine multiple demos into one web app</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> Assign event triggers such as clicked/changed/etc to `Blocks` components</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="2"> Automatically determine which `Blocks` component should be interactive vs. static</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="3"> Create multi-step demos; meaning allowing you to reuse the output of one component as the input to the next</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <h3 class="relative group"><a id="10.-you-can-share-a-public-link-to-a-<code>blocks</code>-demo-and-host-a-<code>blocks</code>-demo-on-hugging-face-spaces." class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#10.-you-can-share-a-public-link-to-a-<code>blocks</code>-demo-and-host-a-<code>blocks</code>-demo-on-hugging-face-spaces."><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>10. You can share a public link to a <code>Blocks</code> demo and host a <code>Blocks</code> demo on Hugging Face spaces.</span></h3> <div><form><label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="0"> True</label> <label class="block"><input autocomplete="off" class="form-input -mt-1.5 mr-2" name="choice" type="checkbox" value="1"> False</label> <div class="flex flex-row items-center mt-3"><button class="btn px-4 mr-4" type="submit" disabled="">Submit</button> </div></form></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter9/8?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Gradio, check!</a> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isLoggedIn&quot;:false,&quot;nextChapterUrl&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;,&quot;quizChapter&quot;:9}" data-target="CourseCompleteButton"><div class="relative ml-auto"> <button class="btn btn-lg" disabled=""><svg class="mr-2" width="1em" height="1em" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0 16C0 7.16344 7.16344 0 16 0V0C24.8366 0 32 7.16344 32 16V16C32 24.8366 24.8366 32 16 32V32C7.16344 32 0 24.8366 0 16V16Z" fill="#D9FFD2"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M23.5607 10.9393C24.1464 11.5251 24.1464 12.4749 23.5607 13.0607L14.5 22.1213L8.93934 16.5607C8.35355 15.9749 8.35355 15.0251 8.93934 14.4393C9.52513 13.8536 10.4749 13.8536 11.0607 14.4393L14.5 17.8787L21.4393 10.9393C22.0251 10.3536 22.9749 10.3536 23.5607 10.9393Z" fill="#188B3F"></path></svg> Complete Chapter</button></div></div> <a href="/learn/nlp-course/events/1?fw=pt" class="hidden">Next chapter</a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;id&quot;:&quot;end-of-chapter-quiz&quot;,&quot;url&quot;:&quot;#end-of-chapter-quiz&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#end-of-chapter-quiz" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-end-of-chapter-quiz"><wbr>End-of-chapter quiz</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter9/9" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter9/9"); } </script> <iframe name="__privateStripeMetricsController8330" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter9%2F9%3Ffw%3Dpt&amp;title=End-of-chapter%20quiz%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:39.397Z
Advanced Interface features - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter9/6?fw=pt
## [](#advanced-interface-features)Advanced Interface features [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-9-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section6.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section6.ipynb) Now that we can build and share a basic interface, let’s explore some more advanced features such as state, and interpretation. ### [](#using-state-to-persist-data)Using state to persist data Gradio supports _session state_, where data persists across multiple submits within a page load. Session state is useful for building demos of, for example, chatbots where you want to persist data as the user interacts with the model. Note that session state does not share data between different users of your model. To store data in a session state, you need to do three things: 1. Pass in an _extra parameter_ into your function, which represents the state of the interface. 2. At the end of the function, return the updated value of the state as an _extra return value_. 3. Add the ‘state’ input and ‘state’ output components when creating your `Interface`. See the chatbot example below: ``` import random import gradio as gr def chat(message, history): history = history or [] if message.startswith("How many"): response = random.randint(1, 10) elif message.startswith("How"): response = random.choice(["Great", "Good", "Okay", "Bad"]) elif message.startswith("Where"): response = random.choice(["Here", "There", "Somewhere"]) else: response = "I don't know" history.append((message, response)) return history, history iface = gr.Interface( chat, ["text", "state"], ["chatbot", "state"], allow_screenshot=False, allow_flagging="never", ) iface.launch()``` Notice how the state of the output component persists across submits. Note: you can pass in a default value to the state parameter, which is used as the initial value of the state. ### [](#using-interpretation-to-understand-predictions)Using interpretation to understand predictions Most machine learning models are black boxes and the internal logic of the function is hidden from the end user. To encourage transparency, we’ve made it very easy to add interpretation to your model by simply setting the interpretation keyword in the Interface class to default. This allows your users to understand what parts of the input are responsible for the output. Take a look at the simple interface below which shows an image classifier that also includes interpretation: ``` import requests import tensorflow as tf import gradio as gr inception_net = tf.keras.applications.MobileNetV2() response = requests.get("https://git.io/JJkYN") labels = response.text.split("\n") def classify_image(inp): inp = inp.reshape((-1, 224, 224, 3)) inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp) prediction = inception_net.predict(inp).flatten() return {labels[i]: float(prediction[i]) for i in range(1000)} image = gr.Image(shape=(224, 224)) label = gr.Label(num_top_classes=3) title = "Gradio Image Classifiction + Interpretation Example" gr.Interface( fn=classify_image, inputs=image, outputs=label, interpretation="default", title=title ).launch()``` Test the interpretation function by submitting an input then clicking Interpret under the output component. Besides the default interpretation method Gradio provides, you can also specify `shap` for the `interpretation` parameter and set the `num_shap` parameter. This uses Shapley-based interpretation, which you can read more about [here](https://christophm.github.io/interpretable-ml-book/shap.html). Lastly, you can also pass in your own interpretation function into the `interpretation` parameter. See an example in Gradio’s getting started page [here](https://gradio.app/getting_started/). This wraps up our deep dive into the `Interface` class of Gradio. As we’ve seen, this class makes it simple to create machine learning demos in a few lines of Python code. However, sometimes you’ll want to customise your demo by changing the layout or chaining multiple prediction functions together. Wouldn’t it be nice if we could somehow split the `Interface` into customizable “blocks”? Fortunately, there is! That’s the topic of the final section.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Advanced Interface features - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter9/6"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Advanced Interface features - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;advanced-interface-features&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;using-state-to-persist-data&quot;,&quot;title&quot;:&quot;Using state to persist data&quot;},{&quot;local&quot;:&quot;using-interpretation-to-understand-predictions&quot;,&quot;title&quot;:&quot;Using interpretation to understand predictions&quot;}],&quot;title&quot;:&quot;Advanced Interface features&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter9/6&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Advanced Interface features&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Advanced Interface features</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/1?fw=pt">Introduction to Gradio </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/2?fw=pt">Building your first demo </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/3?fw=pt">Understanding the Interface class </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/4?fw=pt">Sharing demos with others </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/5?fw=pt">Integrations with the Hugging Face Hub </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter9/6?fw=pt">Advanced Interface features </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/7?fw=pt">Introduction to Blocks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/8?fw=pt">Gradio, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="advanced-interface-features" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#advanced-interface-features"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Advanced Interface features</span></h1> <div class="flex space-x-1 z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-9-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section6.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section6.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>Now that we can build and share a basic interface, let’s explore some more advanced features such as state, and interpretation.</p> <h3 class="relative group"><a id="using-state-to-persist-data" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-state-to-persist-data"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using state to persist data</span></h3> <p>Gradio supports <em>session state</em>, where data persists across multiple submits within a page load. Session state is useful for building demos of, for example, chatbots where you want to persist data as the user interacts with the model. Note that session state does not share data between different users of your model.</p> <p>To store data in a session state, you need to do three things:</p> <ol><li>Pass in an <em>extra parameter</em> into your function, which represents the state of the interface.</li> <li>At the end of the function, return the updated value of the state as an <em>extra return value</em>.</li> <li>Add the ‘state’ input and ‘state’ output components when creating your <code>Interface</code>.</li></ol> <p>See the chatbot example below:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> random <span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr <span class="hljs-keyword">def</span> <span class="hljs-title function_">chat</span>(<span class="hljs-params">message, history</span>): history = history <span class="hljs-keyword">or</span> [] <span class="hljs-keyword">if</span> message.startswith(<span class="hljs-string">"How many"</span>): response = random.randint(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>) <span class="hljs-keyword">elif</span> message.startswith(<span class="hljs-string">"How"</span>): response = random.choice([<span class="hljs-string">"Great"</span>, <span class="hljs-string">"Good"</span>, <span class="hljs-string">"Okay"</span>, <span class="hljs-string">"Bad"</span>]) <span class="hljs-keyword">elif</span> message.startswith(<span class="hljs-string">"Where"</span>): response = random.choice([<span class="hljs-string">"Here"</span>, <span class="hljs-string">"There"</span>, <span class="hljs-string">"Somewhere"</span>]) <span class="hljs-keyword">else</span>: response = <span class="hljs-string">"I don't know"</span> history.append((message, response)) <span class="hljs-keyword">return</span> history, history iface = gr.Interface( chat, [<span class="hljs-string">"text"</span>, <span class="hljs-string">"state"</span>], [<span class="hljs-string">"chatbot"</span>, <span class="hljs-string">"state"</span>], allow_screenshot=<span class="hljs-literal">False</span>, allow_flagging=<span class="hljs-string">"never"</span>, ) iface.launch()</pre></div> <iframe src="https://course-demos-Chatbot-Demo.hf.space" frameborder="0" height="350" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>Notice how the state of the output component persists across submits. Note: you can pass in a default value to the state parameter, which is used as the initial value of the state.</p> <h3 class="relative group"><a id="using-interpretation-to-understand-predictions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-interpretation-to-understand-predictions"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using interpretation to understand predictions</span></h3> <p>Most machine learning models are black boxes and the internal logic of the function is hidden from the end user. To encourage transparency, we’ve made it very easy to add interpretation to your model by simply setting the interpretation keyword in the Interface class to default. This allows your users to understand what parts of the input are responsible for the output. Take a look at the simple interface below which shows an image classifier that also includes interpretation:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> requests <span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf <span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr inception_net = tf.keras.applications.MobileNetV2() <span class="hljs-comment"># load the model</span> <span class="hljs-comment"># Download human-readable labels for ImageNet.</span> response = requests.get(<span class="hljs-string">"https://git.io/JJkYN"</span>) labels = response.text.split(<span class="hljs-string">"\n"</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">classify_image</span>(<span class="hljs-params">inp</span>): inp = inp.reshape((-<span class="hljs-number">1</span>, <span class="hljs-number">224</span>, <span class="hljs-number">224</span>, <span class="hljs-number">3</span>)) inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp) prediction = inception_net.predict(inp).flatten() <span class="hljs-keyword">return</span> {labels[i]: <span class="hljs-built_in">float</span>(prediction[i]) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>)} image = gr.Image(shape=(<span class="hljs-number">224</span>, <span class="hljs-number">224</span>)) label = gr.Label(num_top_classes=<span class="hljs-number">3</span>) title = <span class="hljs-string">"Gradio Image Classifiction + Interpretation Example"</span> gr.Interface( fn=classify_image, inputs=image, outputs=label, interpretation=<span class="hljs-string">"default"</span>, title=title ).launch()</pre></div> <p>Test the interpretation function by submitting an input then clicking Interpret under the output component.</p> <iframe src="https://course-demos-gradio-image-interpretation.hf.space" frameborder="0" height="570" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>Besides the default interpretation method Gradio provides, you can also specify <code>shap</code> for the <code>interpretation</code> parameter and set the <code>num_shap</code> parameter. This uses Shapley-based interpretation, which you can read more about <a href="https://christophm.github.io/interpretable-ml-book/shap.html" rel="nofollow">here</a>. Lastly, you can also pass in your own interpretation function into the <code>interpretation</code> parameter. See an example in Gradio’s getting started page <a href="https://gradio.app/getting_started/" rel="nofollow">here</a>.</p> <p>This wraps up our deep dive into the <code>Interface</code> class of Gradio. As we’ve seen, this class makes it simple to create machine learning demos in a few lines of Python code. However, sometimes you’ll want to customise your demo by changing the layout or chaining multiple prediction functions together. Wouldn’t it be nice if we could somehow split the <code>Interface</code> into customizable “blocks”? Fortunately, there is! That’s the topic of the final section.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter9/5?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Integrations with the Hugging Face Hub</a> <a href="/learn/nlp-course/chapter9/7?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Introduction to Blocks<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;advanced-interface-features&quot;,&quot;url&quot;:&quot;#advanced-interface-features&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Using state to persist data&quot;,&quot;id&quot;:&quot;using-state-to-persist-data&quot;,&quot;url&quot;:&quot;#using-state-to-persist-data&quot;},{&quot;title&quot;:&quot;Using interpretation to understand predictions&quot;,&quot;id&quot;:&quot;using-interpretation-to-understand-predictions&quot;,&quot;url&quot;:&quot;#using-interpretation-to-understand-predictions&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#advanced-interface-features" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-advanced-interface-features"><wbr>Advanced <wbr>Interface features</a> <a href="#using-state-to-persist-data" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-state-to-persist-data"><wbr>Using state to persist data</a> <a href="#using-interpretation-to-understand-predictions" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-interpretation-to-understand-predictions"><wbr>Using interpretation to understand predictions</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter9/6" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter9/6"); } </script> <iframe name="__privateStripeMetricsController5070" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter9%2F6%3Ffw%3Dpt&amp;title=Advanced%20Interface%20features%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:39.882Z
Introduction to Gradio Blocks - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/chapter9/7?fw=pt
## [](#introduction-to-gradio-blocks)Introduction to Gradio Blocks [![Ask a Question](https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4=)](https://discuss.huggingface.co/t/chapter-9-questions) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section7.ipynb) [![Open In Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section7.ipynb) In the previous sections we have explored and created demos using the `Interface` class. In this section we will introduce our **newly developed** low-level API called `gradio.Blocks`. Now, what’s the difference between `Interface` and `Blocks`? - ⚡ `Interface`: a high-level API that allows you to create a full machine learning demo simply by providing a list of inputs and outputs. - 🧱 `Blocks`: a low-level API that allows you to have full control over the data flows and layout of your application. You can build very complex, multi-step applications using `Blocks` (as in “building blocks”). ### [](#why-blocks-)Why Blocks 🧱? As we saw in the previous sections, the `Interface` class allows you to easily create full-fledged machine learning demos with just a few lines of code. The `Interface` API is extremely easy to use but lacks the flexibility that the `Blocks` API provides. For example, you might want to: - Group together related demos as multiple tabs in one web application - Change the layout of your demo, e.g. to specify where the inputs and outputs are located - Have multi-step interfaces, in which the output of one model becomes the input to the next model, or have more flexible data flows in general - Change a component’s properties (for example, the choices in a dropdown) or its visibility based on user input We will explore all of these concepts below. ### [](#creating-a-simple-demo-using-blocks)Creating a simple demo using Blocks After you have installed Gradio, run the code below as a Python script, a Jupyter notebook, or a Colab notebook. ``` import gradio as gr def flip_text(x): return x[::-1] demo = gr.Blocks() with demo: gr.Markdown( """ # Flip Text! Start typing below to see the output. """ ) input = gr.Textbox(placeholder="Flip this text") output = gr.Textbox() input.change(fn=flip_text, inputs=input, outputs=output) demo.launch()``` This simple example above introduces 4 concepts that underlie Blocks: 1. Blocks allow you to build web applications that combine markdown, HTML, buttons, and interactive components simply by instantiating objects in Python inside of a `with gradio.Blocks` context. 🙋If you're not familiar with the \`with\` statement in Python, we recommend checking out the excellent \[tutorial\](https://realpython.com/python-with-statement/) from Real Python. Come back here after reading that 🤗 The order in which you instantiate components matters as each element gets rendered into the web app in the order it was created. (More complex layouts are discussed below) 2. You can define regular Python functions anywhere in your code and run them with user input using `Blocks`. In our example, we have a simple function that “flips” the input text, but you can write any Python function, from a simple calculation to processing the predictions from a machine learning model. 3. You can assign events to any `Blocks` component. This will run your function when the component is clicked, changed, etc. When you assign an event, you pass in three parameters: `fn`: the function that should be called, `inputs`: the (list) of input component(s), and `outputs`: the (list) of output components that should be called. In the example above, we run the `flip_text()` function when the value in the `Textbox` named input `input` changes. The event reads the value in `input`, passes it as the name parameter to `flip_text()`, which then returns a value that gets assigned to our second `Textbox` named `output`. To see a list of events that each component supports, see the Gradio [documentation](https://www.gradio.app/docs/). 4. Blocks automatically figures out whether a component should be interactive (accept user input) or not, based on the event triggers you define. In our example, the first textbox is interactive, since its value is used by the `flip_text()` function. The second textbox is not interactive, since its value is never used as an input. In some cases, you might want to override this, which you can do by passing a boolean to the `interactive` parameter of the component (e.g. `gr.Textbox(placeholder="Flip this text", interactive=True)`). ### [](#customizing-the-layout-of-your-demo)Customizing the layout of your demo How can we use `Blocks` to customize the layout of our demo? By default, `Blocks` renders the components that you create vertically in one column. You can change that by creating additional columns `with gradio.Column():` or rows `with gradio.Row():` and creating components within those contexts. Here’s what you should keep in mind: any components created under a `Column` (this is also the default) will be laid out vertically. Any component created under a `Row` will be laid out horizontally, similar to the [flexbox model in web development](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox). Finally, you can also create tabs for your demo by using the `with gradio.Tabs()` context manager. Within this context, you can create multiple tabs by specifying `with gradio.TabItem(name_of_tab):` children. Any component created inside of a `with gradio.TabItem(name_of_tab):` context appears in that tab. Now let’s add a `flip_image()` function to our demo and add a new tab that flips images. Below is an example with 2 tabs and also uses a Row: ``` import numpy as np import gradio as gr demo = gr.Blocks() def flip_text(x): return x[::-1] def flip_image(x): return np.fliplr(x) with demo: gr.Markdown("Flip text or image files using this demo.") with gr.Tabs(): with gr.TabItem("Flip Text"): with gr.Row(): text_input = gr.Textbox() text_output = gr.Textbox() text_button = gr.Button("Flip") with gr.TabItem("Flip Image"): with gr.Row(): image_input = gr.Image() image_output = gr.Image() image_button = gr.Button("Flip") text_button.click(flip_text, inputs=text_input, outputs=text_output) image_button.click(flip_image, inputs=image_input, outputs=image_output) demo.launch()``` You’ll notice that in this example, we’ve also created a `Button` component in each tab, and we’ve assigned a click event to each button, which is what actually runs the function. ### [](#exploring-events-and-state)Exploring events and state Just as you can control the layout, `Blocks` gives you fine-grained control over what events trigger function calls. Each component and many layouts have specific events that they support. For example, the `Textbox` component has 2 events: `change()` (when the value inside of the textbox changes), and `submit()` (when a user presses the enter key while focused on the textbox). More complex components can have even more events: for example, the `Audio` component also has separate events for when the audio file is played, cleared, paused, etc. See the documentation for the events each component supports. You can attach event trigger to none, one, or more of these events. You create an event trigger by calling the name of the event on the component instance as a function — e.g. `textbox.change(...)` or `btn.click(...)`. The function takes in three parameters, as discussed above: - `fn`: the function to run - `inputs`: a (list of) component(s) whose values should supplied as the input parameters to the function. Each component’s value gets mapped to the corresponding function parameter, in order. This parameter can be None if the function does not take any parameters. - `outputs`: a (list of) component(s) whose values should be updated based on the values returned by the function. Each return value sets the corresponding component’s value, in order. This parameter can be None if the function does not return anything. You can even make the input and output component be the same component, as we do in this example that uses a GPT model to do text completion: ``` import gradio as gr api = gr.Interface.load("huggingface/EleutherAI/gpt-j-6B") def complete_with_gpt(text): return text[:-50] + api(text[-50:]) with gr.Blocks() as demo: textbox = gr.Textbox(placeholder="Type here and press enter...", lines=4) btn = gr.Button("Generate") btn.click(complete_with_gpt, textbox, textbox) demo.launch()``` ### [](#creating-multi-step-demos)Creating multi-step demos In some cases, you might want a _multi-step demo_, in which you reuse the output of one function as the input to the next. This is really easy to do with `Blocks`, as you can use a component for the input of one event trigger but the output of another. Take a look at the text component in the example below, its value is the result of a speech-to-text model, but also gets passed into a sentiment analysis model: ``` from transformers import pipeline import gradio as gr asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h") classifier = pipeline("text-classification") def speech_to_text(speech): text = asr(speech)["text"] return text def text_to_sentiment(text): return classifier(text)[0]["label"] demo = gr.Blocks() with demo: audio_file = gr.Audio(type="filepath") text = gr.Textbox() label = gr.Label() b1 = gr.Button("Recognize Speech") b2 = gr.Button("Classify Sentiment") b1.click(speech_to_text, inputs=audio_file, outputs=text) b2.click(text_to_sentiment, inputs=text, outputs=label) demo.launch()``` ### [](#updating-component-properties)Updating Component Properties So far, we have seen how to create events to update the value of another component. But what happens if you want to change other properties of a component, like the visibility of a textbox or the choices in a radio button group? You can do this by returning a component class’s `update()` method instead of a regular return value from your function. This is most easily illustrated with an example: ``` import gradio as gr def change_textbox(choice): if choice == "short": return gr.Textbox.update(lines=2, visible=True) elif choice == "long": return gr.Textbox.update(lines=8, visible=True) else: return gr.Textbox.update(visible=False) with gr.Blocks() as block: radio = gr.Radio( ["short", "long", "none"], label="What kind of essay would you like to write?" ) text = gr.Textbox(lines=2, interactive=True) radio.change(fn=change_textbox, inputs=radio, outputs=text) block.launch()``` We just explored all the core concepts of `Blocks`! Just like with `Interfaces`, you can create cool demos that can be shared by using `share=True` in the `launch()` method or deployed on [Hugging Face Spaces](https://huggingface.co/spaces).
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Introduction to Gradio Blocks - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/chapter9/7"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Introduction to Gradio Blocks - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;introduction-to-gradio-blocks&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;why-blocks-&quot;,&quot;title&quot;:&quot;Why Blocks 🧱?&quot;},{&quot;local&quot;:&quot;creating-a-simple-demo-using-blocks&quot;,&quot;title&quot;:&quot;Creating a simple demo using Blocks&quot;},{&quot;local&quot;:&quot;customizing-the-layout-of-your-demo&quot;,&quot;title&quot;:&quot;Customizing the layout of your demo&quot;},{&quot;local&quot;:&quot;exploring-events-and-state&quot;,&quot;title&quot;:&quot;Exploring events and state&quot;},{&quot;local&quot;:&quot;creating-multi-step-demos&quot;,&quot;title&quot;:&quot;Creating multi-step demos&quot;},{&quot;local&quot;:&quot;updating-component-properties&quot;,&quot;title&quot;:&quot;Updating Component Properties&quot;}],&quot;title&quot;:&quot;Introduction to Gradio Blocks&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;chapter9/7&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Introduction to Gradio Blocks&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Introduction to Gradio Blocks</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/1?fw=pt">Introduction to Gradio </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/2?fw=pt">Building your first demo </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/3?fw=pt">Understanding the Interface class </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/4?fw=pt">Sharing demos with others </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/5?fw=pt">Integrations with the Hugging Face Hub </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/6?fw=pt">Advanced Interface features </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/chapter9/7?fw=pt">Introduction to Blocks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/8?fw=pt">Gradio, check! </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/chapter9/9?fw=pt">End-of-chapter quiz </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> </nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="introduction-to-gradio-blocks" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#introduction-to-gradio-blocks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Introduction to Gradio Blocks</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"><a href="https://discuss.huggingface.co/t/chapter-9-questions" target="_blank"><img alt="Ask a Question" class="!m-0" src="https://img.shields.io/badge/Ask%20a%20question-ffcb4c.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgLTEgMTA0IDEwNiI+PGRlZnM+PHN0eWxlPi5jbHMtMXtmaWxsOiMyMzFmMjA7fS5jbHMtMntmaWxsOiNmZmY5YWU7fS5jbHMtM3tmaWxsOiMwMGFlZWY7fS5jbHMtNHtmaWxsOiMwMGE5NGY7fS5jbHMtNXtmaWxsOiNmMTVkMjI7fS5jbHMtNntmaWxsOiNlMzFiMjM7fTwvc3R5bGU+PC9kZWZzPjx0aXRsZT5EaXNjb3Vyc2VfbG9nbzwvdGl0bGU+PGcgaWQ9IkxheWVyXzIiPjxnIGlkPSJMYXllcl8zIj48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik01MS44NywwQzIzLjcxLDAsMCwyMi44MywwLDUxYzAsLjkxLDAsNTIuODEsMCw1Mi44MWw1MS44Ni0uMDVjMjguMTYsMCw1MS0yMy43MSw1MS01MS44N1M4MCwwLDUxLjg3LDBaIi8+PHBhdGggY2xhc3M9ImNscy0yIiBkPSJNNTIuMzcsMTkuNzRBMzEuNjIsMzEuNjIsMCwwLDAsMjQuNTgsNjYuNDFsLTUuNzIsMTguNEwzOS40LDgwLjE3YTMxLjYxLDMxLjYxLDAsMSwwLDEzLTYwLjQzWiIvPjxwYXRoIGNsYXNzPSJjbHMtMyIgZD0iTTc3LjQ1LDMyLjEyYTMxLjYsMzEuNiwwLDAsMS0zOC4wNSw0OEwxOC44Niw4NC44MmwyMC45MS0yLjQ3QTMxLjYsMzEuNiwwLDAsMCw3Ny40NSwzMi4xMloiLz48cGF0aCBjbGFzcz0iY2xzLTQiIGQ9Ik03MS42MywyNi4yOUEzMS42LDMxLjYsMCwwLDEsMzguOCw3OEwxOC44Niw4NC44MiwzOS40LDgwLjE3QTMxLjYsMzEuNiwwLDAsMCw3MS42MywyNi4yOVoiLz48cGF0aCBjbGFzcz0iY2xzLTUiIGQ9Ik0yNi40Nyw2Ny4xMWEzMS42MSwzMS42MSwwLDAsMSw1MS0zNUEzMS42MSwzMS42MSwwLDAsMCwyNC41OCw2Ni40MWwtNS43MiwxOC40WiIvPjxwYXRoIGNsYXNzPSJjbHMtNiIgZD0iTTI0LjU4LDY2LjQxQTMxLjYxLDMxLjYxLDAsMCwxLDcxLjYzLDI2LjI5YTMxLjYxLDMxLjYxLDAsMCwwLTQ5LDM5LjYzbC0zLjc2LDE4LjlaIi8+PC9nPjwvZz48L3N2Zz4="></a> <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/en/chapter9/section7.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></a> <a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/en/chapter9/section7.ipynb" target="_blank"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></a></div> <p>In the previous sections we have explored and created demos using the <code>Interface</code> class. In this section we will introduce our <strong>newly developed</strong> low-level API called <code>gradio.Blocks</code>.</p> <p>Now, what’s the difference between <code>Interface</code> and <code>Blocks</code>?</p> <ul><li><p>⚡ <code>Interface</code>: a high-level API that allows you to create a full machine learning demo simply by providing a list of inputs and outputs.</p></li> <li><p>🧱 <code>Blocks</code>: a low-level API that allows you to have full control over the data flows and layout of your application. You can build very complex, multi-step applications using <code>Blocks</code> (as in “building blocks”).</p></li></ul> <h3 class="relative group"><a id="why-blocks-" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#why-blocks-"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Why Blocks 🧱?</span></h3> <p>As we saw in the previous sections, the <code>Interface</code> class allows you to easily create full-fledged machine learning demos with just a few lines of code. The <code>Interface</code> API is extremely easy to use but lacks the flexibility that the <code>Blocks</code> API provides. For example, you might want to:</p> <ul><li>Group together related demos as multiple tabs in one web application</li> <li>Change the layout of your demo, e.g. to specify where the inputs and outputs are located</li> <li>Have multi-step interfaces, in which the output of one model becomes the input to the next model, or have more flexible data flows in general</li> <li>Change a component’s properties (for example, the choices in a dropdown) or its visibility based on user input</li></ul> <p>We will explore all of these concepts below.</p> <h3 class="relative group"><a id="creating-a-simple-demo-using-blocks" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#creating-a-simple-demo-using-blocks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Creating a simple demo using Blocks</span></h3> <p>After you have installed Gradio, run the code below as a Python script, a Jupyter notebook, or a Colab notebook.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr <span class="hljs-keyword">def</span> <span class="hljs-title function_">flip_text</span>(<span class="hljs-params">x</span>): <span class="hljs-keyword">return</span> x[::-<span class="hljs-number">1</span>] demo = gr.Blocks() <span class="hljs-keyword">with</span> demo: gr.Markdown( <span class="hljs-string">""" # Flip Text! Start typing below to see the output. """</span> ) <span class="hljs-built_in">input</span> = gr.Textbox(placeholder=<span class="hljs-string">"Flip this text"</span>) output = gr.Textbox() <span class="hljs-built_in">input</span>.change(fn=flip_text, inputs=<span class="hljs-built_in">input</span>, outputs=output) demo.launch()</pre></div> <iframe src="https://course-demos-flip-text.hf.space" frameborder="0" height="400" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>This simple example above introduces 4 concepts that underlie Blocks:</p> <ol><li><p>Blocks allow you to build web applications that combine markdown, HTML, buttons, and interactive components simply by instantiating objects in Python inside of a <code>with gradio.Blocks</code> context.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400">🙋If you're not familiar with the `with` statement in Python, we recommend checking out the excellent [tutorial](https://realpython.com/python-with-statement/) from Real Python. Come back here after reading that 🤗</div> The order in which you instantiate components matters as each element gets rendered into the web app in the order it was created. (More complex layouts are discussed below)</li> <li><p>You can define regular Python functions anywhere in your code and run them with user input using <code>Blocks</code>. In our example, we have a simple function that “flips” the input text, but you can write any Python function, from a simple calculation to processing the predictions from a machine learning model.</p></li> <li><p>You can assign events to any <code>Blocks</code> component. This will run your function when the component is clicked, changed, etc. When you assign an event, you pass in three parameters: <code>fn</code>: the function that should be called, <code>inputs</code>: the (list) of input component(s), and <code>outputs</code>: the (list) of output components that should be called.</p> <p>In the example above, we run the <code>flip_text()</code> function when the value in the <code>Textbox</code> named input <code>input</code> changes. The event reads the value in <code>input</code>, passes it as the name parameter to <code>flip_text()</code>, which then returns a value that gets assigned to our second <code>Textbox</code> named <code>output</code>.</p> <p>To see a list of events that each component supports, see the Gradio <a href="https://www.gradio.app/docs/" rel="nofollow">documentation</a>.</p></li> <li><p>Blocks automatically figures out whether a component should be interactive (accept user input) or not, based on the event triggers you define. In our example, the first textbox is interactive, since its value is used by the <code>flip_text()</code> function. The second textbox is not interactive, since its value is never used as an input. In some cases, you might want to override this, which you can do by passing a boolean to the <code>interactive</code> parameter of the component (e.g. <code>gr.Textbox(placeholder="Flip this text", interactive=True)</code>).</p></li></ol> <h3 class="relative group"><a id="customizing-the-layout-of-your-demo" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#customizing-the-layout-of-your-demo"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Customizing the layout of your demo</span></h3> <p>How can we use <code>Blocks</code> to customize the layout of our demo? By default, <code>Blocks</code> renders the components that you create vertically in one column. You can change that by creating additional columns <code>with gradio.Column():</code> or rows <code>with gradio.Row():</code> and creating components within those contexts.</p> <p>Here’s what you should keep in mind: any components created under a <code>Column</code> (this is also the default) will be laid out vertically. Any component created under a <code>Row</code> will be laid out horizontally, similar to the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox" rel="nofollow">flexbox model in web development</a>.</p> <p>Finally, you can also create tabs for your demo by using the <code>with gradio.Tabs()</code> context manager. Within this context, you can create multiple tabs by specifying <code>with gradio.TabItem(name_of_tab):</code> children. Any component created inside of a <code>with gradio.TabItem(name_of_tab):</code> context appears in that tab.</p> <p>Now let’s add a <code>flip_image()</code> function to our demo and add a new tab that flips images. Below is an example with 2 tabs and also uses a Row:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr demo = gr.Blocks() <span class="hljs-keyword">def</span> <span class="hljs-title function_">flip_text</span>(<span class="hljs-params">x</span>): <span class="hljs-keyword">return</span> x[::-<span class="hljs-number">1</span>] <span class="hljs-keyword">def</span> <span class="hljs-title function_">flip_image</span>(<span class="hljs-params">x</span>): <span class="hljs-keyword">return</span> np.fliplr(x) <span class="hljs-keyword">with</span> demo: gr.Markdown(<span class="hljs-string">"Flip text or image files using this demo."</span>) <span class="hljs-keyword">with</span> gr.Tabs(): <span class="hljs-keyword">with</span> gr.TabItem(<span class="hljs-string">"Flip Text"</span>): <span class="hljs-keyword">with</span> gr.Row(): text_input = gr.Textbox() text_output = gr.Textbox() text_button = gr.Button(<span class="hljs-string">"Flip"</span>) <span class="hljs-keyword">with</span> gr.TabItem(<span class="hljs-string">"Flip Image"</span>): <span class="hljs-keyword">with</span> gr.Row(): image_input = gr.Image() image_output = gr.Image() image_button = gr.Button(<span class="hljs-string">"Flip"</span>) text_button.click(flip_text, inputs=text_input, outputs=text_output) image_button.click(flip_image, inputs=image_input, outputs=image_output) demo.launch()</pre></div> <iframe src="https://course-demos-flip-text-image.hf.space" frameborder="0" height="450" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>You’ll notice that in this example, we’ve also created a <code>Button</code> component in each tab, and we’ve assigned a click event to each button, which is what actually runs the function.</p> <h3 class="relative group"><a id="exploring-events-and-state" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#exploring-events-and-state"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Exploring events and state</span></h3> <p>Just as you can control the layout, <code>Blocks</code> gives you fine-grained control over what events trigger function calls. Each component and many layouts have specific events that they support.</p> <p>For example, the <code>Textbox</code> component has 2 events: <code>change()</code> (when the value inside of the textbox changes), and <code>submit()</code> (when a user presses the enter key while focused on the textbox). More complex components can have even more events: for example, the <code>Audio</code> component also has separate events for when the audio file is played, cleared, paused, etc. See the documentation for the events each component supports.</p> <p>You can attach event trigger to none, one, or more of these events. You create an event trigger by calling the name of the event on the component instance as a function — e.g. <code>textbox.change(...)</code> or <code>btn.click(...)</code>. The function takes in three parameters, as discussed above:</p> <ul><li><code>fn</code>: the function to run</li> <li><code>inputs</code>: a (list of) component(s) whose values should supplied as the input parameters to the function. Each component’s value gets mapped to the corresponding function parameter, in order. This parameter can be None if the function does not take any parameters.</li> <li><code>outputs</code>: a (list of) component(s) whose values should be updated based on the values returned by the function. Each return value sets the corresponding component’s value, in order. This parameter can be None if the function does not return anything.</li></ul> <p>You can even make the input and output component be the same component, as we do in this example that uses a GPT model to do text completion:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr api = gr.Interface.load(<span class="hljs-string">"huggingface/EleutherAI/gpt-j-6B"</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">complete_with_gpt</span>(<span class="hljs-params">text</span>): <span class="hljs-comment"># Use the last 50 characters of the text as context</span> <span class="hljs-keyword">return</span> text[:-<span class="hljs-number">50</span>] + api(text[-<span class="hljs-number">50</span>:]) <span class="hljs-keyword">with</span> gr.Blocks() <span class="hljs-keyword">as</span> demo: textbox = gr.Textbox(placeholder=<span class="hljs-string">"Type here and press enter..."</span>, lines=<span class="hljs-number">4</span>) btn = gr.Button(<span class="hljs-string">"Generate"</span>) btn.click(complete_with_gpt, textbox, textbox) demo.launch()</pre></div> <iframe src="https://course-demos-blocks-gpt.hf.space" frameborder="0" height="300" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <h3 class="relative group"><a id="creating-multi-step-demos" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#creating-multi-step-demos"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Creating multi-step demos</span></h3> <p>In some cases, you might want a <em>multi-step demo</em>, in which you reuse the output of one function as the input to the next. This is really easy to do with <code>Blocks</code>, as you can use a component for the input of one event trigger but the output of another. Take a look at the text component in the example below, its value is the result of a speech-to-text model, but also gets passed into a sentiment analysis model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline <span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr asr = pipeline(<span class="hljs-string">"automatic-speech-recognition"</span>, <span class="hljs-string">"facebook/wav2vec2-base-960h"</span>) classifier = pipeline(<span class="hljs-string">"text-classification"</span>) <span class="hljs-keyword">def</span> <span class="hljs-title function_">speech_to_text</span>(<span class="hljs-params">speech</span>): text = asr(speech)[<span class="hljs-string">"text"</span>] <span class="hljs-keyword">return</span> text <span class="hljs-keyword">def</span> <span class="hljs-title function_">text_to_sentiment</span>(<span class="hljs-params">text</span>): <span class="hljs-keyword">return</span> classifier(text)[<span class="hljs-number">0</span>][<span class="hljs-string">"label"</span>] demo = gr.Blocks() <span class="hljs-keyword">with</span> demo: audio_file = gr.Audio(<span class="hljs-built_in">type</span>=<span class="hljs-string">"filepath"</span>) text = gr.Textbox() label = gr.Label() b1 = gr.Button(<span class="hljs-string">"Recognize Speech"</span>) b2 = gr.Button(<span class="hljs-string">"Classify Sentiment"</span>) b1.click(speech_to_text, inputs=audio_file, outputs=text) b2.click(text_to_sentiment, inputs=text, outputs=label) demo.launch()</pre></div> <iframe src="https://course-demos-blocks-multi-step.hf.space" frameborder="0" height="600" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <h3 class="relative group"><a id="updating-component-properties" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#updating-component-properties"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Updating Component Properties</span></h3> <p>So far, we have seen how to create events to update the value of another component. But what happens if you want to change other properties of a component, like the visibility of a textbox or the choices in a radio button group? You can do this by returning a component class’s <code>update()</code> method instead of a regular return value from your function.</p> <p>This is most easily illustrated with an example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> gradio <span class="hljs-keyword">as</span> gr <span class="hljs-keyword">def</span> <span class="hljs-title function_">change_textbox</span>(<span class="hljs-params">choice</span>): <span class="hljs-keyword">if</span> choice == <span class="hljs-string">"short"</span>: <span class="hljs-keyword">return</span> gr.Textbox.update(lines=<span class="hljs-number">2</span>, visible=<span class="hljs-literal">True</span>) <span class="hljs-keyword">elif</span> choice == <span class="hljs-string">"long"</span>: <span class="hljs-keyword">return</span> gr.Textbox.update(lines=<span class="hljs-number">8</span>, visible=<span class="hljs-literal">True</span>) <span class="hljs-keyword">else</span>: <span class="hljs-keyword">return</span> gr.Textbox.update(visible=<span class="hljs-literal">False</span>) <span class="hljs-keyword">with</span> gr.Blocks() <span class="hljs-keyword">as</span> block: radio = gr.Radio( [<span class="hljs-string">"short"</span>, <span class="hljs-string">"long"</span>, <span class="hljs-string">"none"</span>], label=<span class="hljs-string">"What kind of essay would you like to write?"</span> ) text = gr.Textbox(lines=<span class="hljs-number">2</span>, interactive=<span class="hljs-literal">True</span>) radio.change(fn=change_textbox, inputs=radio, outputs=text) block.launch()</pre></div> <iframe src="https://course-demos-blocks-update-component-properties.hf.space" frameborder="0" height="300" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <p>We just explored all the core concepts of <code>Blocks</code>! Just like with <code>Interfaces</code>, you can create cool demos that can be shared by using <code>share=True</code> in the <code>launch()</code> method or deployed on <a href="https://huggingface.co/spaces" rel="nofollow">Hugging Face Spaces</a>.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter9/6?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Advanced Interface features</a> <a href="/learn/nlp-course/chapter9/8?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Gradio, check!<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Introduction to Gradio Blocks&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;introduction-to-gradio-blocks&quot;,&quot;url&quot;:&quot;#introduction-to-gradio-blocks&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Why Blocks 🧱?&quot;,&quot;id&quot;:&quot;why-blocks-&quot;,&quot;url&quot;:&quot;#why-blocks-&quot;},{&quot;title&quot;:&quot;Creating a simple demo using Blocks&quot;,&quot;id&quot;:&quot;creating-a-simple-demo-using-blocks&quot;,&quot;url&quot;:&quot;#creating-a-simple-demo-using-blocks&quot;},{&quot;title&quot;:&quot;Customizing the layout of your demo&quot;,&quot;id&quot;:&quot;customizing-the-layout-of-your-demo&quot;,&quot;url&quot;:&quot;#customizing-the-layout-of-your-demo&quot;},{&quot;title&quot;:&quot;Exploring events and state&quot;,&quot;id&quot;:&quot;exploring-events-and-state&quot;,&quot;url&quot;:&quot;#exploring-events-and-state&quot;},{&quot;title&quot;:&quot;Creating multi-step demos&quot;,&quot;id&quot;:&quot;creating-multi-step-demos&quot;,&quot;url&quot;:&quot;#creating-multi-step-demos&quot;},{&quot;title&quot;:&quot;Updating Component Properties&quot;,&quot;id&quot;:&quot;updating-component-properties&quot;,&quot;url&quot;:&quot;#updating-component-properties&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#introduction-to-gradio-blocks" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-introduction-to-gradio-blocks"><wbr>Introduction to <wbr>Gradio <wbr>Blocks</a> <a href="#why-blocks-" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-why-blocks-"><wbr>Why <wbr>Blocks 🧱?</a> <a href="#creating-a-simple-demo-using-blocks" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-creating-a-simple-demo-using-blocks"><wbr>Creating a simple demo using <wbr>Blocks</a> <a href="#customizing-the-layout-of-your-demo" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-customizing-the-layout-of-your-demo"><wbr>Customizing the layout of your demo</a> <a href="#exploring-events-and-state" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-exploring-events-and-state"><wbr>Exploring events and state</a> <a href="#creating-multi-step-demos" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-creating-multi-step-demos"><wbr>Creating multi-step demos</a> <a href="#updating-component-properties" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-updating-component-properties"><wbr>Updating <wbr>Component <wbr>Properties</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/chapter9/7" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/chapter9/7"); } </script> <iframe name="__privateStripeMetricsController3770" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fchapter9%2F7%3Ffw%3Dpt&amp;title=Introduction%20to%20Gradio%20Blocks%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:40.025Z
Live sessions and workshops - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/events/1?fw=pt
NLP Course documentation Live sessions and workshops 3\. Fine-tuning a pretrained model 4\. Sharing models and tokenizers 5\. The 🤗 Datasets library 6\. The 🤗 Tokenizers library 9\. Building and sharing demos new ## [](#live-sessions-and-workshops)Live sessions and workshops For the release of parts 1 and 2 of the course, we organized several live coding sessions and workshops. You can find links to the recordings of these sessions and workshops below. ## [](#live-coding-sessions)Live coding sessions For the first session, Sylvain goes through Chapter 1 of the course with you, explaining it step by step: In the second session, it is Lewis’ turn to present Chapter 2: Because Chapter 2 is so cool, Sylvain has also given a walkthrough of it! For Chapter 3, Lewis returns to guide you through the code: Finally, Omar concludes the live sessions related to the first part of the course by tackling chapter 4: ## [](#workshops)Workshops In the first workshop, Merve welcomes Lewis to discuss section 7 of chapter 7 about [question answering](https://huggingface.co/course/chapter7/7?fw=pt). For the second workshop, Merve hosts Leandro to talk about chapter 7, section 6 on [training a causal language model from scratch](https://huggingface.co/course/chapter7/6?fw=pt) with an application with [CodeParrot](https://huggingface.co/codeparrot).
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Live sessions and workshops - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/events/1"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Live sessions and workshops - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;live-sessions-and-workshops&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;live-coding-sessions&quot;,&quot;title&quot;:&quot;Live coding sessions&quot;},{&quot;local&quot;:&quot;workshops&quot;,&quot;title&quot;:&quot;Workshops&quot;}],&quot;title&quot;:&quot;Live sessions and workshops&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;events/1&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Live sessions and workshops&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Live sessions and workshops</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/events/1?fw=pt">Live sessions and workshops </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/events/2?fw=pt">Part 2 release event </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/events/3?fw=pt">Gradio Blocks party </a> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="live-sessions-and-workshops" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#live-sessions-and-workshops"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Live sessions and workshops</span></h1> <p>For the release of parts 1 and 2 of the course, we organized several live coding sessions and workshops. You can find links to the recordings of these sessions and workshops below.</p> <h2 class="relative group"><a id="live-coding-sessions" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#live-coding-sessions"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Live coding sessions</span></h2> <p>For the first session, Sylvain goes through Chapter 1 of the course with you, explaining it step by step:</p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/aV4wfnIakSQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p>In the second session, it is Lewis’ turn to present Chapter 2:</p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/qEl7eORxpFA" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p>Because Chapter 2 is so cool, Sylvain has also given a walkthrough of it!</p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/u4e8OGWYpPk" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p>For Chapter 3, Lewis returns to guide you through the code:</p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/Be4s0dsbavM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p>Finally, Omar concludes the live sessions related to the first part of the course by tackling chapter 4:</p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/1ATVsyBxu1I" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <h2 class="relative group"><a id="workshops" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#workshops"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Workshops</span></h2> <p>In the first workshop, Merve welcomes Lewis to discuss section 7 of chapter 7 about <a href="https://huggingface.co/course/chapter7/7?fw=pt" rel="nofollow">question answering</a>.</p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/Ihgk8kGLpIE" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p>For the second workshop, Merve hosts Leandro to talk about chapter 7, section 6 on <a href="https://huggingface.co/course/chapter7/6?fw=pt" rel="nofollow">training a causal language model from scratch</a> with an application with <a href="https://huggingface.co/codeparrot" rel="nofollow">CodeParrot</a>.</p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/ExUR7w6xe94" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/chapter9/9?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>End-of-chapter quiz</a> <a href="/learn/nlp-course/events/2?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Part 2 release event<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;live-sessions-and-workshops&quot;,&quot;url&quot;:&quot;#live-sessions-and-workshops&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live coding sessions&quot;,&quot;id&quot;:&quot;live-coding-sessions&quot;,&quot;url&quot;:&quot;#live-coding-sessions&quot;},{&quot;title&quot;:&quot;Workshops&quot;,&quot;id&quot;:&quot;workshops&quot;,&quot;url&quot;:&quot;#workshops&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#live-sessions-and-workshops" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-live-sessions-and-workshops"><wbr>Live sessions and workshops</a> <a href="#live-coding-sessions" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-live-coding-sessions"><wbr>Live coding sessions</a> <a href="#workshops" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-workshops"><wbr>Workshops</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/events/1" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/events/1"); } </script> <iframe name="__privateStripeMetricsController9050" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fevents%2F1%3Ffw%3Dpt&amp;title=Live%20sessions%20and%20workshops%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=456ed826-ad66-4181-ad65-25c4ea54cf0fdba01b&amp;sid=e8454723-943e-4f8b-810d-2299133166dec91dc2&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:41.011Z
Gradio Blocks Party - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/events/3?fw=pt
## [](#gradio-blocks-party)Gradio Blocks Party Along with the release of the Gradio chapter of the course, Hugging Face hosted a community event on building cool machine learning demos using the new Gradio Blocks feature. You can find all the demos that the community created under the [`Gradio-Blocks`](https://huggingface.co/Gradio-Blocks) organisation on the Hub. Here’s a few examples from the winners: **Natural language to SQL**
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Gradio Blocks Party - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/events/3"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Gradio Blocks Party - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;gradio-blocks-party&quot;,&quot;title&quot;:&quot;Gradio Blocks Party&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;events/3&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Gradio Blocks Party&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Gradio Blocks Party</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/events/1?fw=pt">Live sessions and workshops </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/events/2?fw=pt">Part 2 release event </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/events/3?fw=pt">Gradio Blocks party </a> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="gradio-blocks-party" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#gradio-blocks-party"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Gradio Blocks Party</span></h1> <p>Along with the release of the Gradio chapter of the course, Hugging Face hosted a community event on building cool machine learning demos using the new Gradio Blocks feature.</p> <p>You can find all the demos that the community created under the <a href="https://huggingface.co/Gradio-Blocks" rel="nofollow"><code>Gradio-Blocks</code></a> organisation on the Hub. Here’s a few examples from the winners:</p> <p><strong>Natural language to SQL</strong></p> <iframe src="https://huggingface.co/spaces/Curranj/Words_To_SQL" frameborder="0" height="640" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/events/2?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Part 2 release event</a> </div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Gradio Blocks Party&quot;,&quot;id&quot;:&quot;gradio-blocks-party&quot;,&quot;url&quot;:&quot;#gradio-blocks-party&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#gradio-blocks-party" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-gradio-blocks-party"><wbr>Gradio <wbr>Blocks <wbr>Party</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/events/3" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/events/3"); } </script> <iframe name="__privateStripeMetricsController8410" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fevents%2F3%3Ffw%3Dpt&amp;title=Gradio%20Blocks%20Party%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:42.821Z
Part 2 Release Event - Hugging Face NLP Course
https://huggingface.co/learn/nlp-course/events/2?fw=pt
## [](#part-2-release-event)Part 2 Release Event For the release of part 2 of the course, we organized a live event with two days of talks before a fine-tuning sprint. If you missed it, you can catch up with the talks which are all listed below! ## [](#day-1-a-high-level-view-of-transformers-and-how-to-train-them)Day 1: A high-level view of Transformers and how to train them **Thomas Wolf:** _Transfer Learning and the birth of the Transformers library_ ![A visual summary of Thom's talk](https://i.imgur.com/9eq8oUi.png) Thomas Wolf is co-founder and Chief Science Officer of Hugging Face. The tools created by Thomas Wolf and the Hugging Face team are used across more than 5,000 research organisations including Facebook Artificial Intelligence Research, Google Research, DeepMind, Amazon Research, Apple, the Allen Institute for Artificial Intelligence as well as most university departments. Thomas Wolf is the initiator and senior chair of the largest research collaboration that has ever existed in Artificial Intelligence: [“BigScience”](https://bigscience.huggingface.co/), as well as a set of widely used [libraries and tools](https://github.com/huggingface/). Thomas Wolf is also a prolific educator, a thought leader in the field of Artificial Intelligence and Natural Language Processing, and a regular invited speaker to conferences all around the world [https://thomwolf.io](https://thomwolf.io/). **Jay Alammar:** _A gentle visual intro to Transformers models_ ![A visual summary of Jay's talk](https://i.imgur.com/rOZAuE9.png) Through his popular ML blog, Jay has helped millions of researchers and engineers visually understand machine learning tools and concepts from the basic (ending up in NumPy, Pandas docs) to the cutting-edge (Transformers, BERT, GPT-3). **Margaret Mitchell:** _On Values in ML Development_ ![A visual summary of Margaret's talk](https://i.imgur.com/NuIsnY3.png) Margaret Mitchell is a researcher working on Ethical AI, currently focused on the ins and outs of ethics-informed AI development in tech. She has published over 50 papers on natural language generation, assistive technology, computer vision, and AI ethics, and holds multiple patents in the areas of conversation generation and sentiment classification. She previously worked at Google AI as a Staff Research Scientist, where she founded and co-led Google’s Ethical AI group, focused on foundational AI ethics research and operationalizing AI ethics Google-internally. Before joining Google, she was a researcher at Microsoft Research, focused on computer vision-to-language generation; and was a postdoc at Johns Hopkins, focused on Bayesian modeling and information extraction. She holds a PhD in Computer Science from the University of Aberdeen and a Master’s in computational linguistics from the University of Washington. While earning her degrees, she also worked from 2005-2012 on machine learning, neurological disorders, and assistive technology at Oregon Health and Science University. She has spearheaded a number of workshops and initiatives at the intersections of diversity, inclusion, computer science, and ethics. Her work has received awards from Secretary of Defense Ash Carter and the American Foundation for the Blind, and has been implemented by multiple technology companies. She likes gardening, dogs, and cats. **Matthew Watson and Chen Qian:** _NLP workflows with Keras_ ![A visual summary of Matt and Chen's talk](https://i.imgur.com/1vD2az8.png) Matthew Watson is a machine learning engineer on the Keras team, with a focus on high-level modeling APIs. He studied Computer Graphics during undergrad and a Masters at Stanford University. An almost English major who turned towards computer science, he is passionate about working across disciplines and making NLP accessible to a wider audience. Chen Qian is a software engineer from Keras team, with a focus on high-level modeling APIs. Chen got a Master degree of Electrical Engineering from Stanford University, and he is especially interested in simplifying code implementations of ML tasks and large-scale ML. **Mark Saroufim:** _How to Train a Model with Pytorch_ ![A visual summary of Mark's talk](https://i.imgur.com/TPmlkm8.png) Mark Saroufim is a Partner Engineer at Pytorch working on OSS production tools including TorchServe and Pytorch Enterprise. In his past lives, Mark was an Applied Scientist and Product Manager at Graphcore, [yuri.ai](http://yuri.ai/), Microsoft and NASA’s JPL. His primary passion is to make programming more fun. **Jakob Uszkoreit:** _It Ain’t Broke So ~Don’t Fix~ Let’s Break It_ ![A visual summary of Jakob's talk](https://i.imgur.com/5dWQeNB.png) Jakob Uszkoreit is the co-founder of Inceptive. Inceptive designs RNA molecules for vaccines and therapeutics using large-scale deep learning in a tight loop with high throughput experiments with the goal of making RNA-based medicines more accessible, more effective and more broadly applicable. Previously, Jakob worked at Google for more than a decade, leading research and development teams in Google Brain, Research and Search working on deep learning fundamentals, computer vision, language understanding and machine translation. ## [](#day-2-the-tools-to-use)Day 2: The tools to use **Lewis Tunstall:** _Simple Training with the 🤗 Transformers Trainer_ Lewis is a machine learning engineer at Hugging Face, focused on developing open-source tools and making them accessible to the wider community. He is also a co-author of the O’Reilly book [Natural Language Processing with Transformers](https://www.oreilly.com/library/view/natural-language-processing/9781098136789/). You can follow him on Twitter (@\_lewtun) for NLP tips and tricks! **Matthew Carrigan:** _New TensorFlow Features for 🤗 Transformers and 🤗 Datasets_ Matt is responsible for TensorFlow maintenance at Transformers, and will eventually lead a coup against the incumbent PyTorch faction which will likely be co-ordinated via his Twitter account @carrigmat. **Lysandre Debut:** _The Hugging Face Hub as a means to collaborate on and share Machine Learning projects_ ![A visual summary of Lysandre's talk](https://i.imgur.com/TarIPCz.png) Lysandre is a Machine Learning Engineer at Hugging Face where he is involved in many open source projects. His aim is to make Machine Learning accessible to everyone by developing powerful tools with a very simple API. **Lucile Saulnier:** _Get your own tokenizer with 🤗 Transformers & 🤗 Tokenizers_ Lucile is a machine learning engineer at Hugging Face, developing and supporting the use of open source tools. She is also actively involved in many research projects in the field of Natural Language Processing such as collaborative training and BigScience. **Sylvain Gugger:** _Supercharge your PyTorch training loop with 🤗 Accelerate_ Sylvain is a Research Engineer at Hugging Face and one of the core maintainers of 🤗 Transformers and the developer behind 🤗 Accelerate. He likes making model training more accessible. **Merve Noyan:** _Showcase your model demos with 🤗 Spaces_ Merve is a developer advocate at Hugging Face, working on developing tools and building content around them to democratize machine learning for everyone. **Abubakar Abid:** _Building Machine Learning Applications Fast_ ![A visual summary of Abubakar's talk](https://i.imgur.com/qWIFeiF.png) Abubakar Abid is the CEO of [Gradio](www.gradio.app). He received his Bachelor’s of Science in Electrical Engineering and Computer Science from MIT in 2015, and his PhD in Applied Machine Learning from Stanford in 2021. In his role as the CEO of Gradio, Abubakar works on making machine learning models easier to demo, debug, and deploy. **Mathieu Desvé:** _AWS ML Vision: Making Machine Learning Accessible to all Customers_ ![A visual summary of Mathieu's talk](https://i.imgur.com/oLdZTKy.png) Technology enthusiast, maker on my free time. I like challenges and solving problem of clients and users, and work with talented people to learn every day. Since 2004, I work in multiple positions switching from frontend, backend, infrastructure, operations and managements. Try to solve commons technical and managerial issues in agile manner. **Philipp Schmid:** _Managed Training with Amazon SageMaker and 🤗 Transformers_ Philipp Schmid is a Machine Learning Engineer and Tech Lead at Hugging Face, where he leads the collaboration with the Amazon SageMaker team. He is passionate about democratizing and productionizing cutting-edge NLP models and improving the ease of use for Deep Learning.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Part 2 Release Event - Hugging Face NLP Course"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/learn/nlp-course/events/2"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/learn/nlp-course.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Part 2 Release Event - Hugging Face NLP Course</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/course/main/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/course/main/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;part-2-release-event&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;day-1-a-high-level-view-of-transformers-and-how-to-train-them&quot;,&quot;title&quot;:&quot;Day 1: A high-level view of Transformers and how to train them&quot;},{&quot;local&quot;:&quot;day-2-the-tools-to-use&quot;,&quot;title&quot;:&quot;Day 2: The tools to use&quot;}],&quot;title&quot;:&quot;Part 2 Release Event&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;0. Setup&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter0/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter0/1?fw=pt&quot;}]},{&quot;title&quot;:&quot;1. Transformer models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter1/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/1?fw=pt&quot;},{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;id&quot;:&quot;chapter1/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/2?fw=pt&quot;},{&quot;title&quot;:&quot;Transformers, what can they do?&quot;,&quot;id&quot;:&quot;chapter1/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/3?fw=pt&quot;},{&quot;title&quot;:&quot;How do Transformers work?&quot;,&quot;id&quot;:&quot;chapter1/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/4?fw=pt&quot;},{&quot;title&quot;:&quot;Encoder models&quot;,&quot;id&quot;:&quot;chapter1/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/5?fw=pt&quot;},{&quot;title&quot;:&quot;Decoder models&quot;,&quot;id&quot;:&quot;chapter1/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/6?fw=pt&quot;},{&quot;title&quot;:&quot;Sequence-to-sequence models&quot;,&quot;id&quot;:&quot;chapter1/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/7?fw=pt&quot;},{&quot;title&quot;:&quot;Bias and limitations&quot;,&quot;id&quot;:&quot;chapter1/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/8?fw=pt&quot;},{&quot;title&quot;:&quot;Summary&quot;,&quot;id&quot;:&quot;chapter1/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:1,&quot;id&quot;:&quot;chapter1/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter1/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;2. Using 🤗 Transformers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter2/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/1?fw=pt&quot;},{&quot;title&quot;:&quot;Behind the pipeline&quot;,&quot;id&quot;:&quot;chapter2/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/2?fw=pt&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;id&quot;:&quot;chapter2/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/3?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers&quot;,&quot;id&quot;:&quot;chapter2/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/4?fw=pt&quot;},{&quot;title&quot;:&quot;Handling multiple sequences&quot;,&quot;id&quot;:&quot;chapter2/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/5?fw=pt&quot;},{&quot;title&quot;:&quot;Putting it all together&quot;,&quot;id&quot;:&quot;chapter2/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/6?fw=pt&quot;},{&quot;title&quot;:&quot;Basic usage completed!&quot;,&quot;id&quot;:&quot;chapter2/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:2,&quot;id&quot;:&quot;chapter2/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter2/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;3. Fine-tuning a pretrained model&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter3/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/1?fw=pt&quot;},{&quot;title&quot;:&quot;Processing the data&quot;,&quot;id&quot;:&quot;chapter3/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a model with the Trainer API or Keras&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter3/3&quot;,&quot;tf&quot;:&quot;chapter3/3_tf&quot;},&quot;id&quot;:&quot;chapter3/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/3?fw=pt&quot;},{&quot;title&quot;:&quot;A full training&quot;,&quot;id&quot;:&quot;chapter3/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/4?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning, Check!&quot;,&quot;id&quot;:&quot;chapter3/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:3,&quot;id&quot;:&quot;chapter3/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter3/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;4. Sharing models and tokenizers&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;The Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter4/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/1?fw=pt&quot;},{&quot;title&quot;:&quot;Using pretrained models&quot;,&quot;id&quot;:&quot;chapter4/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/2?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing pretrained models&quot;,&quot;id&quot;:&quot;chapter4/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/3?fw=pt&quot;},{&quot;title&quot;:&quot;Building a model card&quot;,&quot;id&quot;:&quot;chapter4/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/4?fw=pt&quot;},{&quot;title&quot;:&quot;Part 1 completed!&quot;,&quot;id&quot;:&quot;chapter4/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/5?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:4,&quot;id&quot;:&quot;chapter4/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter4/6?fw=pt&quot;}]},{&quot;title&quot;:&quot;5. The 🤗 Datasets library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter5/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/1?fw=pt&quot;},{&quot;title&quot;:&quot;What if my dataset isn't on the Hub?&quot;,&quot;id&quot;:&quot;chapter5/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/2?fw=pt&quot;},{&quot;title&quot;:&quot;Time to slice and dice&quot;,&quot;id&quot;:&quot;chapter5/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/3?fw=pt&quot;},{&quot;title&quot;:&quot;Big data? 🤗 Datasets to the rescue!&quot;,&quot;id&quot;:&quot;chapter5/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/4?fw=pt&quot;},{&quot;title&quot;:&quot;Creating your own dataset&quot;,&quot;id&quot;:&quot;chapter5/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/5?fw=pt&quot;},{&quot;title&quot;:&quot;Semantic search with FAISS&quot;,&quot;id&quot;:&quot;chapter5/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/6?fw=pt&quot;},{&quot;title&quot;:&quot;🤗 Datasets, check!&quot;,&quot;id&quot;:&quot;chapter5/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/7?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:5,&quot;id&quot;:&quot;chapter5/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter5/8?fw=pt&quot;}]},{&quot;title&quot;:&quot;6. The 🤗 Tokenizers library&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter6/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/1?fw=pt&quot;},{&quot;title&quot;:&quot;Training a new tokenizer from an old one&quot;,&quot;id&quot;:&quot;chapter6/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers' special powers&quot;,&quot;id&quot;:&quot;chapter6/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3?fw=pt&quot;},{&quot;title&quot;:&quot;Fast tokenizers in the QA pipeline&quot;,&quot;id&quot;:&quot;chapter6/3b&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/3b?fw=pt&quot;},{&quot;title&quot;:&quot;Normalization and pre-tokenization&quot;,&quot;id&quot;:&quot;chapter6/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/4?fw=pt&quot;},{&quot;title&quot;:&quot;Byte-Pair Encoding tokenization&quot;,&quot;id&quot;:&quot;chapter6/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/5?fw=pt&quot;},{&quot;title&quot;:&quot;WordPiece tokenization&quot;,&quot;id&quot;:&quot;chapter6/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/6?fw=pt&quot;},{&quot;title&quot;:&quot;Unigram tokenization&quot;,&quot;id&quot;:&quot;chapter6/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/7?fw=pt&quot;},{&quot;title&quot;:&quot;Building a tokenizer, block by block&quot;,&quot;id&quot;:&quot;chapter6/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/8?fw=pt&quot;},{&quot;title&quot;:&quot;Tokenizers, check!&quot;,&quot;id&quot;:&quot;chapter6/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/9?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:6,&quot;id&quot;:&quot;chapter6/10&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter6/10?fw=pt&quot;}]},{&quot;title&quot;:&quot;7. Main NLP tasks&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter7/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/1?fw=pt&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;chapter7/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/2?fw=pt&quot;},{&quot;title&quot;:&quot;Fine-tuning a masked language model&quot;,&quot;id&quot;:&quot;chapter7/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/3?fw=pt&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;chapter7/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/4?fw=pt&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;chapter7/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/5?fw=pt&quot;},{&quot;title&quot;:&quot;Training a causal language model from scratch&quot;,&quot;id&quot;:&quot;chapter7/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/6?fw=pt&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;chapter7/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/7?fw=pt&quot;},{&quot;title&quot;:&quot;Mastering NLP&quot;,&quot;id&quot;:&quot;chapter7/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:7,&quot;id&quot;:&quot;chapter7/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter7/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;8. How to ask for help&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction&quot;,&quot;id&quot;:&quot;chapter8/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/1?fw=pt&quot;},{&quot;title&quot;:&quot;What to do when you get an error&quot;,&quot;id&quot;:&quot;chapter8/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/2?fw=pt&quot;},{&quot;title&quot;:&quot;Asking for help on the forums&quot;,&quot;id&quot;:&quot;chapter8/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/3?fw=pt&quot;},{&quot;title&quot;:&quot;Debugging the training pipeline&quot;,&quot;local_fw&quot;:{&quot;pt&quot;:&quot;chapter8/4&quot;,&quot;tf&quot;:&quot;chapter8/4_tf&quot;},&quot;id&quot;:&quot;chapter8/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/4?fw=pt&quot;},{&quot;title&quot;:&quot;How to write a good issue&quot;,&quot;id&quot;:&quot;chapter8/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/5?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 completed!&quot;,&quot;id&quot;:&quot;chapter8/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/6?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:8,&quot;id&quot;:&quot;chapter8/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter8/7?fw=pt&quot;}]},{&quot;title&quot;:&quot;9. Building and sharing demos&quot;,&quot;new&quot;:true,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Introduction to Gradio&quot;,&quot;id&quot;:&quot;chapter9/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/1?fw=pt&quot;},{&quot;title&quot;:&quot;Building your first demo&quot;,&quot;id&quot;:&quot;chapter9/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/2?fw=pt&quot;},{&quot;title&quot;:&quot;Understanding the Interface class&quot;,&quot;id&quot;:&quot;chapter9/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/3?fw=pt&quot;},{&quot;title&quot;:&quot;Sharing demos with others&quot;,&quot;id&quot;:&quot;chapter9/4&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/4?fw=pt&quot;},{&quot;title&quot;:&quot;Integrations with the Hugging Face Hub&quot;,&quot;id&quot;:&quot;chapter9/5&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/5?fw=pt&quot;},{&quot;title&quot;:&quot;Advanced Interface features&quot;,&quot;id&quot;:&quot;chapter9/6&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/6?fw=pt&quot;},{&quot;title&quot;:&quot;Introduction to Blocks&quot;,&quot;id&quot;:&quot;chapter9/7&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/7?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio, check!&quot;,&quot;id&quot;:&quot;chapter9/8&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/8?fw=pt&quot;},{&quot;title&quot;:&quot;End-of-chapter quiz&quot;,&quot;quiz&quot;:9,&quot;id&quot;:&quot;chapter9/9&quot;,&quot;url&quot;:&quot;/learn/nlp-course/chapter9/9?fw=pt&quot;}]},{&quot;title&quot;:&quot;Course Events&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Live sessions and workshops&quot;,&quot;id&quot;:&quot;events/1&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/1?fw=pt&quot;},{&quot;title&quot;:&quot;Part 2 release event&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;events/2&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/2?fw=pt&quot;},{&quot;title&quot;:&quot;Gradio Blocks party&quot;,&quot;id&quot;:&quot;events/3&quot;,&quot;url&quot;:&quot;/learn/nlp-course/events/3?fw=pt&quot;}]}],&quot;chapterId&quot;:&quot;events/2&quot;,&quot;docType&quot;:&quot;learn&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;ar&quot;,&quot;bn&quot;,&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fa&quot;,&quot;fr&quot;,&quot;gj&quot;,&quot;he&quot;,&quot;hi&quot;,&quot;id&quot;,&quot;it&quot;,&quot;ja&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;ru&quot;,&quot;th&quot;,&quot;tr&quot;,&quot;vi&quot;,&quot;zh-CN&quot;,&quot;zh-TW&quot;],&quot;library&quot;:&quot;nlp-course&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;main&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;}],&quot;title&quot;:&quot;Part 2 Release Event&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">NLP Course documentation</p> <div class="flex items-center"><p class="font-semibold">Part 2 Release Event</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-amber-50 to-white dark:from-gray-900 dark:to-gray-950" style="background: url(https://huggingface.co/datasets/huggingface-course/documentation-images/resolve/main/course-logo.png); background-position: 90% 5%; background-repeat: no-repeat; background-size: 18%;"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-amber-500 flex-none"></div> NLP Course <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="ar">AR</option><option value="bn">BN</option><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fa">FA</option><option value="fr">FR</option><option value="gj">GJ</option><option value="he">HE</option><option value="hi">HI</option><option value="id">ID</option><option value="it">IT</option><option value="ja">JA</option><option value="ko">KO</option><option value="pt">PT</option><option value="ru">RU</option><option value="th">TH</option><option value="tr">TR</option><option value="vi">VI</option><option value="zh-CN">ZH-CN</option><option value="zh-TW">ZH-TW</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/course" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 1,182</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>0. Setup</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>1. Transformer models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>2. Using 🤗 Transformers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>3. Fine-tuning a pretrained model</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>4. Sharing models and tokenizers</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>5. The 🤗 Datasets library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>6. The 🤗 Tokenizers library</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>7. Main NLP tasks</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>8. How to ask for help</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>9. Building and sharing demos</span> <span class="ml-1 rounded bg-yellow-200 px-1 text-xs text-yellow-900 dark:bg-yellow-500">new</span></span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Course Events</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/events/1?fw=pt">Live sessions and workshops </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/learn/nlp-course/events/2?fw=pt">Part 2 release event </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/learn/nlp-course/events/3?fw=pt">Gradio Blocks party </a> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="part-2-release-event" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#part-2-release-event"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Part 2 Release Event</span></h1> <p>For the release of part 2 of the course, we organized a live event with two days of talks before a fine-tuning sprint. If you missed it, you can catch up with the talks which are all listed below!</p> <h2 class="relative group"><a id="day-1-a-high-level-view-of-transformers-and-how-to-train-them" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#day-1-a-high-level-view-of-transformers-and-how-to-train-them"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Day 1: A high-level view of Transformers and how to train them</span></h2> <p><strong>Thomas Wolf:</strong> <em>Transfer Learning and the birth of the Transformers library</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/wCYVeahJES0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p align="center"><img src="https://i.imgur.com/9eq8oUi.png" alt="A visual summary of Thom's talk" width="80%"></p> <p>Thomas Wolf is co-founder and Chief Science Officer of Hugging Face. The tools created by Thomas Wolf and the Hugging Face team are used across more than 5,000 research organisations including Facebook Artificial Intelligence Research, Google Research, DeepMind, Amazon Research, Apple, the Allen Institute for Artificial Intelligence as well as most university departments. Thomas Wolf is the initiator and senior chair of the largest research collaboration that has ever existed in Artificial Intelligence: <a href="https://bigscience.huggingface.co" rel="nofollow">“BigScience”</a>, as well as a set of widely used <a href="https://github.com/huggingface/" rel="nofollow">libraries and tools</a>. Thomas Wolf is also a prolific educator, a thought leader in the field of Artificial Intelligence and Natural Language Processing, and a regular invited speaker to conferences all around the world <a href="https://thomwolf.io" rel="nofollow">https://thomwolf.io</a>.</p> <p><strong>Jay Alammar:</strong> <em>A gentle visual intro to Transformers models</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/VzvG23gmcYU" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p align="center"><img src="https://i.imgur.com/rOZAuE9.png" alt="A visual summary of Jay's talk" width="80%"></p> <p>Through his popular ML blog, Jay has helped millions of researchers and engineers visually understand machine learning tools and concepts from the basic (ending up in NumPy, Pandas docs) to the cutting-edge (Transformers, BERT, GPT-3).</p> <p><strong>Margaret Mitchell:</strong> <em>On Values in ML Development</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/8j9HRMjh_s8" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p align="center"><img src="https://i.imgur.com/NuIsnY3.png" alt="A visual summary of Margaret's talk" width="80%"></p> <p>Margaret Mitchell is a researcher working on Ethical AI, currently focused on the ins and outs of ethics-informed AI development in tech. She has published over 50 papers on natural language generation, assistive technology, computer vision, and AI ethics, and holds multiple patents in the areas of conversation generation and sentiment classification. She previously worked at Google AI as a Staff Research Scientist, where she founded and co-led Google’s Ethical AI group, focused on foundational AI ethics research and operationalizing AI ethics Google-internally. Before joining Google, she was a researcher at Microsoft Research, focused on computer vision-to-language generation; and was a postdoc at Johns Hopkins, focused on Bayesian modeling and information extraction. She holds a PhD in Computer Science from the University of Aberdeen and a Master’s in computational linguistics from the University of Washington. While earning her degrees, she also worked from 2005-2012 on machine learning, neurological disorders, and assistive technology at Oregon Health and Science University. She has spearheaded a number of workshops and initiatives at the intersections of diversity, inclusion, computer science, and ethics. Her work has received awards from Secretary of Defense Ash Carter and the American Foundation for the Blind, and has been implemented by multiple technology companies. She likes gardening, dogs, and cats.</p> <p><strong>Matthew Watson and Chen Qian:</strong> <em>NLP workflows with Keras</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/gZIP-_2XYMM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p align="center"><img src="https://i.imgur.com/1vD2az8.png" alt="A visual summary of Matt and Chen's talk" width="80%"></p> <p>Matthew Watson is a machine learning engineer on the Keras team, with a focus on high-level modeling APIs. He studied Computer Graphics during undergrad and a Masters at Stanford University. An almost English major who turned towards computer science, he is passionate about working across disciplines and making NLP accessible to a wider audience.</p> <p>Chen Qian is a software engineer from Keras team, with a focus on high-level modeling APIs. Chen got a Master degree of Electrical Engineering from Stanford University, and he is especially interested in simplifying code implementations of ML tasks and large-scale ML.</p> <p><strong>Mark Saroufim:</strong> <em>How to Train a Model with Pytorch</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/KmvPlW2cbIo" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p align="center"><img src="https://i.imgur.com/TPmlkm8.png" alt="A visual summary of Mark's talk" width="80%"></p> <p>Mark Saroufim is a Partner Engineer at Pytorch working on OSS production tools including TorchServe and Pytorch Enterprise. In his past lives, Mark was an Applied Scientist and Product Manager at Graphcore, <a href="http://yuri.ai/" rel="nofollow">yuri.ai</a>, Microsoft and NASA’s JPL. His primary passion is to make programming more fun.</p> <p><strong>Jakob Uszkoreit:</strong> <em>It Ain’t Broke So <del>Don’t Fix</del> Let’s Break It</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/C6jweXYFHSA" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p align="center"><img src="https://i.imgur.com/5dWQeNB.png" alt="A visual summary of Jakob's talk" width="80%"></p> <p>Jakob Uszkoreit is the co-founder of Inceptive. Inceptive designs RNA molecules for vaccines and therapeutics using large-scale deep learning in a tight loop with high throughput experiments with the goal of making RNA-based medicines more accessible, more effective and more broadly applicable. Previously, Jakob worked at Google for more than a decade, leading research and development teams in Google Brain, Research and Search working on deep learning fundamentals, computer vision, language understanding and machine translation.</p> <h2 class="relative group"><a id="day-2-the-tools-to-use" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#day-2-the-tools-to-use"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Day 2: The tools to use</span></h2> <p><strong>Lewis Tunstall:</strong> <em>Simple Training with the 🤗 Transformers Trainer</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/u--UVvH-LIQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p>Lewis is a machine learning engineer at Hugging Face, focused on developing open-source tools and making them accessible to the wider community. He is also a co-author of the O’Reilly book <a href="https://www.oreilly.com/library/view/natural-language-processing/9781098136789/" rel="nofollow">Natural Language Processing with Transformers</a>. You can follow him on Twitter (@_lewtun) for NLP tips and tricks!</p> <p><strong>Matthew Carrigan:</strong> <em>New TensorFlow Features for 🤗 Transformers and 🤗 Datasets</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/gQUlXp1691w" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p>Matt is responsible for TensorFlow maintenance at Transformers, and will eventually lead a coup against the incumbent PyTorch faction which will likely be co-ordinated via his Twitter account @carrigmat.</p> <p><strong>Lysandre Debut:</strong> <em>The Hugging Face Hub as a means to collaborate on and share Machine Learning projects</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/RBw1TmdEZp0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p align="center"><img src="https://i.imgur.com/TarIPCz.png" alt="A visual summary of Lysandre's talk" width="80%"></p> <p>Lysandre is a Machine Learning Engineer at Hugging Face where he is involved in many open source projects. His aim is to make Machine Learning accessible to everyone by developing powerful tools with a very simple API.</p> <p><strong>Lucile Saulnier:</strong> <em>Get your own tokenizer with 🤗 Transformers &amp; 🤗 Tokenizers</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/UkNmyTFKriI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p>Lucile is a machine learning engineer at Hugging Face, developing and supporting the use of open source tools. She is also actively involved in many research projects in the field of Natural Language Processing such as collaborative training and BigScience.</p> <p><strong>Sylvain Gugger:</strong> <em>Supercharge your PyTorch training loop with 🤗 Accelerate</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/t8Krzu-nSeY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p>Sylvain is a Research Engineer at Hugging Face and one of the core maintainers of 🤗 Transformers and the developer behind 🤗 Accelerate. He likes making model training more accessible.</p> <p><strong>Merve Noyan:</strong> <em>Showcase your model demos with 🤗 Spaces</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/vbaKOa4UXoM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p>Merve is a developer advocate at Hugging Face, working on developing tools and building content around them to democratize machine learning for everyone.</p> <p><strong>Abubakar Abid:</strong> <em>Building Machine Learning Applications Fast</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/c7mle2yYpwQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p align="center"><img src="https://i.imgur.com/qWIFeiF.png" alt="A visual summary of Abubakar's talk" width="80%"></p> <p>Abubakar Abid is the CEO of <a href="www.gradio.app">Gradio</a>. He received his Bachelor’s of Science in Electrical Engineering and Computer Science from MIT in 2015, and his PhD in Applied Machine Learning from Stanford in 2021. In his role as the CEO of Gradio, Abubakar works on making machine learning models easier to demo, debug, and deploy.</p> <p><strong>Mathieu Desvé:</strong> <em>AWS ML Vision: Making Machine Learning Accessible to all Customers</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/O2e3pXO4aRE" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p align="center"><img src="https://i.imgur.com/oLdZTKy.png" alt="A visual summary of Mathieu's talk" width="80%"></p> <p>Technology enthusiast, maker on my free time. I like challenges and solving problem of clients and users, and work with talented people to learn every day. Since 2004, I work in multiple positions switching from frontend, backend, infrastructure, operations and managements. Try to solve commons technical and managerial issues in agile manner.</p> <p><strong>Philipp Schmid:</strong> <em>Managed Training with Amazon SageMaker and 🤗 Transformers</em></p> <div class="flex justify-center"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/yG6J2Zfo8iw" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div> <p>Philipp Schmid is a Machine Learning Engineer and Tech Lead at Hugging Face, where he leads the collaboration with the Amazon SageMaker team. He is passionate about democratizing and productionizing cutting-edge NLP models and improving the ease of use for Deep Learning.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/learn/nlp-course/events/1?fw=pt" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Live sessions and workshops</a> <a href="/learn/nlp-course/events/3?fw=pt" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Gradio Blocks party<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Part 2 Release Event&quot;,&quot;isExpanded&quot;:false,&quot;id&quot;:&quot;part-2-release-event&quot;,&quot;url&quot;:&quot;#part-2-release-event&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Day 1: A high-level view of Transformers and how to train them&quot;,&quot;id&quot;:&quot;day-1-a-high-level-view-of-transformers-and-how-to-train-them&quot;,&quot;url&quot;:&quot;#day-1-a-high-level-view-of-transformers-and-how-to-train-them&quot;},{&quot;title&quot;:&quot;Day 2: The tools to use&quot;,&quot;id&quot;:&quot;day-2-the-tools-to-use&quot;,&quot;url&quot;:&quot;#day-2-the-tools-to-use&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#part-2-release-event" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-part-2-release-event"><wbr>Part 2 <wbr>Release <wbr>Event</a> <a href="#day-1-a-high-level-view-of-transformers-and-how-to-train-them" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-day-1-a-high-level-view-of-transformers-and-how-to-train-them"><wbr>Day 1: <wbr>A high-level view of <wbr>Transformers and how to train them</a> <a href="#day-2-the-tools-to-use" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-day-2-the-tools-to-use"><wbr>Day 2: <wbr>The tools to use</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/learn/nlp-course/events/2" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/learn/nlp-course/events/2"); } </script> <iframe name="__privateStripeMetricsController1390" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Flearn%2Fnlp-course%2Fevents%2F2%3Ffw%3Dpt&amp;title=Part%202%20Release%20Event%20-%20Hugging%20Face%20NLP%20Course&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T20:00:44.492Z
🤗 Transformers
https://huggingface.co/docs/transformers/index
## [](#transformers)🤗 Transformers State-of-the-art Machine Learning for [PyTorch](https://pytorch.org/), [TensorFlow](https://www.tensorflow.org/), and [JAX](https://jax.readthedocs.io/en/latest/). 🤗 Transformers provides APIs and tools to easily download and train state-of-the-art pretrained models. Using pretrained models can reduce your compute costs, carbon footprint, and save you the time and resources required to train a model from scratch. These models support common tasks in different modalities, such as: 📝 **Natural Language Processing**: text classification, named entity recognition, question answering, language modeling, summarization, translation, multiple choice, and text generation. 🖼️ **Computer Vision**: image classification, object detection, and segmentation. 🗣️ **Audio**: automatic speech recognition and audio classification. 🐙 **Multimodal**: table question answering, optical character recognition, information extraction from scanned documents, video classification, and visual question answering. 🤗 Transformers support framework interoperability between PyTorch, TensorFlow, and JAX. This provides the flexibility to use a different framework at each stage of a model’s life; train a model in three lines of code in one framework, and load it for inference in another. Models can also be exported to a format like ONNX and TorchScript for deployment in production environments. Join the growing community on the [Hub](https://huggingface.co/models), [forum](https://discuss.huggingface.co/), or [Discord](https://discord.com/invite/JfAtkvEtRb) today! ## [](#if-you-are-looking-for-custom-support-from-the-hugging-face-team)If you are looking for custom support from the Hugging Face team [![HuggingFace Expert Acceleration Program](https://cdn-media.huggingface.co/marketing/transformers/new-support-improved.png)](https://huggingface.co/support) ## [](#contents)Contents The documentation is organized into five sections: - **GET STARTED** provides a quick tour of the library and installation instructions to get up and running. - **TUTORIALS** are a great place to start if you’re a beginner. This section will help you gain the basic skills you need to start using the library. - **HOW-TO GUIDES** show you how to achieve a specific goal, like finetuning a pretrained model for language modeling or how to write and share a custom model. - **CONCEPTUAL GUIDES** offers more discussion and explanation of the underlying concepts and ideas behind models, tasks, and the design philosophy of 🤗 Transformers. - **API** describes all classes and functions: - **MAIN CLASSES** details the most important classes like configuration, model, tokenizer, and pipeline. - **MODELS** details the classes and functions related to each model implemented in the library. - **INTERNAL HELPERS** details utility classes and functions used internally. ### [](#supported-models)Supported models 1. **[ALBERT](model_doc/albert)** (from Google Research and the Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut. 2. **[ALIGN](model_doc/align)** (from Google Research) released with the paper [Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision](https://arxiv.org/abs/2102.05918) by Chao Jia, Yinfei Yang, Ye Xia, Yi-Ting Chen, Zarana Parekh, Hieu Pham, Quoc V. Le, Yunhsuan Sung, Zhen Li, Tom Duerig. 3. **[AltCLIP](model_doc/altclip)** (from BAAI) released with the paper [AltCLIP: Altering the Language Encoder in CLIP for Extended Language Capabilities](https://arxiv.org/abs/2211.06679) by Chen, Zhongzhi and Liu, Guang and Zhang, Bo-Wen and Ye, Fulong and Yang, Qinghong and Wu, Ledell. 4. **[Audio Spectrogram Transformer](model_doc/audio-spectrogram-transformer)** (from MIT) released with the paper [AST: Audio Spectrogram Transformer](https://arxiv.org/abs/2104.01778) by Yuan Gong, Yu-An Chung, James Glass. 5. **[Autoformer](model_doc/autoformer)** (from Tsinghua University) released with the paper [Autoformer: Decomposition Transformers with Auto-Correlation for Long-Term Series Forecasting](https://arxiv.org/abs/2106.13008) by Haixu Wu, Jiehui Xu, Jianmin Wang, Mingsheng Long. 6. **[BART](model_doc/bart)** (from Facebook) released with the paper [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension](https://arxiv.org/abs/1910.13461) by Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer. 7. **[BARThez](model_doc/barthez)** (from École polytechnique) released with the paper [BARThez: a Skilled Pretrained French Sequence-to-Sequence Model](https://arxiv.org/abs/2010.12321) by Moussa Kamal Eddine, Antoine J.-P. Tixier, Michalis Vazirgiannis. 8. **[BARTpho](model_doc/bartpho)** (from VinAI Research) released with the paper [BARTpho: Pre-trained Sequence-to-Sequence Models for Vietnamese](https://arxiv.org/abs/2109.09701) by Nguyen Luong Tran, Duong Minh Le and Dat Quoc Nguyen. 9. **[BEiT](model_doc/beit)** (from Microsoft) released with the paper [BEiT: BERT Pre-Training of Image Transformers](https://arxiv.org/abs/2106.08254) by Hangbo Bao, Li Dong, Furu Wei. 10. **[BERT](model_doc/bert)** (from Google) released with the paper [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805) by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova. 11. **[BERT For Sequence Generation](model_doc/bert-generation)** (from Google) released with the paper [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://arxiv.org/abs/1907.12461) by Sascha Rothe, Shashi Narayan, Aliaksei Severyn. 12. **[BERTweet](model_doc/bertweet)** (from VinAI Research) released with the paper [BERTweet: A pre-trained language model for English Tweets](https://aclanthology.org/2020.emnlp-demos.2/) by Dat Quoc Nguyen, Thanh Vu and Anh Tuan Nguyen. 13. **[BigBird-Pegasus](model_doc/bigbird_pegasus)** (from Google Research) released with the paper [Big Bird: Transformers for Longer Sequences](https://arxiv.org/abs/2007.14062) by Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed. 14. **[BigBird-RoBERTa](model_doc/big_bird)** (from Google Research) released with the paper [Big Bird: Transformers for Longer Sequences](https://arxiv.org/abs/2007.14062) by Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed. 15. **[BioGpt](model_doc/biogpt)** (from Microsoft Research AI4Science) released with the paper [BioGPT: generative pre-trained transformer for biomedical text generation and mining](https://academic.oup.com/bib/advance-article/doi/10.1093/bib/bbac409/6713511?guestAccessKey=a66d9b5d-4f83-4017-bb52-405815c907b9) by Renqian Luo, Liai Sun, Yingce Xia, Tao Qin, Sheng Zhang, Hoifung Poon and Tie-Yan Liu. 16. **[BiT](model_doc/bit)** (from Google AI) released with the paper [Big Transfer (BiT): General Visual Representation Learning](https://arxiv.org/abs/1912.11370) by Alexander Kolesnikov, Lucas Beyer, Xiaohua Zhai, Joan Puigcerver, Jessica Yung, Sylvain Gelly, Neil Houlsby. 17. **[Blenderbot](model_doc/blenderbot)** (from Facebook) released with the paper [Recipes for building an open-domain chatbot](https://arxiv.org/abs/2004.13637) by Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston. 18. **[BlenderbotSmall](model_doc/blenderbot-small)** (from Facebook) released with the paper [Recipes for building an open-domain chatbot](https://arxiv.org/abs/2004.13637) by Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston. 19. **[BLIP](model_doc/blip)** (from Salesforce) released with the paper [BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation](https://arxiv.org/abs/2201.12086) by Junnan Li, Dongxu Li, Caiming Xiong, Steven Hoi. 20. **[BLIP-2](model_doc/blip-2)** (from Salesforce) released with the paper [BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models](https://arxiv.org/abs/2301.12597) by Junnan Li, Dongxu Li, Silvio Savarese, Steven Hoi. 21. **[BLOOM](model_doc/bloom)** (from BigScience workshop) released by the [BigScience Workshop](https://bigscience.huggingface.co/). 22. **[BORT](model_doc/bort)** (from Alexa) released with the paper [Optimal Subarchitecture Extraction For BERT](https://arxiv.org/abs/2010.10499) by Adrian de Wynter and Daniel J. Perry. 23. **[BridgeTower](model_doc/bridgetower)** (from Harbin Institute of Technology/Microsoft Research Asia/Intel Labs) released with the paper [BridgeTower: Building Bridges Between Encoders in Vision-Language Representation Learning](https://arxiv.org/abs/2206.08657) by Xiao Xu, Chenfei Wu, Shachar Rosenman, Vasudev Lal, Wanxiang Che, Nan Duan. 24. **[ByT5](model_doc/byt5)** (from Google Research) released with the paper [ByT5: Towards a token-free future with pre-trained byte-to-byte models](https://arxiv.org/abs/2105.13626) by Linting Xue, Aditya Barua, Noah Constant, Rami Al-Rfou, Sharan Narang, Mihir Kale, Adam Roberts, Colin Raffel. 25. **[CamemBERT](model_doc/camembert)** (from Inria/Facebook/Sorbonne) released with the paper [CamemBERT: a Tasty French Language Model](https://arxiv.org/abs/1911.03894) by Louis Martin_, Benjamin Muller_, Pedro Javier Ortiz Suárez\*, Yoann Dupont, Laurent Romary, Éric Villemonte de la Clergerie, Djamé Seddah and Benoît Sagot. 26. **[CANINE](model_doc/canine)** (from Google Research) released with the paper [CANINE: Pre-training an Efficient Tokenization-Free Encoder for Language Representation](https://arxiv.org/abs/2103.06874) by Jonathan H. Clark, Dan Garrette, Iulia Turc, John Wieting. 27. **[Chinese-CLIP](model_doc/chinese_clip)** (from OFA-Sys) released with the paper [Chinese CLIP: Contrastive Vision-Language Pretraining in Chinese](https://arxiv.org/abs/2211.01335) by An Yang, Junshu Pan, Junyang Lin, Rui Men, Yichang Zhang, Jingren Zhou, Chang Zhou. 28. **[CLAP](model_doc/clap)** (from LAION-AI) released with the paper [Large-scale Contrastive Language-Audio Pretraining with Feature Fusion and Keyword-to-Caption Augmentation](https://arxiv.org/abs/2211.06687) by Yusong Wu, Ke Chen, Tianyu Zhang, Yuchen Hui, Taylor Berg-Kirkpatrick, Shlomo Dubnov. 29. **[CLIP](model_doc/clip)** (from OpenAI) released with the paper [Learning Transferable Visual Models From Natural Language Supervision](https://arxiv.org/abs/2103.00020) by Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever. 30. **[CLIPSeg](model_doc/clipseg)** (from University of Göttingen) released with the paper [Image Segmentation Using Text and Image Prompts](https://arxiv.org/abs/2112.10003) by Timo Lüddecke and Alexander Ecker. 31. **[CodeGen](model_doc/codegen)** (from Salesforce) released with the paper [A Conversational Paradigm for Program Synthesis](https://arxiv.org/abs/2203.13474) by Erik Nijkamp, Bo Pang, Hiroaki Hayashi, Lifu Tu, Huan Wang, Yingbo Zhou, Silvio Savarese, Caiming Xiong. 32. **[Conditional DETR](model_doc/conditional_detr)** (from Microsoft Research Asia) released with the paper [Conditional DETR for Fast Training Convergence](https://arxiv.org/abs/2108.06152) by Depu Meng, Xiaokang Chen, Zejia Fan, Gang Zeng, Houqiang Li, Yuhui Yuan, Lei Sun, Jingdong Wang. 33. **[ConvBERT](model_doc/convbert)** (from YituTech) released with the paper [ConvBERT: Improving BERT with Span-based Dynamic Convolution](https://arxiv.org/abs/2008.02496) by Zihang Jiang, Weihao Yu, Daquan Zhou, Yunpeng Chen, Jiashi Feng, Shuicheng Yan. 34. **[ConvNeXT](model_doc/convnext)** (from Facebook AI) released with the paper [A ConvNet for the 2020s](https://arxiv.org/abs/2201.03545) by Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie. 35. **[ConvNeXTV2](model_doc/convnextv2)** (from Facebook AI) released with the paper [ConvNeXt V2: Co-designing and Scaling ConvNets with Masked Autoencoders](https://arxiv.org/abs/2301.00808) by Sanghyun Woo, Shoubhik Debnath, Ronghang Hu, Xinlei Chen, Zhuang Liu, In So Kweon, Saining Xie. 36. **[CPM](model_doc/cpm)** (from Tsinghua University) released with the paper [CPM: A Large-scale Generative Chinese Pre-trained Language Model](https://arxiv.org/abs/2012.00413) by Zhengyan Zhang, Xu Han, Hao Zhou, Pei Ke, Yuxian Gu, Deming Ye, Yujia Qin, Yusheng Su, Haozhe Ji, Jian Guan, Fanchao Qi, Xiaozhi Wang, Yanan Zheng, Guoyang Zeng, Huanqi Cao, Shengqi Chen, Daixuan Li, Zhenbo Sun, Zhiyuan Liu, Minlie Huang, Wentao Han, Jie Tang, Juanzi Li, Xiaoyan Zhu, Maosong Sun. 37. **[CPM-Ant](model_doc/cpmant)** (from OpenBMB) released by the [OpenBMB](https://www.openbmb.org/). 38. **[CTRL](model_doc/ctrl)** (from Salesforce) released with the paper [CTRL: A Conditional Transformer Language Model for Controllable Generation](https://arxiv.org/abs/1909.05858) by Nitish Shirish Keskar_, Bryan McCann_, Lav R. Varshney, Caiming Xiong and Richard Socher. 39. **[CvT](model_doc/cvt)** (from Microsoft) released with the paper [CvT: Introducing Convolutions to Vision Transformers](https://arxiv.org/abs/2103.15808) by Haiping Wu, Bin Xiao, Noel Codella, Mengchen Liu, Xiyang Dai, Lu Yuan, Lei Zhang. 40. **[Data2Vec](model_doc/data2vec)** (from Facebook) released with the paper [Data2Vec: A General Framework for Self-supervised Learning in Speech, Vision and Language](https://arxiv.org/abs/2202.03555) by Alexei Baevski, Wei-Ning Hsu, Qiantong Xu, Arun Babu, Jiatao Gu, Michael Auli. 41. **[DeBERTa](model_doc/deberta)** (from Microsoft) released with the paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://arxiv.org/abs/2006.03654) by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen. 42. **[DeBERTa-v2](model_doc/deberta-v2)** (from Microsoft) released with the paper [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://arxiv.org/abs/2006.03654) by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen. 43. **[Decision Transformer](model_doc/decision_transformer)** (from Berkeley/Facebook/Google) released with the paper [Decision Transformer: Reinforcement Learning via Sequence Modeling](https://arxiv.org/abs/2106.01345) by Lili Chen, Kevin Lu, Aravind Rajeswaran, Kimin Lee, Aditya Grover, Michael Laskin, Pieter Abbeel, Aravind Srinivas, Igor Mordatch. 44. **[Deformable DETR](model_doc/deformable_detr)** (from SenseTime Research) released with the paper [Deformable DETR: Deformable Transformers for End-to-End Object Detection](https://arxiv.org/abs/2010.04159) by Xizhou Zhu, Weijie Su, Lewei Lu, Bin Li, Xiaogang Wang, Jifeng Dai. 45. **[DeiT](model_doc/deit)** (from Facebook) released with the paper [Training data-efficient image transformers & distillation through attention](https://arxiv.org/abs/2012.12877) by Hugo Touvron, Matthieu Cord, Matthijs Douze, Francisco Massa, Alexandre Sablayrolles, Hervé Jégou. 46. **[DePlot](model_doc/deplot)** (from Google AI) released with the paper [DePlot: One-shot visual language reasoning by plot-to-table translation](https://arxiv.org/abs/2212.10505) by Fangyu Liu, Julian Martin Eisenschlos, Francesco Piccinno, Syrine Krichene, Chenxi Pang, Kenton Lee, Mandar Joshi, Wenhu Chen, Nigel Collier, Yasemin Altun. 47. **[DETA](model_doc/deta)** (from The University of Texas at Austin) released with the paper [NMS Strikes Back](https://arxiv.org/abs/2212.06137) by Jeffrey Ouyang-Zhang, Jang Hyun Cho, Xingyi Zhou, Philipp Krähenbühl. 48. **[DETR](model_doc/detr)** (from Facebook) released with the paper [End-to-End Object Detection with Transformers](https://arxiv.org/abs/2005.12872) by Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov, Sergey Zagoruyko. 49. **[DialoGPT](model_doc/dialogpt)** (from Microsoft Research) released with the paper [DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation](https://arxiv.org/abs/1911.00536) by Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao, Jianfeng Gao, Jingjing Liu, Bill Dolan. 50. **[DiNAT](model_doc/dinat)** (from SHI Labs) released with the paper [Dilated Neighborhood Attention Transformer](https://arxiv.org/abs/2209.15001) by Ali Hassani and Humphrey Shi. 51. **[DistilBERT](model_doc/distilbert)** (from HuggingFace), released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation), RoBERTa into [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation), Multilingual BERT into [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation) and a German version of DistilBERT. 52. **[DiT](model_doc/dit)** (from Microsoft Research) released with the paper [DiT: Self-supervised Pre-training for Document Image Transformer](https://arxiv.org/abs/2203.02378) by Junlong Li, Yiheng Xu, Tengchao Lv, Lei Cui, Cha Zhang, Furu Wei. 53. **[Donut](model_doc/donut)** (from NAVER), released together with the paper [OCR-free Document Understanding Transformer](https://arxiv.org/abs/2111.15664) by Geewook Kim, Teakgyu Hong, Moonbin Yim, Jeongyeon Nam, Jinyoung Park, Jinyeong Yim, Wonseok Hwang, Sangdoo Yun, Dongyoon Han, Seunghyun Park. 54. **[DPR](model_doc/dpr)** (from Facebook) released with the paper [Dense Passage Retrieval for Open-Domain Question Answering](https://arxiv.org/abs/2004.04906) by Vladimir Karpukhin, Barlas Oğuz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, and Wen-tau Yih. 55. **[DPT](master/model_doc/dpt)** (from Intel Labs) released with the paper [Vision Transformers for Dense Prediction](https://arxiv.org/abs/2103.13413) by René Ranftl, Alexey Bochkovskiy, Vladlen Koltun. 56. **[EfficientFormer](model_doc/efficientformer)** (from Snap Research) released with the paper [EfficientFormer: Vision Transformers at MobileNetSpeed](https://arxiv.org/abs/2206.01191) by Yanyu Li, Geng Yuan, Yang Wen, Ju Hu, Georgios Evangelidis, Sergey Tulyakov, Yanzhi Wang, Jian Ren. 57. **[EfficientNet](model_doc/efficientnet)** (from Google Brain) released with the paper [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](https://arxiv.org/abs/1905.11946) by Mingxing Tan, Quoc V. Le. 58. **[ELECTRA](model_doc/electra)** (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning. 59. **[EncoderDecoder](model_doc/encoder-decoder)** (from Google Research) released with the paper [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://arxiv.org/abs/1907.12461) by Sascha Rothe, Shashi Narayan, Aliaksei Severyn. 60. **[ERNIE](model_doc/ernie)** (from Baidu) released with the paper [ERNIE: Enhanced Representation through Knowledge Integration](https://arxiv.org/abs/1904.09223) by Yu Sun, Shuohuan Wang, Yukun Li, Shikun Feng, Xuyi Chen, Han Zhang, Xin Tian, Danxiang Zhu, Hao Tian, Hua Wu. 61. **[ErnieM](model_doc/ernie_m)** (from Baidu) released with the paper [ERNIE-M: Enhanced Multilingual Representation by Aligning Cross-lingual Semantics with Monolingual Corpora](https://arxiv.org/abs/2012.15674) by Xuan Ouyang, Shuohuan Wang, Chao Pang, Yu Sun, Hao Tian, Hua Wu, Haifeng Wang. 62. **[ESM](model_doc/esm)** (from Meta AI) are transformer protein language models. **ESM-1b** was released with the paper [Biological structure and function emerge from scaling unsupervised learning to 250 million protein sequences](https://www.pnas.org/content/118/15/e2016239118) by Alexander Rives, Joshua Meier, Tom Sercu, Siddharth Goyal, Zeming Lin, Jason Liu, Demi Guo, Myle Ott, C. Lawrence Zitnick, Jerry Ma, and Rob Fergus. **ESM-1v** was released with the paper [Language models enable zero-shot prediction of the effects of mutations on protein function](https://doi.org/10.1101/2021.07.09.450648) by Joshua Meier, Roshan Rao, Robert Verkuil, Jason Liu, Tom Sercu and Alexander Rives. **ESM-2 and ESMFold** were released with the paper [Language models of protein sequences at the scale of evolution enable accurate structure prediction](https://doi.org/10.1101/2022.07.20.500902) by Zeming Lin, Halil Akin, Roshan Rao, Brian Hie, Zhongkai Zhu, Wenting Lu, Allan dos Santos Costa, Maryam Fazel-Zarandi, Tom Sercu, Sal Candido, Alexander Rives. 63. **[FLAN-T5](model_doc/flan-t5)** (from Google AI) released in the repository [google-research/t5x](https://github.com/google-research/t5x/blob/main/docs/models.md#flan-t5-checkpoints) by Hyung Won Chung, Le Hou, Shayne Longpre, Barret Zoph, Yi Tay, William Fedus, Eric Li, Xuezhi Wang, Mostafa Dehghani, Siddhartha Brahma, Albert Webson, Shixiang Shane Gu, Zhuyun Dai, Mirac Suzgun, Xinyun Chen, Aakanksha Chowdhery, Sharan Narang, Gaurav Mishra, Adams Yu, Vincent Zhao, Yanping Huang, Andrew Dai, Hongkun Yu, Slav Petrov, Ed H. Chi, Jeff Dean, Jacob Devlin, Adam Roberts, Denny Zhou, Quoc V. Le, and Jason Wei 64. **[FLAN-UL2](model_doc/flan-ul2)** (from Google AI) released in the repository [google-research/t5x](https://github.com/google-research/t5x/blob/main/docs/models.md#flan-ul2-checkpoints) by Hyung Won Chung, Le Hou, Shayne Longpre, Barret Zoph, Yi Tay, William Fedus, Eric Li, Xuezhi Wang, Mostafa Dehghani, Siddhartha Brahma, Albert Webson, Shixiang Shane Gu, Zhuyun Dai, Mirac Suzgun, Xinyun Chen, Aakanksha Chowdhery, Sharan Narang, Gaurav Mishra, Adams Yu, Vincent Zhao, Yanping Huang, Andrew Dai, Hongkun Yu, Slav Petrov, Ed H. Chi, Jeff Dean, Jacob Devlin, Adam Roberts, Denny Zhou, Quoc V. Le, and Jason Wei 65. **[FlauBERT](model_doc/flaubert)** (from CNRS) released with the paper [FlauBERT: Unsupervised Language Model Pre-training for French](https://arxiv.org/abs/1912.05372) by Hang Le, Loïc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoît Crabbé, Laurent Besacier, Didier Schwab. 66. **[FLAVA](model_doc/flava)** (from Facebook AI) released with the paper [FLAVA: A Foundational Language And Vision Alignment Model](https://arxiv.org/abs/2112.04482) by Amanpreet Singh, Ronghang Hu, Vedanuj Goswami, Guillaume Couairon, Wojciech Galuba, Marcus Rohrbach, and Douwe Kiela. 67. **[FNet](model_doc/fnet)** (from Google Research) released with the paper [FNet: Mixing Tokens with Fourier Transforms](https://arxiv.org/abs/2105.03824) by James Lee-Thorp, Joshua Ainslie, Ilya Eckstein, Santiago Ontanon. 68. **[FocalNet](model_doc/focalnet)** (from Microsoft Research) released with the paper [Focal Modulation Networks](https://arxiv.org/abs/2203.11926) by Jianwei Yang, Chunyuan Li, Xiyang Dai, Lu Yuan, Jianfeng Gao. 69. **[Funnel Transformer](model_doc/funnel)** (from CMU/Google Brain) released with the paper [Funnel-Transformer: Filtering out Sequential Redundancy for Efficient Language Processing](https://arxiv.org/abs/2006.03236) by Zihang Dai, Guokun Lai, Yiming Yang, Quoc V. Le. 70. **[GIT](model_doc/git)** (from Microsoft Research) released with the paper [GIT: A Generative Image-to-text Transformer for Vision and Language](https://arxiv.org/abs/2205.14100) by Jianfeng Wang, Zhengyuan Yang, Xiaowei Hu, Linjie Li, Kevin Lin, Zhe Gan, Zicheng Liu, Ce Liu, Lijuan Wang. 71. **[GLPN](model_doc/glpn)** (from KAIST) released with the paper [Global-Local Path Networks for Monocular Depth Estimation with Vertical CutDepth](https://arxiv.org/abs/2201.07436) by Doyeon Kim, Woonghyun Ga, Pyungwhan Ahn, Donggyu Joo, Sehwan Chun, Junmo Kim. 72. **[GPT](model_doc/openai-gpt)** (from OpenAI) released with the paper [Improving Language Understanding by Generative Pre-Training](https://blog.openai.com/language-unsupervised/) by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever. 73. **[GPT Neo](model_doc/gpt_neo)** (from EleutherAI) released in the repository [EleutherAI/gpt-neo](https://github.com/EleutherAI/gpt-neo) by Sid Black, Stella Biderman, Leo Gao, Phil Wang and Connor Leahy. 74. **[GPT NeoX](model_doc/gpt_neox)** (from EleutherAI) released with the paper [GPT-NeoX-20B: An Open-Source Autoregressive Language Model](https://arxiv.org/abs/2204.06745) by Sid Black, Stella Biderman, Eric Hallahan, Quentin Anthony, Leo Gao, Laurence Golding, Horace He, Connor Leahy, Kyle McDonell, Jason Phang, Michael Pieler, USVSN Sai Prashanth, Shivanshu Purohit, Laria Reynolds, Jonathan Tow, Ben Wang, Samuel Weinbach 75. **[GPT NeoX Japanese](model_doc/gpt_neox_japanese)** (from ABEJA) released by Shinya Otani, Takayoshi Makabe, Anuj Arora, and Kyo Hattori. 76. **[GPT-2](model_doc/gpt2)** (from OpenAI) released with the paper [Language Models are Unsupervised Multitask Learners](https://blog.openai.com/better-language-models/) by Alec Radford_, Jeffrey Wu_, Rewon Child, David Luan, Dario Amodei**and Ilya Sutskever**. 77. **[GPT-J](model_doc/gptj)** (from EleutherAI) released in the repository [kingoflolz/mesh-transformer-jax](https://github.com/kingoflolz/mesh-transformer-jax/) by Ben Wang and Aran Komatsuzaki. 78. **[GPT-Sw3](model_doc/gpt-sw3)** (from AI-Sweden) released with the paper [Lessons Learned from GPT-SW3: Building the First Large-Scale Generative Language Model for Swedish](http://www.lrec-conf.org/proceedings/lrec2022/pdf/2022.lrec-1.376.pdf) by Ariel Ekgren, Amaru Cuba Gyllensten, Evangelia Gogoulou, Alice Heiman, Severine Verlinden, Joey Öhman, Fredrik Carlsson, Magnus Sahlgren. 79. **[GPTBigCode](model_doc/gpt_bigcode)** (from BigCode) released with the paper [SantaCoder: don’t reach for the stars!](https://arxiv.org/abs/2301.03988) by Loubna Ben Allal, Raymond Li, Denis Kocetkov, Chenghao Mou, Christopher Akiki, Carlos Munoz Ferrandis, Niklas Muennighoff, Mayank Mishra, Alex Gu, Manan Dey, Logesh Kumar Umapathi, Carolyn Jane Anderson, Yangtian Zi, Joel Lamy Poirier, Hailey Schoelkopf, Sergey Troshin, Dmitry Abulkhanov, Manuel Romero, Michael Lappert, Francesco De Toni, Bernardo García del Río, Qian Liu, Shamik Bose, Urvashi Bhattacharyya, Terry Yue Zhuo, Ian Yu, Paulo Villegas, Marco Zocca, Sourab Mangrulkar, David Lansky, Huu Nguyen, Danish Contractor, Luis Villa, Jia Li, Dzmitry Bahdanau, Yacine Jernite, Sean Hughes, Daniel Fried, Arjun Guha, Harm de Vries, Leandro von Werra. 80. **[GPTSAN-japanese](model_doc/gptsan-japanese)** released in the repository [tanreinama/GPTSAN](https://github.com/tanreinama/GPTSAN/blob/main/report/model.md) by Toshiyuki Sakamoto(tanreinama). 81. **[Graphormer](model_doc/graphormer)** (from Microsoft) released with the paper [Do Transformers Really Perform Bad for Graph Representation?](https://arxiv.org/abs/2106.05234) by Chengxuan Ying, Tianle Cai, Shengjie Luo, Shuxin Zheng, Guolin Ke, Di He, Yanming Shen, Tie-Yan Liu. 82. **[GroupViT](model_doc/groupvit)** (from UCSD, NVIDIA) released with the paper [GroupViT: Semantic Segmentation Emerges from Text Supervision](https://arxiv.org/abs/2202.11094) by Jiarui Xu, Shalini De Mello, Sifei Liu, Wonmin Byeon, Thomas Breuel, Jan Kautz, Xiaolong Wang. 83. **[Hubert](model_doc/hubert)** (from Facebook) released with the paper [HuBERT: Self-Supervised Speech Representation Learning by Masked Prediction of Hidden Units](https://arxiv.org/abs/2106.07447) by Wei-Ning Hsu, Benjamin Bolte, Yao-Hung Hubert Tsai, Kushal Lakhotia, Ruslan Salakhutdinov, Abdelrahman Mohamed. 84. **[I-BERT](model_doc/ibert)** (from Berkeley) released with the paper [I-BERT: Integer-only BERT Quantization](https://arxiv.org/abs/2101.01321) by Sehoon Kim, Amir Gholami, Zhewei Yao, Michael W. Mahoney, Kurt Keutzer. 85. **[ImageGPT](model_doc/imagegpt)** (from OpenAI) released with the paper [Generative Pretraining from Pixels](https://openai.com/blog/image-gpt/) by Mark Chen, Alec Radford, Rewon Child, Jeffrey Wu, Heewoo Jun, David Luan, Ilya Sutskever. 86. **[Informer](model_doc/informer)** (from Beihang University, UC Berkeley, Rutgers University, SEDD Company) released with the paper [Informer: Beyond Efficient Transformer for Long Sequence Time-Series Forecasting](https://arxiv.org/abs/2012.07436) by Haoyi Zhou, Shanghang Zhang, Jieqi Peng, Shuai Zhang, Jianxin Li, Hui Xiong, and Wancai Zhang. 87. **[Jukebox](model_doc/jukebox)** (from OpenAI) released with the paper [Jukebox: A Generative Model for Music](https://arxiv.org/pdf/2005.00341.pdf) by Prafulla Dhariwal, Heewoo Jun, Christine Payne, Jong Wook Kim, Alec Radford, Ilya Sutskever. 88. **[LayoutLM](model_doc/layoutlm)** (from Microsoft Research Asia) released with the paper [LayoutLM: Pre-training of Text and Layout for Document Image Understanding](https://arxiv.org/abs/1912.13318) by Yiheng Xu, Minghao Li, Lei Cui, Shaohan Huang, Furu Wei, Ming Zhou. 89. **[LayoutLMv2](model_doc/layoutlmv2)** (from Microsoft Research Asia) released with the paper [LayoutLMv2: Multi-modal Pre-training for Visually-Rich Document Understanding](https://arxiv.org/abs/2012.14740) by Yang Xu, Yiheng Xu, Tengchao Lv, Lei Cui, Furu Wei, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Wanxiang Che, Min Zhang, Lidong Zhou. 90. **[LayoutLMv3](model_doc/layoutlmv3)** (from Microsoft Research Asia) released with the paper [LayoutLMv3: Pre-training for Document AI with Unified Text and Image Masking](https://arxiv.org/abs/2204.08387) by Yupan Huang, Tengchao Lv, Lei Cui, Yutong Lu, Furu Wei. 91. **[LayoutXLM](model_doc/layoutxlm)** (from Microsoft Research Asia) released with the paper [LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding](https://arxiv.org/abs/2104.08836) by Yiheng Xu, Tengchao Lv, Lei Cui, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Furu Wei. 92. **[LED](model_doc/led)** (from AllenAI) released with the paper [Longformer: The Long-Document Transformer](https://arxiv.org/abs/2004.05150) by Iz Beltagy, Matthew E. Peters, Arman Cohan. 93. **[LeViT](model_doc/levit)** (from Meta AI) released with the paper [LeViT: A Vision Transformer in ConvNet’s Clothing for Faster Inference](https://arxiv.org/abs/2104.01136) by Ben Graham, Alaaeldin El-Nouby, Hugo Touvron, Pierre Stock, Armand Joulin, Hervé Jégou, Matthijs Douze. 94. **[LiLT](model_doc/lilt)** (from South China University of Technology) released with the paper [LiLT: A Simple yet Effective Language-Independent Layout Transformer for Structured Document Understanding](https://arxiv.org/abs/2202.13669) by Jiapeng Wang, Lianwen Jin, Kai Ding. 95. **[LLaMA](model_doc/llama)** (from The FAIR team of Meta AI) released with the paper [LLaMA: Open and Efficient Foundation Language Models](https://arxiv.org/abs/2302.13971) by Hugo Touvron, Thibaut Lavril, Gautier Izacard, Xavier Martinet, Marie-Anne Lachaux, Timothée Lacroix, Baptiste Rozière, Naman Goyal, Eric Hambro, Faisal Azhar, Aurelien Rodriguez, Armand Joulin, Edouard Grave, Guillaume Lample. 96. **[Longformer](model_doc/longformer)** (from AllenAI) released with the paper [Longformer: The Long-Document Transformer](https://arxiv.org/abs/2004.05150) by Iz Beltagy, Matthew E. Peters, Arman Cohan. 97. **[LongT5](model_doc/longt5)** (from Google AI) released with the paper [LongT5: Efficient Text-To-Text Transformer for Long Sequences](https://arxiv.org/abs/2112.07916) by Mandy Guo, Joshua Ainslie, David Uthus, Santiago Ontanon, Jianmo Ni, Yun-Hsuan Sung, Yinfei Yang. 98. **[LUKE](model_doc/luke)** (from Studio Ousia) released with the paper [LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention](https://arxiv.org/abs/2010.01057) by Ikuya Yamada, Akari Asai, Hiroyuki Shindo, Hideaki Takeda, Yuji Matsumoto. 99. **[LXMERT](model_doc/lxmert)** (from UNC Chapel Hill) released with the paper [LXMERT: Learning Cross-Modality Encoder Representations from Transformers for Open-Domain Question Answering](https://arxiv.org/abs/1908.07490) by Hao Tan and Mohit Bansal. 100. **[M-CTC-T](model_doc/mctct)** (from Facebook) released with the paper [Pseudo-Labeling For Massively Multilingual Speech Recognition](https://arxiv.org/abs/2111.00161) by Loren Lugosch, Tatiana Likhomanenko, Gabriel Synnaeve, and Ronan Collobert. 101. **[M2M100](model_doc/m2m_100)** (from Facebook) released with the paper [Beyond English-Centric Multilingual Machine Translation](https://arxiv.org/abs/2010.11125) by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin. 102. **[MarianMT](model_doc/marian)** Machine translation models trained using [OPUS](http://opus.nlpl.eu/) data by Jörg Tiedemann. The [Marian Framework](https://marian-nmt.github.io/) is being developed by the Microsoft Translator Team. 103. **[MarkupLM](model_doc/markuplm)** (from Microsoft Research Asia) released with the paper [MarkupLM: Pre-training of Text and Markup Language for Visually-rich Document Understanding](https://arxiv.org/abs/2110.08518) by Junlong Li, Yiheng Xu, Lei Cui, Furu Wei. 104. **[Mask2Former](model_doc/mask2former)** (from FAIR and UIUC) released with the paper [Masked-attention Mask Transformer for Universal Image Segmentation](https://arxiv.org/abs/2112.01527) by Bowen Cheng, Ishan Misra, Alexander G. Schwing, Alexander Kirillov, Rohit Girdhar. 105. **[MaskFormer](model_doc/maskformer)** (from Meta and UIUC) released with the paper [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://arxiv.org/abs/2107.06278) by Bowen Cheng, Alexander G. Schwing, Alexander Kirillov. 106. **[MatCha](model_doc/matcha)** (from Google AI) released with the paper [MatCha: Enhancing Visual Language Pretraining with Math Reasoning and Chart Derendering](https://arxiv.org/abs/2212.09662) by Fangyu Liu, Francesco Piccinno, Syrine Krichene, Chenxi Pang, Kenton Lee, Mandar Joshi, Yasemin Altun, Nigel Collier, Julian Martin Eisenschlos. 107. **[mBART](model_doc/mbart)** (from Facebook) released with the paper [Multilingual Denoising Pre-training for Neural Machine Translation](https://arxiv.org/abs/2001.08210) by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer. 108. **[mBART-50](model_doc/mbart)** (from Facebook) released with the paper [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://arxiv.org/abs/2008.00401) by Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan. 109. **[MEGA](model_doc/mega)** (from Meta/USC/CMU/SJTU) released with the paper [Mega: Moving Average Equipped Gated Attention](https://arxiv.org/abs/2209.10655) by Xuezhe Ma, Chunting Zhou, Xiang Kong, Junxian He, Liangke Gui, Graham Neubig, Jonathan May, and Luke Zettlemoyer. 110. **[Megatron-BERT](model_doc/megatron-bert)** (from NVIDIA) released with the paper [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://arxiv.org/abs/1909.08053) by Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro. 111. **[Megatron-GPT2](model_doc/megatron_gpt2)** (from NVIDIA) released with the paper [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism](https://arxiv.org/abs/1909.08053) by Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro. 112. **[MGP-STR](model_doc/mgp-str)** (from Alibaba Research) released with the paper [Multi-Granularity Prediction for Scene Text Recognition](https://arxiv.org/abs/2209.03592) by Peng Wang, Cheng Da, and Cong Yao. 113. **[mLUKE](model_doc/mluke)** (from Studio Ousia) released with the paper [mLUKE: The Power of Entity Representations in Multilingual Pretrained Language Models](https://arxiv.org/abs/2110.08151) by Ryokan Ri, Ikuya Yamada, and Yoshimasa Tsuruoka. 114. **[MMS](model_doc/mms)** (from Facebook) released with the paper [Scaling Speech Technology to 1,000+ Languages](https://arxiv.org/abs/2305.13516) by Vineel Pratap, Andros Tjandra, Bowen Shi, Paden Tomasello, Arun Babu, Sayani Kundu, Ali Elkahky, Zhaoheng Ni, Apoorv Vyas, Maryam Fazel-Zarandi, Alexei Baevski, Yossi Adi, Xiaohui Zhang, Wei-Ning Hsu, Alexis Conneau, Michael Auli. 115. **[MobileBERT](model_doc/mobilebert)** (from CMU/Google Brain) released with the paper [MobileBERT: a Compact Task-Agnostic BERT for Resource-Limited Devices](https://arxiv.org/abs/2004.02984) by Zhiqing Sun, Hongkun Yu, Xiaodan Song, Renjie Liu, Yiming Yang, and Denny Zhou. 116. **[MobileNetV1](model_doc/mobilenet_v1)** (from Google Inc.) released with the paper [MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications](https://arxiv.org/abs/1704.04861) by Andrew G. Howard, Menglong Zhu, Bo Chen, Dmitry Kalenichenko, Weijun Wang, Tobias Weyand, Marco Andreetto, Hartwig Adam. 117. **[MobileNetV2](model_doc/mobilenet_v2)** (from Google Inc.) released with the paper [MobileNetV2: Inverted Residuals and Linear Bottlenecks](https://arxiv.org/abs/1801.04381) by Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen. 118. **[MobileViT](model_doc/mobilevit)** (from Apple) released with the paper [MobileViT: Light-weight, General-purpose, and Mobile-friendly Vision Transformer](https://arxiv.org/abs/2110.02178) by Sachin Mehta and Mohammad Rastegari. 119. **[MobileViTV2](model_doc/mobilevitv2)** (from Apple) released with the paper [Separable Self-attention for Mobile Vision Transformers](https://arxiv.org/abs/2206.02680) by Sachin Mehta and Mohammad Rastegari. 120. **[MPNet](model_doc/mpnet)** (from Microsoft Research) released with the paper [MPNet: Masked and Permuted Pre-training for Language Understanding](https://arxiv.org/abs/2004.09297) by Kaitao Song, Xu Tan, Tao Qin, Jianfeng Lu, Tie-Yan Liu. 121. **[MT5](model_doc/mt5)** (from Google AI) released with the paper [mT5: A massively multilingual pre-trained text-to-text transformer](https://arxiv.org/abs/2010.11934) by Linting Xue, Noah Constant, Adam Roberts, Mihir Kale, Rami Al-Rfou, Aditya Siddhant, Aditya Barua, Colin Raffel. 122. **[MVP](model_doc/mvp)** (from RUC AI Box) released with the paper [MVP: Multi-task Supervised Pre-training for Natural Language Generation](https://arxiv.org/abs/2206.12131) by Tianyi Tang, Junyi Li, Wayne Xin Zhao and Ji-Rong Wen. 123. **[NAT](model_doc/nat)** (from SHI Labs) released with the paper [Neighborhood Attention Transformer](https://arxiv.org/abs/2204.07143) by Ali Hassani, Steven Walton, Jiachen Li, Shen Li, and Humphrey Shi. 124. **[Nezha](model_doc/nezha)** (from Huawei Noah’s Ark Lab) released with the paper [NEZHA: Neural Contextualized Representation for Chinese Language Understanding](https://arxiv.org/abs/1909.00204) by Junqiu Wei, Xiaozhe Ren, Xiaoguang Li, Wenyong Huang, Yi Liao, Yasheng Wang, Jiashu Lin, Xin Jiang, Xiao Chen and Qun Liu. 125. **[NLLB](model_doc/nllb)** (from Meta) released with the paper [No Language Left Behind: Scaling Human-Centered Machine Translation](https://arxiv.org/abs/2207.04672) by the NLLB team. 126. **[NLLB-MOE](model_doc/nllb-moe)** (from Meta) released with the paper [No Language Left Behind: Scaling Human-Centered Machine Translation](https://arxiv.org/abs/2207.04672) by the NLLB team. 127. **[Nyströmformer](model_doc/nystromformer)** (from the University of Wisconsin - Madison) released with the paper [Nyströmformer: A Nyström-Based Algorithm for Approximating Self-Attention](https://arxiv.org/abs/2102.03902) by Yunyang Xiong, Zhanpeng Zeng, Rudrasis Chakraborty, Mingxing Tan, Glenn Fung, Yin Li, Vikas Singh. 128. **[OneFormer](model_doc/oneformer)** (from SHI Labs) released with the paper [OneFormer: One Transformer to Rule Universal Image Segmentation](https://arxiv.org/abs/2211.06220) by Jitesh Jain, Jiachen Li, MangTik Chiu, Ali Hassani, Nikita Orlov, Humphrey Shi. 129. **[OpenLlama](model_doc/open-llama)** (from [s-JoL](https://huggingface.co/s-JoL)) released in [Open-Llama](https://github.com/s-JoL/Open-Llama). 130. **[OPT](master/model_doc/opt)** (from Meta AI) released with the paper [OPT: Open Pre-trained Transformer Language Models](https://arxiv.org/abs/2205.01068) by Susan Zhang, Stephen Roller, Naman Goyal, Mikel Artetxe, Moya Chen, Shuohui Chen et al. 131. **[OWL-ViT](model_doc/owlvit)** (from Google AI) released with the paper [Simple Open-Vocabulary Object Detection with Vision Transformers](https://arxiv.org/abs/2205.06230) by Matthias Minderer, Alexey Gritsenko, Austin Stone, Maxim Neumann, Dirk Weissenborn, Alexey Dosovitskiy, Aravindh Mahendran, Anurag Arnab, Mostafa Dehghani, Zhuoran Shen, Xiao Wang, Xiaohua Zhai, Thomas Kipf, and Neil Houlsby. 132. **[Pegasus](model_doc/pegasus)** (from Google) released with the paper [PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization](https://arxiv.org/abs/1912.08777) by Jingqing Zhang, Yao Zhao, Mohammad Saleh and Peter J. Liu. 133. **[PEGASUS-X](model_doc/pegasus_x)** (from Google) released with the paper [Investigating Efficiently Extending Transformers for Long Input Summarization](https://arxiv.org/abs/2208.04347) by Jason Phang, Yao Zhao, and Peter J. Liu. 134. **[Perceiver IO](model_doc/perceiver)** (from Deepmind) released with the paper [Perceiver IO: A General Architecture for Structured Inputs & Outputs](https://arxiv.org/abs/2107.14795) by Andrew Jaegle, Sebastian Borgeaud, Jean-Baptiste Alayrac, Carl Doersch, Catalin Ionescu, David Ding, Skanda Koppula, Daniel Zoran, Andrew Brock, Evan Shelhamer, Olivier Hénaff, Matthew M. Botvinick, Andrew Zisserman, Oriol Vinyals, João Carreira. 135. **[PhoBERT](model_doc/phobert)** (from VinAI Research) released with the paper [PhoBERT: Pre-trained language models for Vietnamese](https://www.aclweb.org/anthology/2020.findings-emnlp.92/) by Dat Quoc Nguyen and Anh Tuan Nguyen. 136. **[Pix2Struct](model_doc/pix2struct)** (from Google) released with the paper [Pix2Struct: Screenshot Parsing as Pretraining for Visual Language Understanding](https://arxiv.org/abs/2210.03347) by Kenton Lee, Mandar Joshi, Iulia Turc, Hexiang Hu, Fangyu Liu, Julian Eisenschlos, Urvashi Khandelwal, Peter Shaw, Ming-Wei Chang, Kristina Toutanova. 137. **[PLBart](model_doc/plbart)** (from UCLA NLP) released with the paper [Unified Pre-training for Program Understanding and Generation](https://arxiv.org/abs/2103.06333) by Wasi Uddin Ahmad, Saikat Chakraborty, Baishakhi Ray, Kai-Wei Chang. 138. **[PoolFormer](model_doc/poolformer)** (from Sea AI Labs) released with the paper [MetaFormer is Actually What You Need for Vision](https://arxiv.org/abs/2111.11418) by Yu, Weihao and Luo, Mi and Zhou, Pan and Si, Chenyang and Zhou, Yichen and Wang, Xinchao and Feng, Jiashi and Yan, Shuicheng. 139. **[ProphetNet](model_doc/prophetnet)** (from Microsoft Research) released with the paper [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://arxiv.org/abs/2001.04063) by Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang and Ming Zhou. 140. **[QDQBert](model_doc/qdqbert)** (from NVIDIA) released with the paper [Integer Quantization for Deep Learning Inference: Principles and Empirical Evaluation](https://arxiv.org/abs/2004.09602) by Hao Wu, Patrick Judd, Xiaojie Zhang, Mikhail Isaev and Paulius Micikevicius. 141. **[RAG](model_doc/rag)** (from Facebook) released with the paper [Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks](https://arxiv.org/abs/2005.11401) by Patrick Lewis, Ethan Perez, Aleksandara Piktus, Fabio Petroni, Vladimir Karpukhin, Naman Goyal, Heinrich Küttler, Mike Lewis, Wen-tau Yih, Tim Rocktäschel, Sebastian Riedel, Douwe Kiela. 142. **[REALM](model_doc/realm.html)** (from Google Research) released with the paper [REALM: Retrieval-Augmented Language Model Pre-Training](https://arxiv.org/abs/2002.08909) by Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Ming-Wei Chang. 143. **[Reformer](model_doc/reformer)** (from Google Research) released with the paper [Reformer: The Efficient Transformer](https://arxiv.org/abs/2001.04451) by Nikita Kitaev, Łukasz Kaiser, Anselm Levskaya. 144. **[RegNet](model_doc/regnet)** (from META Platforms) released with the paper [Designing Network Design Space](https://arxiv.org/abs/2003.13678) by Ilija Radosavovic, Raj Prateek Kosaraju, Ross Girshick, Kaiming He, Piotr Dollár. 145. **[RemBERT](model_doc/rembert)** (from Google Research) released with the paper [Rethinking embedding coupling in pre-trained language models](https://arxiv.org/abs/2010.12821) by Hyung Won Chung, Thibault Févry, Henry Tsai, M. Johnson, Sebastian Ruder. 146. **[ResNet](model_doc/resnet)** (from Microsoft Research) released with the paper [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385) by Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun. 147. **[RoBERTa](model_doc/roberta)** (from Facebook), released together with the paper [RoBERTa: A Robustly Optimized BERT Pretraining Approach](https://arxiv.org/abs/1907.11692) by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov. 148. **[RoBERTa-PreLayerNorm](model_doc/roberta-prelayernorm)** (from Facebook) released with the paper [fairseq: A Fast, Extensible Toolkit for Sequence Modeling](https://arxiv.org/abs/1904.01038) by Myle Ott, Sergey Edunov, Alexei Baevski, Angela Fan, Sam Gross, Nathan Ng, David Grangier, Michael Auli. 149. **[RoCBert](model_doc/roc_bert)** (from WeChatAI) released with the paper [RoCBert: Robust Chinese Bert with Multimodal Contrastive Pretraining](https://aclanthology.org/2022.acl-long.65.pdf) by HuiSu, WeiweiShi, XiaoyuShen, XiaoZhou, TuoJi, JiaruiFang, JieZhou. 150. **[RoFormer](model_doc/roformer)** (from ZhuiyiTechnology), released together with the paper [RoFormer: Enhanced Transformer with Rotary Position Embedding](https://arxiv.org/abs/2104.09864) by Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu. 151. **[RWKV](model_doc/rwkv)** (from Bo Peng), released on [this repo](https://github.com/BlinkDL/RWKV-LM) by Bo Peng. 152. **[SegFormer](model_doc/segformer)** (from NVIDIA) released with the paper [SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers](https://arxiv.org/abs/2105.15203) by Enze Xie, Wenhai Wang, Zhiding Yu, Anima Anandkumar, Jose M. Alvarez, Ping Luo. 153. **[Segment Anything](model_doc/sam)** (from Meta AI) released with the paper [Segment Anything](https://arxiv.org/pdf/2304.02643v1.pdf) by Alexander Kirillov, Eric Mintun, Nikhila Ravi, Hanzi Mao, Chloe Rolland, Laura Gustafson, Tete Xiao, Spencer Whitehead, Alex Berg, Wan-Yen Lo, Piotr Dollar, Ross Girshick. 154. **[SEW](model_doc/sew)** (from ASAPP) released with the paper [Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition](https://arxiv.org/abs/2109.06870) by Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi. 155. **[SEW-D](model_doc/sew_d)** (from ASAPP) released with the paper [Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition](https://arxiv.org/abs/2109.06870) by Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi. 156. **[SpeechT5](model_doc/speecht5)** (from Microsoft Research) released with the paper [SpeechT5: Unified-Modal Encoder-Decoder Pre-Training for Spoken Language Processing](https://arxiv.org/abs/2110.07205) by Junyi Ao, Rui Wang, Long Zhou, Chengyi Wang, Shuo Ren, Yu Wu, Shujie Liu, Tom Ko, Qing Li, Yu Zhang, Zhihua Wei, Yao Qian, Jinyu Li, Furu Wei. 157. **[SpeechToTextTransformer](model_doc/speech_to_text)** (from Facebook), released together with the paper [fairseq S2T: Fast Speech-to-Text Modeling with fairseq](https://arxiv.org/abs/2010.05171) by Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Dmytro Okhonko, Juan Pino. 158. **[SpeechToTextTransformer2](model_doc/speech_to_text_2)** (from Facebook), released together with the paper [Large-Scale Self- and Semi-Supervised Learning for Speech Translation](https://arxiv.org/abs/2104.06678) by Changhan Wang, Anne Wu, Juan Pino, Alexei Baevski, Michael Auli, Alexis Conneau. 159. **[Splinter](model_doc/splinter)** (from Tel Aviv University), released together with the paper [Few-Shot Question Answering by Pretraining Span Selection](https://arxiv.org/abs/2101.00438) by Ori Ram, Yuval Kirstain, Jonathan Berant, Amir Globerson, Omer Levy. 160. **[SqueezeBERT](model_doc/squeezebert)** (from Berkeley) released with the paper [SqueezeBERT: What can computer vision teach NLP about efficient neural networks?](https://arxiv.org/abs/2006.11316) by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer. 161. **[SwiftFormer](model_doc/swiftformer)** (from MBZUAI) released with the paper [SwiftFormer: Efficient Additive Attention for Transformer-based Real-time Mobile Vision Applications](https://arxiv.org/abs/2303.15446) by Abdelrahman Shaker, Muhammad Maaz, Hanoona Rasheed, Salman Khan, Ming-Hsuan Yang, Fahad Shahbaz Khan. 162. **[Swin Transformer](model_doc/swin)** (from Microsoft) released with the paper [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows](https://arxiv.org/abs/2103.14030) by Ze Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo. 163. **[Swin Transformer V2](model_doc/swinv2)** (from Microsoft) released with the paper [Swin Transformer V2: Scaling Up Capacity and Resolution](https://arxiv.org/abs/2111.09883) by Ze Liu, Han Hu, Yutong Lin, Zhuliang Yao, Zhenda Xie, Yixuan Wei, Jia Ning, Yue Cao, Zheng Zhang, Li Dong, Furu Wei, Baining Guo. 164. **[Swin2SR](model_doc/swin2sr)** (from University of Würzburg) released with the paper [Swin2SR: SwinV2 Transformer for Compressed Image Super-Resolution and Restoration](https://arxiv.org/abs/2209.11345) by Marcos V. Conde, Ui-Jin Choi, Maxime Burchi, Radu Timofte. 165. **[SwitchTransformers](model_doc/switch_transformers)** (from Google) released with the paper [Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity](https://arxiv.org/abs/2101.03961) by William Fedus, Barret Zoph, Noam Shazeer. 166. **[T5](model_doc/t5)** (from Google AI) released with the paper [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://arxiv.org/abs/1910.10683) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu. 167. **[T5v1.1](model_doc/t5v1.1)** (from Google AI) released in the repository [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu. 168. **[Table Transformer](model_doc/table-transformer)** (from Microsoft Research) released with the paper [PubTables-1M: Towards Comprehensive Table Extraction From Unstructured Documents](https://arxiv.org/abs/2110.00061) by Brandon Smock, Rohith Pesala, Robin Abraham. 169. **[TAPAS](model_doc/tapas)** (from Google AI) released with the paper [TAPAS: Weakly Supervised Table Parsing via Pre-training](https://arxiv.org/abs/2004.02349) by Jonathan Herzig, Paweł Krzysztof Nowak, Thomas Müller, Francesco Piccinno and Julian Martin Eisenschlos. 170. **[TAPEX](model_doc/tapex)** (from Microsoft Research) released with the paper [TAPEX: Table Pre-training via Learning a Neural SQL Executor](https://arxiv.org/abs/2107.07653) by Qian Liu, Bei Chen, Jiaqi Guo, Morteza Ziyadi, Zeqi Lin, Weizhu Chen, Jian-Guang Lou. 171. **[Time Series Transformer](model_doc/time_series_transformer)** (from HuggingFace). 172. **[TimeSformer](model_doc/timesformer)** (from Facebook) released with the paper [Is Space-Time Attention All You Need for Video Understanding?](https://arxiv.org/abs/2102.05095) by Gedas Bertasius, Heng Wang, Lorenzo Torresani. 173. **[Trajectory Transformer](model_doc/trajectory_transformers)** (from the University of California at Berkeley) released with the paper [Offline Reinforcement Learning as One Big Sequence Modeling Problem](https://arxiv.org/abs/2106.02039) by Michael Janner, Qiyang Li, Sergey Levine 174. **[Transformer-XL](model_doc/transfo-xl)** (from Google/CMU) released with the paper [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://arxiv.org/abs/1901.02860) by Zihang Dai_, Zhilin Yang_, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov. 175. **[TrOCR](model_doc/trocr)** (from Microsoft), released together with the paper [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://arxiv.org/abs/2109.10282) by Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei. 176. **[TVLT](model_doc/tvlt)** (from UNC Chapel Hill) released with the paper [TVLT: Textless Vision-Language Transformer](https://arxiv.org/abs/2209.14156) by Zineng Tang, Jaemin Cho, Yixin Nie, Mohit Bansal. 177. **[UL2](model_doc/ul2)** (from Google Research) released with the paper [Unifying Language Learning Paradigms](https://arxiv.org/abs/2205.05131v1) by Yi Tay, Mostafa Dehghani, Vinh Q. Tran, Xavier Garcia, Dara Bahri, Tal Schuster, Huaixiu Steven Zheng, Neil Houlsby, Donald Metzler 178. **[UniSpeech](model_doc/unispeech)** (from Microsoft Research) released with the paper [UniSpeech: Unified Speech Representation Learning with Labeled and Unlabeled Data](https://arxiv.org/abs/2101.07597) by Chengyi Wang, Yu Wu, Yao Qian, Kenichi Kumatani, Shujie Liu, Furu Wei, Michael Zeng, Xuedong Huang. 179. **[UniSpeechSat](model_doc/unispeech-sat)** (from Microsoft Research) released with the paper [UNISPEECH-SAT: UNIVERSAL SPEECH REPRESENTATION LEARNING WITH SPEAKER AWARE PRE-TRAINING](https://arxiv.org/abs/2110.05752) by Sanyuan Chen, Yu Wu, Chengyi Wang, Zhengyang Chen, Zhuo Chen, Shujie Liu, Jian Wu, Yao Qian, Furu Wei, Jinyu Li, Xiangzhan Yu. 180. **[UPerNet](model_doc/upernet)** (from Peking University) released with the paper [Unified Perceptual Parsing for Scene Understanding](https://arxiv.org/abs/1807.10221) by Tete Xiao, Yingcheng Liu, Bolei Zhou, Yuning Jiang, Jian Sun. 181. **[VAN](model_doc/van)** (from Tsinghua University and Nankai University) released with the paper [Visual Attention Network](https://arxiv.org/abs/2202.09741) by Meng-Hao Guo, Cheng-Ze Lu, Zheng-Ning Liu, Ming-Ming Cheng, Shi-Min Hu. 182. **[VideoMAE](model_doc/videomae)** (from Multimedia Computing Group, Nanjing University) released with the paper [VideoMAE: Masked Autoencoders are Data-Efficient Learners for Self-Supervised Video Pre-Training](https://arxiv.org/abs/2203.12602) by Zhan Tong, Yibing Song, Jue Wang, Limin Wang. 183. **[ViLT](model_doc/vilt)** (from NAVER AI Lab/Kakao Enterprise/Kakao Brain) released with the paper [ViLT: Vision-and-Language Transformer Without Convolution or Region Supervision](https://arxiv.org/abs/2102.03334) by Wonjae Kim, Bokyung Son, Ildoo Kim. 184. **[Vision Transformer (ViT)](model_doc/vit)** (from Google AI) released with the paper [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://arxiv.org/abs/2010.11929) by Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby. 185. **[VisualBERT](model_doc/visual_bert)** (from UCLA NLP) released with the paper [VisualBERT: A Simple and Performant Baseline for Vision and Language](https://arxiv.org/pdf/1908.03557) by Liunian Harold Li, Mark Yatskar, Da Yin, Cho-Jui Hsieh, Kai-Wei Chang. 186. **[ViT Hybrid](model_doc/vit_hybrid)** (from Google AI) released with the paper [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://arxiv.org/abs/2010.11929) by Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby. 187. **[ViTMAE](model_doc/vit_mae)** (from Meta AI) released with the paper [Masked Autoencoders Are Scalable Vision Learners](https://arxiv.org/abs/2111.06377) by Kaiming He, Xinlei Chen, Saining Xie, Yanghao Li, Piotr Dollár, Ross Girshick. 188. **[ViTMSN](model_doc/vit_msn)** (from Meta AI) released with the paper [Masked Siamese Networks for Label-Efficient Learning](https://arxiv.org/abs/2204.07141) by Mahmoud Assran, Mathilde Caron, Ishan Misra, Piotr Bojanowski, Florian Bordes, Pascal Vincent, Armand Joulin, Michael Rabbat, Nicolas Ballas. 189. **[Wav2Vec2](model_doc/wav2vec2)** (from Facebook AI) released with the paper [wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations](https://arxiv.org/abs/2006.11477) by Alexei Baevski, Henry Zhou, Abdelrahman Mohamed, Michael Auli. 190. **[Wav2Vec2-Conformer](model_doc/wav2vec2-conformer)** (from Facebook AI) released with the paper [FAIRSEQ S2T: Fast Speech-to-Text Modeling with FAIRSEQ](https://arxiv.org/abs/2010.05171) by Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Sravya Popuri, Dmytro Okhonko, Juan Pino. 191. **[Wav2Vec2Phoneme](model_doc/wav2vec2_phoneme)** (from Facebook AI) released with the paper [Simple and Effective Zero-shot Cross-lingual Phoneme Recognition](https://arxiv.org/abs/2109.11680) by Qiantong Xu, Alexei Baevski, Michael Auli. 192. **[WavLM](model_doc/wavlm)** (from Microsoft Research) released with the paper [WavLM: Large-Scale Self-Supervised Pre-Training for Full Stack Speech Processing](https://arxiv.org/abs/2110.13900) by Sanyuan Chen, Chengyi Wang, Zhengyang Chen, Yu Wu, Shujie Liu, Zhuo Chen, Jinyu Li, Naoyuki Kanda, Takuya Yoshioka, Xiong Xiao, Jian Wu, Long Zhou, Shuo Ren, Yanmin Qian, Yao Qian, Jian Wu, Michael Zeng, Furu Wei. 193. **[Whisper](model_doc/whisper)** (from OpenAI) released with the paper [Robust Speech Recognition via Large-Scale Weak Supervision](https://cdn.openai.com/papers/whisper.pdf) by Alec Radford, Jong Wook Kim, Tao Xu, Greg Brockman, Christine McLeavey, Ilya Sutskever. 194. **[X-CLIP](model_doc/xclip)** (from Microsoft Research) released with the paper [Expanding Language-Image Pretrained Models for General Video Recognition](https://arxiv.org/abs/2208.02816) by Bolin Ni, Houwen Peng, Minghao Chen, Songyang Zhang, Gaofeng Meng, Jianlong Fu, Shiming Xiang, Haibin Ling. 195. **[X-MOD](model_doc/xmod)** (from Meta AI) released with the paper [Lifting the Curse of Multilinguality by Pre-training Modular Transformers](http://dx.doi.org/10.18653/v1/2022.naacl-main.255) by Jonas Pfeiffer, Naman Goyal, Xi Lin, Xian Li, James Cross, Sebastian Riedel, Mikel Artetxe. 196. **[XGLM](model_doc/xglm)** (From Facebook AI) released with the paper [Few-shot Learning with Multilingual Language Models](https://arxiv.org/abs/2112.10668) by Xi Victoria Lin, Todor Mihaylov, Mikel Artetxe, Tianlu Wang, Shuohui Chen, Daniel Simig, Myle Ott, Naman Goyal, Shruti Bhosale, Jingfei Du, Ramakanth Pasunuru, Sam Shleifer, Punit Singh Koura, Vishrav Chaudhary, Brian O’Horo, Jeff Wang, Luke Zettlemoyer, Zornitsa Kozareva, Mona Diab, Veselin Stoyanov, Xian Li. 197. **[XLM](model_doc/xlm)** (from Facebook) released together with the paper [Cross-lingual Language Model Pretraining](https://arxiv.org/abs/1901.07291) by Guillaume Lample and Alexis Conneau. 198. **[XLM-ProphetNet](model_doc/xlm-prophetnet)** (from Microsoft Research) released with the paper [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training](https://arxiv.org/abs/2001.04063) by Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang and Ming Zhou. 199. **[XLM-RoBERTa](model_doc/xlm-roberta)** (from Facebook AI), released together with the paper [Unsupervised Cross-lingual Representation Learning at Scale](https://arxiv.org/abs/1911.02116) by Alexis Conneau_, Kartikay Khandelwal_, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmán, Edouard Grave, Myle Ott, Luke Zettlemoyer and Veselin Stoyanov. 200. **[XLM-RoBERTa-XL](model_doc/xlm-roberta-xl)** (from Facebook AI), released together with the paper [Larger-Scale Transformers for Multilingual Masked Language Modeling](https://arxiv.org/abs/2105.00572) by Naman Goyal, Jingfei Du, Myle Ott, Giri Anantharaman, Alexis Conneau. 201. **[XLM-V](model_doc/xlm-v)** (from Meta AI) released with the paper [XLM-V: Overcoming the Vocabulary Bottleneck in Multilingual Masked Language Models](https://arxiv.org/abs/2301.10472) by Davis Liang, Hila Gonen, Yuning Mao, Rui Hou, Naman Goyal, Marjan Ghazvininejad, Luke Zettlemoyer, Madian Khabsa. 202. **[XLNet](model_doc/xlnet)** (from Google/CMU) released with the paper [​XLNet: Generalized Autoregressive Pretraining for Language Understanding](https://arxiv.org/abs/1906.08237) by Zhilin Yang_, Zihang Dai_, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le. 203. **[XLS-R](model_doc/xls_r)** (from Facebook AI) released with the paper [XLS-R: Self-supervised Cross-lingual Speech Representation Learning at Scale](https://arxiv.org/abs/2111.09296) by Arun Babu, Changhan Wang, Andros Tjandra, Kushal Lakhotia, Qiantong Xu, Naman Goyal, Kritika Singh, Patrick von Platen, Yatharth Saraf, Juan Pino, Alexei Baevski, Alexis Conneau, Michael Auli. 204. **[XLSR-Wav2Vec2](model_doc/xlsr_wav2vec2)** (from Facebook AI) released with the paper [Unsupervised Cross-Lingual Representation Learning For Speech Recognition](https://arxiv.org/abs/2006.13979) by Alexis Conneau, Alexei Baevski, Ronan Collobert, Abdelrahman Mohamed, Michael Auli. 205. **[YOLOS](model_doc/yolos)** (from Huazhong University of Science & Technology) released with the paper [You Only Look at One Sequence: Rethinking Transformer in Vision through Object Detection](https://arxiv.org/abs/2106.00666) by Yuxin Fang, Bencheng Liao, Xinggang Wang, Jiemin Fang, Jiyang Qi, Rui Wu, Jianwei Niu, Wenyu Liu. 206. **[YOSO](model_doc/yoso)** (from the University of Wisconsin - Madison) released with the paper [You Only Sample (Almost) Once: Linear Cost Self-Attention Via Bernoulli Sampling](https://arxiv.org/abs/2111.09714) by Zhanpeng Zeng, Yunyang Xiong, Sathya N. Ravi, Shailesh Acharya, Glenn Fung, Vikas Singh. ### [](#supported-frameworks)Supported frameworks The table below represents the current support in the library for each of those models, whether they have a Python tokenizer (called “slow”). A “fast” tokenizer backed by the 🤗 Tokenizers library, whether they have support in Jax (via Flax), PyTorch, and/or TensorFlow. | Model | Tokenizer slow | Tokenizer fast | PyTorch support | TensorFlow support | Flax Support | | --- | --- | --- | --- | --- | --- | | ALBERT | ✅ | ✅ | ✅ | ✅ | ✅ | | ALIGN | ❌ | ❌ | ✅ | ❌ | ❌ | | AltCLIP | ❌ | ❌ | ✅ | ❌ | ❌ | | Audio Spectrogram Transformer | ❌ | ❌ | ✅ | ❌ | ❌ | | Autoformer | ❌ | ❌ | ✅ | ❌ | ❌ | | BART | ✅ | ✅ | ✅ | ✅ | ✅ | | BEiT | ❌ | ❌ | ✅ | ❌ | ✅ | | BERT | ✅ | ✅ | ✅ | ✅ | ✅ | | Bert Generation | ✅ | ❌ | ✅ | ❌ | ❌ | | BigBird | ✅ | ✅ | ✅ | ❌ | ✅ | | BigBird-Pegasus | ❌ | ❌ | ✅ | ❌ | ❌ | | BioGpt | ✅ | ❌ | ✅ | ❌ | ❌ | | BiT | ❌ | ❌ | ✅ | ❌ | ❌ | | Blenderbot | ✅ | ✅ | ✅ | ✅ | ✅ | | BlenderbotSmall | ✅ | ✅ | ✅ | ✅ | ✅ | | BLIP | ❌ | ❌ | ✅ | ✅ | ❌ | | BLIP-2 | ❌ | ❌ | ✅ | ❌ | ❌ | | BLOOM | ❌ | ✅ | ✅ | ❌ | ❌ | | BridgeTower | ❌ | ❌ | ✅ | ❌ | ❌ | | CamemBERT | ✅ | ✅ | ✅ | ✅ | ❌ | | CANINE | ✅ | ❌ | ✅ | ❌ | ❌ | | Chinese-CLIP | ❌ | ❌ | ✅ | ❌ | ❌ | | CLAP | ❌ | ❌ | ✅ | ❌ | ❌ | | CLIP | ✅ | ✅ | ✅ | ✅ | ✅ | | CLIPSeg | ❌ | ❌ | ✅ | ❌ | ❌ | | CodeGen | ✅ | ✅ | ✅ | ❌ | ❌ | | Conditional DETR | ❌ | ❌ | ✅ | ❌ | ❌ | | ConvBERT | ✅ | ✅ | ✅ | ✅ | ❌ | | ConvNeXT | ❌ | ❌ | ✅ | ✅ | ❌ | | ConvNeXTV2 | ❌ | ❌ | ✅ | ❌ | ❌ | | CPM-Ant | ✅ | ❌ | ✅ | ❌ | ❌ | | CTRL | ✅ | ❌ | ✅ | ✅ | ❌ | | CvT | ❌ | ❌ | ✅ | ✅ | ❌ | | Data2VecAudio | ❌ | ❌ | ✅ | ❌ | ❌ | | Data2VecText | ❌ | ❌ | ✅ | ❌ | ❌ | | Data2VecVision | ❌ | ❌ | ✅ | ✅ | ❌ | | DeBERTa | ✅ | ✅ | ✅ | ✅ | ❌ | | DeBERTa-v2 | ✅ | ✅ | ✅ | ✅ | ❌ | | Decision Transformer | ❌ | ❌ | ✅ | ❌ | ❌ | | Deformable DETR | ❌ | ❌ | ✅ | ❌ | ❌ | | DeiT | ❌ | ❌ | ✅ | ✅ | ❌ | | DETA | ❌ | ❌ | ✅ | ❌ | ❌ | | DETR | ❌ | ❌ | ✅ | ❌ | ❌ | | DiNAT | ❌ | ❌ | ✅ | ❌ | ❌ | | DistilBERT | ✅ | ✅ | ✅ | ✅ | ✅ | | DonutSwin | ❌ | ❌ | ✅ | ❌ | ❌ | | DPR | ✅ | ✅ | ✅ | ✅ | ❌ | | DPT | ❌ | ❌ | ✅ | ❌ | ❌ | | EfficientFormer | ❌ | ❌ | ✅ | ✅ | ❌ | | EfficientNet | ❌ | ❌ | ✅ | ❌ | ❌ | | ELECTRA | ✅ | ✅ | ✅ | ✅ | ✅ | | Encoder decoder | ❌ | ❌ | ✅ | ✅ | ✅ | | ERNIE | ❌ | ❌ | ✅ | ❌ | ❌ | | ErnieM | ✅ | ❌ | ✅ | ❌ | ❌ | | ESM | ✅ | ❌ | ✅ | ✅ | ❌ | | FairSeq Machine-Translation | ✅ | ❌ | ✅ | ❌ | ❌ | | FlauBERT | ✅ | ❌ | ✅ | ✅ | ❌ | | FLAVA | ❌ | ❌ | ✅ | ❌ | ❌ | | FNet | ✅ | ✅ | ✅ | ❌ | ❌ | | FocalNet | ❌ | ❌ | ✅ | ❌ | ❌ | | Funnel Transformer | ✅ | ✅ | ✅ | ✅ | ❌ | | GIT | ❌ | ❌ | ✅ | ❌ | ❌ | | GLPN | ❌ | ❌ | ✅ | ❌ | ❌ | | GPT Neo | ❌ | ❌ | ✅ | ❌ | ✅ | | GPT NeoX | ❌ | ✅ | ✅ | ❌ | ❌ | | GPT NeoX Japanese | ✅ | ❌ | ✅ | ❌ | ❌ | | GPT-J | ❌ | ❌ | ✅ | ✅ | ✅ | | GPT-Sw3 | ✅ | ✅ | ✅ | ✅ | ✅ | | GPTBigCode | ❌ | ❌ | ✅ | ❌ | ❌ | | GPTSAN-japanese | ✅ | ❌ | ✅ | ❌ | ❌ | | Graphormer | ❌ | ❌ | ✅ | ❌ | ❌ | | GroupViT | ❌ | ❌ | ✅ | ✅ | ❌ | | Hubert | ❌ | ❌ | ✅ | ✅ | ❌ | | I-BERT | ❌ | ❌ | ✅ | ❌ | ❌ | | ImageGPT | ❌ | ❌ | ✅ | ❌ | ❌ | | Informer | ❌ | ❌ | ✅ | ❌ | ❌ | | Jukebox | ✅ | ❌ | ✅ | ❌ | ❌ | | LayoutLM | ✅ | ✅ | ✅ | ✅ | ❌ | | LayoutLMv2 | ✅ | ✅ | ✅ | ❌ | ❌ | | LayoutLMv3 | ✅ | ✅ | ✅ | ✅ | ❌ | | LED | ✅ | ✅ | ✅ | ✅ | ❌ | | LeViT | ❌ | ❌ | ✅ | ❌ | ❌ | | LiLT | ❌ | ❌ | ✅ | ❌ | ❌ | | LLaMA | ✅ | ✅ | ✅ | ❌ | ❌ | | Longformer | ✅ | ✅ | ✅ | ✅ | ❌ | | LongT5 | ❌ | ❌ | ✅ | ❌ | ✅ | | LUKE | ✅ | ❌ | ✅ | ❌ | ❌ | | LXMERT | ✅ | ✅ | ✅ | ✅ | ❌ | | M-CTC-T | ❌ | ❌ | ✅ | ❌ | ❌ | | M2M100 | ✅ | ❌ | ✅ | ❌ | ❌ | | Marian | ✅ | ❌ | ✅ | ✅ | ✅ | | MarkupLM | ✅ | ✅ | ✅ | ❌ | ❌ | | Mask2Former | ❌ | ❌ | ✅ | ❌ | ❌ | | MaskFormer | ❌ | ❌ | ✅ | ❌ | ❌ | | MaskFormerSwin | ❌ | ❌ | ❌ | ❌ | ❌ | | mBART | ✅ | ✅ | ✅ | ✅ | ✅ | | MEGA | ❌ | ❌ | ✅ | ❌ | ❌ | | Megatron-BERT | ❌ | ❌ | ✅ | ❌ | ❌ | | MGP-STR | ✅ | ❌ | ✅ | ❌ | ❌ | | MobileBERT | ✅ | ✅ | ✅ | ✅ | ❌ | | MobileNetV1 | ❌ | ❌ | ✅ | ❌ | ❌ | | MobileNetV2 | ❌ | ❌ | ✅ | ❌ | ❌ | | MobileViT | ❌ | ❌ | ✅ | ✅ | ❌ | | MobileViTV2 | ❌ | ❌ | ✅ | ❌ | ❌ | | MPNet | ✅ | ✅ | ✅ | ✅ | ❌ | | MT5 | ✅ | ✅ | ✅ | ✅ | ✅ | | MVP | ✅ | ✅ | ✅ | ❌ | ❌ | | NAT | ❌ | ❌ | ✅ | ❌ | ❌ | | Nezha | ❌ | ❌ | ✅ | ❌ | ❌ | | NLLB-MOE | ❌ | ❌ | ✅ | ❌ | ❌ | | Nyströmformer | ❌ | ❌ | ✅ | ❌ | ❌ | | OneFormer | ❌ | ❌ | ✅ | ❌ | ❌ | | OpenAI GPT | ✅ | ✅ | ✅ | ✅ | ❌ | | OpenAI GPT-2 | ✅ | ✅ | ✅ | ✅ | ✅ | | OpenLlama | ❌ | ❌ | ✅ | ❌ | ❌ | | OPT | ❌ | ❌ | ✅ | ✅ | ✅ | | OWL-ViT | ❌ | ❌ | ✅ | ❌ | ❌ | | Pegasus | ✅ | ✅ | ✅ | ✅ | ✅ | | PEGASUS-X | ❌ | ❌ | ✅ | ❌ | ❌ | | Perceiver | ✅ | ❌ | ✅ | ❌ | ❌ | | Pix2Struct | ❌ | ❌ | ✅ | ❌ | ❌ | | PLBart | ✅ | ❌ | ✅ | ❌ | ❌ | | PoolFormer | ❌ | ❌ | ✅ | ❌ | ❌ | | ProphetNet | ✅ | ❌ | ✅ | ❌ | ❌ | | QDQBert | ❌ | ❌ | ✅ | ❌ | ❌ | | RAG | ✅ | ❌ | ✅ | ✅ | ❌ | | REALM | ✅ | ✅ | ✅ | ❌ | ❌ | | Reformer | ✅ | ✅ | ✅ | ❌ | ❌ | | RegNet | ❌ | ❌ | ✅ | ✅ | ✅ | | RemBERT | ✅ | ✅ | ✅ | ✅ | ❌ | | ResNet | ❌ | ❌ | ✅ | ✅ | ✅ | | RetriBERT | ✅ | ✅ | ✅ | ❌ | ❌ | | RoBERTa | ✅ | ✅ | ✅ | ✅ | ✅ | | RoBERTa-PreLayerNorm | ❌ | ❌ | ✅ | ✅ | ✅ | | RoCBert | ✅ | ❌ | ✅ | ❌ | ❌ | | RoFormer | ✅ | ✅ | ✅ | ✅ | ✅ | | RWKV | ❌ | ❌ | ✅ | ❌ | ❌ | | SAM | ❌ | ❌ | ✅ | ✅ | ❌ | | SegFormer | ❌ | ❌ | ✅ | ✅ | ❌ | | SEW | ❌ | ❌ | ✅ | ❌ | ❌ | | SEW-D | ❌ | ❌ | ✅ | ❌ | ❌ | | Speech Encoder decoder | ❌ | ❌ | ✅ | ❌ | ✅ | | Speech2Text | ✅ | ❌ | ✅ | ✅ | ❌ | | Speech2Text2 | ✅ | ❌ | ❌ | ❌ | ❌ | | SpeechT5 | ✅ | ❌ | ✅ | ❌ | ❌ | | Splinter | ✅ | ✅ | ✅ | ❌ | ❌ | | SqueezeBERT | ✅ | ✅ | ✅ | ❌ | ❌ | | SwiftFormer | ❌ | ❌ | ✅ | ❌ | ❌ | | Swin Transformer | ❌ | ❌ | ✅ | ✅ | ❌ | | Swin Transformer V2 | ❌ | ❌ | ✅ | ❌ | ❌ | | Swin2SR | ❌ | ❌ | ✅ | ❌ | ❌ | | SwitchTransformers | ❌ | ❌ | ✅ | ❌ | ❌ | | T5 | ✅ | ✅ | ✅ | ✅ | ✅ | | Table Transformer | ❌ | ❌ | ✅ | ❌ | ❌ | | TAPAS | ✅ | ❌ | ✅ | ✅ | ❌ | | Time Series Transformer | ❌ | ❌ | ✅ | ❌ | ❌ | | TimeSformer | ❌ | ❌ | ✅ | ❌ | ❌ | | TimmBackbone | ❌ | ❌ | ❌ | ❌ | ❌ | | Trajectory Transformer | ❌ | ❌ | ✅ | ❌ | ❌ | | Transformer-XL | ✅ | ❌ | ✅ | ✅ | ❌ | | TrOCR | ❌ | ❌ | ✅ | ❌ | ❌ | | TVLT | ❌ | ❌ | ✅ | ❌ | ❌ | | UniSpeech | ❌ | ❌ | ✅ | ❌ | ❌ | | UniSpeechSat | ❌ | ❌ | ✅ | ❌ | ❌ | | UPerNet | ❌ | ❌ | ✅ | ❌ | ❌ | | VAN | ❌ | ❌ | ✅ | ❌ | ❌ | | VideoMAE | ❌ | ❌ | ✅ | ❌ | ❌ | | ViLT | ❌ | ❌ | ✅ | ❌ | ❌ | | Vision Encoder decoder | ❌ | ❌ | ✅ | ✅ | ✅ | | VisionTextDualEncoder | ❌ | ❌ | ✅ | ✅ | ✅ | | VisualBERT | ❌ | ❌ | ✅ | ❌ | ❌ | | ViT | ❌ | ❌ | ✅ | ✅ | ✅ | | ViT Hybrid | ❌ | ❌ | ✅ | ❌ | ❌ | | ViTMAE | ❌ | ❌ | ✅ | ✅ | ❌ | | ViTMSN | ❌ | ❌ | ✅ | ❌ | ❌ | | Wav2Vec2 | ✅ | ❌ | ✅ | ✅ | ✅ | | Wav2Vec2-Conformer | ❌ | ❌ | ✅ | ❌ | ❌ | | WavLM | ❌ | ❌ | ✅ | ❌ | ❌ | | Whisper | ✅ | ✅ | ✅ | ✅ | ✅ | | X-CLIP | ❌ | ❌ | ✅ | ❌ | ❌ | | X-MOD | ❌ | ❌ | ✅ | ❌ | ❌ | | XGLM | ✅ | ✅ | ✅ | ✅ | ✅ | | XLM | ✅ | ❌ | ✅ | ✅ | ❌ | | XLM-ProphetNet | ✅ | ❌ | ✅ | ❌ | ❌ | | XLM-RoBERTa | ✅ | ✅ | ✅ | ✅ | ✅ | | XLM-RoBERTa-XL | ❌ | ❌ | ✅ | ❌ | ❌ | | XLNet | ✅ | ✅ | ✅ | ✅ | ❌ | | YOLOS | ❌ | ❌ | ✅ | ❌ | ❌ | | YOSO | ❌ | ❌ | ✅ | ❌ | ❌ |
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="🤗 Transformers"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/index"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>🤗 Transformers</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;transformers&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;if-you-are-looking-for-custom-support-from-the-hugging-face-team&quot;,&quot;title&quot;:&quot;If you are looking for custom support from the Hugging Face team&quot;},{&quot;local&quot;:&quot;contents&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;supported-models&quot;,&quot;title&quot;:&quot;Supported models&quot;},{&quot;local&quot;:&quot;supported-frameworks&quot;,&quot;title&quot;:&quot;Supported frameworks&quot;}],&quot;title&quot;:&quot;Contents&quot;}],&quot;title&quot;:&quot;🤗 Transformers&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;index&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;🤗 Transformers&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">🤗 Transformers</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="transformers" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>🤗 Transformers</span></h1> <p>State-of-the-art Machine Learning for <a href="https://pytorch.org/" rel="nofollow">PyTorch</a>, <a href="https://www.tensorflow.org/" rel="nofollow">TensorFlow</a>, and <a href="https://jax.readthedocs.io/en/latest/" rel="nofollow">JAX</a>.</p> <p>🤗 Transformers provides APIs and tools to easily download and train state-of-the-art pretrained models. Using pretrained models can reduce your compute costs, carbon footprint, and save you the time and resources required to train a model from scratch. These models support common tasks in different modalities, such as:</p> <p>📝 <strong>Natural Language Processing</strong>: text classification, named entity recognition, question answering, language modeling, summarization, translation, multiple choice, and text generation.<br> 🖼️ <strong>Computer Vision</strong>: image classification, object detection, and segmentation.<br> 🗣️ <strong>Audio</strong>: automatic speech recognition and audio classification.<br> 🐙 <strong>Multimodal</strong>: table question answering, optical character recognition, information extraction from scanned documents, video classification, and visual question answering.</p> <p>🤗 Transformers support framework interoperability between PyTorch, TensorFlow, and JAX. This provides the flexibility to use a different framework at each stage of a model’s life; train a model in three lines of code in one framework, and load it for inference in another. Models can also be exported to a format like ONNX and TorchScript for deployment in production environments.</p> <p>Join the growing community on the <a href="https://huggingface.co/models" rel="nofollow">Hub</a>, <a href="https://discuss.huggingface.co/" rel="nofollow">forum</a>, or <a href="https://discord.com/invite/JfAtkvEtRb" rel="nofollow">Discord</a> today!</p> <h2 class="relative group"><a id="if-you-are-looking-for-custom-support-from-the-hugging-face-team" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#if-you-are-looking-for-custom-support-from-the-hugging-face-team"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>If you are looking for custom support from the Hugging Face team</span></h2> <a target="_blank" href="https://huggingface.co/support"><img alt="HuggingFace Expert Acceleration Program" src="https://cdn-media.huggingface.co/marketing/transformers/new-support-improved.png" style="width: 100%; max-width: 600px; border: 1px solid #eee; border-radius: 4px; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);"></a> <h2 class="relative group"><a id="contents" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#contents"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Contents</span></h2> <p>The documentation is organized into five sections:</p> <ul><li><p><strong>GET STARTED</strong> provides a quick tour of the library and installation instructions to get up and running.</p></li> <li><p><strong>TUTORIALS</strong> are a great place to start if you’re a beginner. This section will help you gain the basic skills you need to start using the library.</p></li> <li><p><strong>HOW-TO GUIDES</strong> show you how to achieve a specific goal, like finetuning a pretrained model for language modeling or how to write and share a custom model.</p></li> <li><p><strong>CONCEPTUAL GUIDES</strong> offers more discussion and explanation of the underlying concepts and ideas behind models, tasks, and the design philosophy of 🤗 Transformers.</p></li> <li><p><strong>API</strong> describes all classes and functions:</p> <ul><li><strong>MAIN CLASSES</strong> details the most important classes like configuration, model, tokenizer, and pipeline.</li> <li><strong>MODELS</strong> details the classes and functions related to each model implemented in the library.</li> <li><strong>INTERNAL HELPERS</strong> details utility classes and functions used internally.</li></ul></li></ul> <h3 class="relative group"><a id="supported-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#supported-models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Supported models</span></h3> <ol><li><strong><a href="model_doc/albert">ALBERT</a></strong> (from Google Research and the Toyota Technological Institute at Chicago) released with the paper <a href="https://arxiv.org/abs/1909.11942" rel="nofollow">ALBERT: A Lite BERT for Self-supervised Learning of Language Representations</a>, by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.</li> <li><strong><a href="model_doc/align">ALIGN</a></strong> (from Google Research) released with the paper <a href="https://arxiv.org/abs/2102.05918" rel="nofollow">Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision</a> by Chao Jia, Yinfei Yang, Ye Xia, Yi-Ting Chen, Zarana Parekh, Hieu Pham, Quoc V. Le, Yunhsuan Sung, Zhen Li, Tom Duerig.</li> <li><strong><a href="model_doc/altclip">AltCLIP</a></strong> (from BAAI) released with the paper <a href="https://arxiv.org/abs/2211.06679" rel="nofollow">AltCLIP: Altering the Language Encoder in CLIP for Extended Language Capabilities</a> by Chen, Zhongzhi and Liu, Guang and Zhang, Bo-Wen and Ye, Fulong and Yang, Qinghong and Wu, Ledell.</li> <li><strong><a href="model_doc/audio-spectrogram-transformer">Audio Spectrogram Transformer</a></strong> (from MIT) released with the paper <a href="https://arxiv.org/abs/2104.01778" rel="nofollow">AST: Audio Spectrogram Transformer</a> by Yuan Gong, Yu-An Chung, James Glass.</li> <li><strong><a href="model_doc/autoformer">Autoformer</a></strong> (from Tsinghua University) released with the paper <a href="https://arxiv.org/abs/2106.13008" rel="nofollow">Autoformer: Decomposition Transformers with Auto-Correlation for Long-Term Series Forecasting</a> by Haixu Wu, Jiehui Xu, Jianmin Wang, Mingsheng Long.</li> <li><strong><a href="model_doc/bart">BART</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/1910.13461" rel="nofollow">BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension</a> by Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer.</li> <li><strong><a href="model_doc/barthez">BARThez</a></strong> (from École polytechnique) released with the paper <a href="https://arxiv.org/abs/2010.12321" rel="nofollow">BARThez: a Skilled Pretrained French Sequence-to-Sequence Model</a> by Moussa Kamal Eddine, Antoine J.-P. Tixier, Michalis Vazirgiannis.</li> <li><strong><a href="model_doc/bartpho">BARTpho</a></strong> (from VinAI Research) released with the paper <a href="https://arxiv.org/abs/2109.09701" rel="nofollow">BARTpho: Pre-trained Sequence-to-Sequence Models for Vietnamese</a> by Nguyen Luong Tran, Duong Minh Le and Dat Quoc Nguyen.</li> <li><strong><a href="model_doc/beit">BEiT</a></strong> (from Microsoft) released with the paper <a href="https://arxiv.org/abs/2106.08254" rel="nofollow">BEiT: BERT Pre-Training of Image Transformers</a> by Hangbo Bao, Li Dong, Furu Wei.</li> <li><strong><a href="model_doc/bert">BERT</a></strong> (from Google) released with the paper <a href="https://arxiv.org/abs/1810.04805" rel="nofollow">BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding</a> by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova.</li> <li><strong><a href="model_doc/bert-generation">BERT For Sequence Generation</a></strong> (from Google) released with the paper <a href="https://arxiv.org/abs/1907.12461" rel="nofollow">Leveraging Pre-trained Checkpoints for Sequence Generation Tasks</a> by Sascha Rothe, Shashi Narayan, Aliaksei Severyn.</li> <li><strong><a href="model_doc/bertweet">BERTweet</a></strong> (from VinAI Research) released with the paper <a href="https://aclanthology.org/2020.emnlp-demos.2/" rel="nofollow">BERTweet: A pre-trained language model for English Tweets</a> by Dat Quoc Nguyen, Thanh Vu and Anh Tuan Nguyen.</li> <li><strong><a href="model_doc/bigbird_pegasus">BigBird-Pegasus</a></strong> (from Google Research) released with the paper <a href="https://arxiv.org/abs/2007.14062" rel="nofollow">Big Bird: Transformers for Longer Sequences</a> by Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed.</li> <li><strong><a href="model_doc/big_bird">BigBird-RoBERTa</a></strong> (from Google Research) released with the paper <a href="https://arxiv.org/abs/2007.14062" rel="nofollow">Big Bird: Transformers for Longer Sequences</a> by Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed.</li> <li><strong><a href="model_doc/biogpt">BioGpt</a></strong> (from Microsoft Research AI4Science) released with the paper <a href="https://academic.oup.com/bib/advance-article/doi/10.1093/bib/bbac409/6713511?guestAccessKey=a66d9b5d-4f83-4017-bb52-405815c907b9" rel="nofollow">BioGPT: generative pre-trained transformer for biomedical text generation and mining</a> by Renqian Luo, Liai Sun, Yingce Xia, Tao Qin, Sheng Zhang, Hoifung Poon and Tie-Yan Liu.</li> <li><strong><a href="model_doc/bit">BiT</a></strong> (from Google AI) released with the paper <a href="https://arxiv.org/abs/1912.11370" rel="nofollow">Big Transfer (BiT): General Visual Representation Learning</a> by Alexander Kolesnikov, Lucas Beyer, Xiaohua Zhai, Joan Puigcerver, Jessica Yung, Sylvain Gelly, Neil Houlsby.</li> <li><strong><a href="model_doc/blenderbot">Blenderbot</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2004.13637" rel="nofollow">Recipes for building an open-domain chatbot</a> by Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston.</li> <li><strong><a href="model_doc/blenderbot-small">BlenderbotSmall</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2004.13637" rel="nofollow">Recipes for building an open-domain chatbot</a> by Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston.</li> <li><strong><a href="model_doc/blip">BLIP</a></strong> (from Salesforce) released with the paper <a href="https://arxiv.org/abs/2201.12086" rel="nofollow">BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation</a> by Junnan Li, Dongxu Li, Caiming Xiong, Steven Hoi.</li> <li><strong><a href="model_doc/blip-2">BLIP-2</a></strong> (from Salesforce) released with the paper <a href="https://arxiv.org/abs/2301.12597" rel="nofollow">BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models</a> by Junnan Li, Dongxu Li, Silvio Savarese, Steven Hoi.</li> <li><strong><a href="model_doc/bloom">BLOOM</a></strong> (from BigScience workshop) released by the <a href="https://bigscience.huggingface.co/" rel="nofollow">BigScience Workshop</a>.</li> <li><strong><a href="model_doc/bort">BORT</a></strong> (from Alexa) released with the paper <a href="https://arxiv.org/abs/2010.10499" rel="nofollow">Optimal Subarchitecture Extraction For BERT</a> by Adrian de Wynter and Daniel J. Perry.</li> <li><strong><a href="model_doc/bridgetower">BridgeTower</a></strong> (from Harbin Institute of Technology/Microsoft Research Asia/Intel Labs) released with the paper <a href="https://arxiv.org/abs/2206.08657" rel="nofollow">BridgeTower: Building Bridges Between Encoders in Vision-Language Representation Learning</a> by Xiao Xu, Chenfei Wu, Shachar Rosenman, Vasudev Lal, Wanxiang Che, Nan Duan.</li> <li><strong><a href="model_doc/byt5">ByT5</a></strong> (from Google Research) released with the paper <a href="https://arxiv.org/abs/2105.13626" rel="nofollow">ByT5: Towards a token-free future with pre-trained byte-to-byte models</a> by Linting Xue, Aditya Barua, Noah Constant, Rami Al-Rfou, Sharan Narang, Mihir Kale, Adam Roberts, Colin Raffel.</li> <li><strong><a href="model_doc/camembert">CamemBERT</a></strong> (from Inria/Facebook/Sorbonne) released with the paper <a href="https://arxiv.org/abs/1911.03894" rel="nofollow">CamemBERT: a Tasty French Language Model</a> by Louis Martin<em>, Benjamin Muller</em>, Pedro Javier Ortiz Suárez*, Yoann Dupont, Laurent Romary, Éric Villemonte de la Clergerie, Djamé Seddah and Benoît Sagot.</li> <li><strong><a href="model_doc/canine">CANINE</a></strong> (from Google Research) released with the paper <a href="https://arxiv.org/abs/2103.06874" rel="nofollow">CANINE: Pre-training an Efficient Tokenization-Free Encoder for Language Representation</a> by Jonathan H. Clark, Dan Garrette, Iulia Turc, John Wieting.</li> <li><strong><a href="model_doc/chinese_clip">Chinese-CLIP</a></strong> (from OFA-Sys) released with the paper <a href="https://arxiv.org/abs/2211.01335" rel="nofollow">Chinese CLIP: Contrastive Vision-Language Pretraining in Chinese</a> by An Yang, Junshu Pan, Junyang Lin, Rui Men, Yichang Zhang, Jingren Zhou, Chang Zhou.</li> <li><strong><a href="model_doc/clap">CLAP</a></strong> (from LAION-AI) released with the paper <a href="https://arxiv.org/abs/2211.06687" rel="nofollow">Large-scale Contrastive Language-Audio Pretraining with Feature Fusion and Keyword-to-Caption Augmentation</a> by Yusong Wu, Ke Chen, Tianyu Zhang, Yuchen Hui, Taylor Berg-Kirkpatrick, Shlomo Dubnov.</li> <li><strong><a href="model_doc/clip">CLIP</a></strong> (from OpenAI) released with the paper <a href="https://arxiv.org/abs/2103.00020" rel="nofollow">Learning Transferable Visual Models From Natural Language Supervision</a> by Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever.</li> <li><strong><a href="model_doc/clipseg">CLIPSeg</a></strong> (from University of Göttingen) released with the paper <a href="https://arxiv.org/abs/2112.10003" rel="nofollow">Image Segmentation Using Text and Image Prompts</a> by Timo Lüddecke and Alexander Ecker.</li> <li><strong><a href="model_doc/codegen">CodeGen</a></strong> (from Salesforce) released with the paper <a href="https://arxiv.org/abs/2203.13474" rel="nofollow">A Conversational Paradigm for Program Synthesis</a> by Erik Nijkamp, Bo Pang, Hiroaki Hayashi, Lifu Tu, Huan Wang, Yingbo Zhou, Silvio Savarese, Caiming Xiong.</li> <li><strong><a href="model_doc/conditional_detr">Conditional DETR</a></strong> (from Microsoft Research Asia) released with the paper <a href="https://arxiv.org/abs/2108.06152" rel="nofollow">Conditional DETR for Fast Training Convergence</a> by Depu Meng, Xiaokang Chen, Zejia Fan, Gang Zeng, Houqiang Li, Yuhui Yuan, Lei Sun, Jingdong Wang.</li> <li><strong><a href="model_doc/convbert">ConvBERT</a></strong> (from YituTech) released with the paper <a href="https://arxiv.org/abs/2008.02496" rel="nofollow">ConvBERT: Improving BERT with Span-based Dynamic Convolution</a> by Zihang Jiang, Weihao Yu, Daquan Zhou, Yunpeng Chen, Jiashi Feng, Shuicheng Yan.</li> <li><strong><a href="model_doc/convnext">ConvNeXT</a></strong> (from Facebook AI) released with the paper <a href="https://arxiv.org/abs/2201.03545" rel="nofollow">A ConvNet for the 2020s</a> by Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie.</li> <li><strong><a href="model_doc/convnextv2">ConvNeXTV2</a></strong> (from Facebook AI) released with the paper <a href="https://arxiv.org/abs/2301.00808" rel="nofollow">ConvNeXt V2: Co-designing and Scaling ConvNets with Masked Autoencoders</a> by Sanghyun Woo, Shoubhik Debnath, Ronghang Hu, Xinlei Chen, Zhuang Liu, In So Kweon, Saining Xie.</li> <li><strong><a href="model_doc/cpm">CPM</a></strong> (from Tsinghua University) released with the paper <a href="https://arxiv.org/abs/2012.00413" rel="nofollow">CPM: A Large-scale Generative Chinese Pre-trained Language Model</a> by Zhengyan Zhang, Xu Han, Hao Zhou, Pei Ke, Yuxian Gu, Deming Ye, Yujia Qin, Yusheng Su, Haozhe Ji, Jian Guan, Fanchao Qi, Xiaozhi Wang, Yanan Zheng, Guoyang Zeng, Huanqi Cao, Shengqi Chen, Daixuan Li, Zhenbo Sun, Zhiyuan Liu, Minlie Huang, Wentao Han, Jie Tang, Juanzi Li, Xiaoyan Zhu, Maosong Sun.</li> <li><strong><a href="model_doc/cpmant">CPM-Ant</a></strong> (from OpenBMB) released by the <a href="https://www.openbmb.org/" rel="nofollow">OpenBMB</a>.</li> <li><strong><a href="model_doc/ctrl">CTRL</a></strong> (from Salesforce) released with the paper <a href="https://arxiv.org/abs/1909.05858" rel="nofollow">CTRL: A Conditional Transformer Language Model for Controllable Generation</a> by Nitish Shirish Keskar<em>, Bryan McCann</em>, Lav R. Varshney, Caiming Xiong and Richard Socher.</li> <li><strong><a href="model_doc/cvt">CvT</a></strong> (from Microsoft) released with the paper <a href="https://arxiv.org/abs/2103.15808" rel="nofollow">CvT: Introducing Convolutions to Vision Transformers</a> by Haiping Wu, Bin Xiao, Noel Codella, Mengchen Liu, Xiyang Dai, Lu Yuan, Lei Zhang.</li> <li><strong><a href="model_doc/data2vec">Data2Vec</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2202.03555" rel="nofollow">Data2Vec: A General Framework for Self-supervised Learning in Speech, Vision and Language</a> by Alexei Baevski, Wei-Ning Hsu, Qiantong Xu, Arun Babu, Jiatao Gu, Michael Auli.</li> <li><strong><a href="model_doc/deberta">DeBERTa</a></strong> (from Microsoft) released with the paper <a href="https://arxiv.org/abs/2006.03654" rel="nofollow">DeBERTa: Decoding-enhanced BERT with Disentangled Attention</a> by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen.</li> <li><strong><a href="model_doc/deberta-v2">DeBERTa-v2</a></strong> (from Microsoft) released with the paper <a href="https://arxiv.org/abs/2006.03654" rel="nofollow">DeBERTa: Decoding-enhanced BERT with Disentangled Attention</a> by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen.</li> <li><strong><a href="model_doc/decision_transformer">Decision Transformer</a></strong> (from Berkeley/Facebook/Google) released with the paper <a href="https://arxiv.org/abs/2106.01345" rel="nofollow">Decision Transformer: Reinforcement Learning via Sequence Modeling</a> by Lili Chen, Kevin Lu, Aravind Rajeswaran, Kimin Lee, Aditya Grover, Michael Laskin, Pieter Abbeel, Aravind Srinivas, Igor Mordatch.</li> <li><strong><a href="model_doc/deformable_detr">Deformable DETR</a></strong> (from SenseTime Research) released with the paper <a href="https://arxiv.org/abs/2010.04159" rel="nofollow">Deformable DETR: Deformable Transformers for End-to-End Object Detection</a> by Xizhou Zhu, Weijie Su, Lewei Lu, Bin Li, Xiaogang Wang, Jifeng Dai.</li> <li><strong><a href="model_doc/deit">DeiT</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2012.12877" rel="nofollow">Training data-efficient image transformers &amp; distillation through attention</a> by Hugo Touvron, Matthieu Cord, Matthijs Douze, Francisco Massa, Alexandre Sablayrolles, Hervé Jégou.</li> <li><strong><a href="model_doc/deplot">DePlot</a></strong> (from Google AI) released with the paper <a href="https://arxiv.org/abs/2212.10505" rel="nofollow">DePlot: One-shot visual language reasoning by plot-to-table translation</a> by Fangyu Liu, Julian Martin Eisenschlos, Francesco Piccinno, Syrine Krichene, Chenxi Pang, Kenton Lee, Mandar Joshi, Wenhu Chen, Nigel Collier, Yasemin Altun.</li> <li><strong><a href="model_doc/deta">DETA</a></strong> (from The University of Texas at Austin) released with the paper <a href="https://arxiv.org/abs/2212.06137" rel="nofollow">NMS Strikes Back</a> by Jeffrey Ouyang-Zhang, Jang Hyun Cho, Xingyi Zhou, Philipp Krähenbühl.</li> <li><strong><a href="model_doc/detr">DETR</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2005.12872" rel="nofollow">End-to-End Object Detection with Transformers</a> by Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov, Sergey Zagoruyko.</li> <li><strong><a href="model_doc/dialogpt">DialoGPT</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/1911.00536" rel="nofollow">DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation</a> by Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao, Jianfeng Gao, Jingjing Liu, Bill Dolan.</li> <li><strong><a href="model_doc/dinat">DiNAT</a></strong> (from SHI Labs) released with the paper <a href="https://arxiv.org/abs/2209.15001" rel="nofollow">Dilated Neighborhood Attention Transformer</a> by Ali Hassani and Humphrey Shi.</li> <li><strong><a href="model_doc/distilbert">DistilBERT</a></strong> (from HuggingFace), released together with the paper <a href="https://arxiv.org/abs/1910.01108" rel="nofollow">DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter</a> by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into <a href="https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation" rel="nofollow">DistilGPT2</a>, RoBERTa into <a href="https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation" rel="nofollow">DistilRoBERTa</a>, Multilingual BERT into <a href="https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation" rel="nofollow">DistilmBERT</a> and a German version of DistilBERT.</li> <li><strong><a href="model_doc/dit">DiT</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2203.02378" rel="nofollow">DiT: Self-supervised Pre-training for Document Image Transformer</a> by Junlong Li, Yiheng Xu, Tengchao Lv, Lei Cui, Cha Zhang, Furu Wei.</li> <li><strong><a href="model_doc/donut">Donut</a></strong> (from NAVER), released together with the paper <a href="https://arxiv.org/abs/2111.15664" rel="nofollow">OCR-free Document Understanding Transformer</a> by Geewook Kim, Teakgyu Hong, Moonbin Yim, Jeongyeon Nam, Jinyoung Park, Jinyeong Yim, Wonseok Hwang, Sangdoo Yun, Dongyoon Han, Seunghyun Park.</li> <li><strong><a href="model_doc/dpr">DPR</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2004.04906" rel="nofollow">Dense Passage Retrieval for Open-Domain Question Answering</a> by Vladimir Karpukhin, Barlas Oğuz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, and Wen-tau Yih.</li> <li><strong><a href="master/model_doc/dpt">DPT</a></strong> (from Intel Labs) released with the paper <a href="https://arxiv.org/abs/2103.13413" rel="nofollow">Vision Transformers for Dense Prediction</a> by René Ranftl, Alexey Bochkovskiy, Vladlen Koltun.</li> <li><strong><a href="model_doc/efficientformer">EfficientFormer</a></strong> (from Snap Research) released with the paper <a href="https://arxiv.org/abs/2206.01191" rel="nofollow">EfficientFormer: Vision Transformers at MobileNetSpeed</a> by Yanyu Li, Geng Yuan, Yang Wen, Ju Hu, Georgios Evangelidis, Sergey Tulyakov, Yanzhi Wang, Jian Ren.</li> <li><strong><a href="model_doc/efficientnet">EfficientNet</a></strong> (from Google Brain) released with the paper <a href="https://arxiv.org/abs/1905.11946" rel="nofollow">EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks</a> by Mingxing Tan, Quoc V. Le.</li> <li><strong><a href="model_doc/electra">ELECTRA</a></strong> (from Google Research/Stanford University) released with the paper <a href="https://arxiv.org/abs/2003.10555" rel="nofollow">ELECTRA: Pre-training text encoders as discriminators rather than generators</a> by Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning.</li> <li><strong><a href="model_doc/encoder-decoder">EncoderDecoder</a></strong> (from Google Research) released with the paper <a href="https://arxiv.org/abs/1907.12461" rel="nofollow">Leveraging Pre-trained Checkpoints for Sequence Generation Tasks</a> by Sascha Rothe, Shashi Narayan, Aliaksei Severyn.</li> <li><strong><a href="model_doc/ernie">ERNIE</a></strong> (from Baidu) released with the paper <a href="https://arxiv.org/abs/1904.09223" rel="nofollow">ERNIE: Enhanced Representation through Knowledge Integration</a> by Yu Sun, Shuohuan Wang, Yukun Li, Shikun Feng, Xuyi Chen, Han Zhang, Xin Tian, Danxiang Zhu, Hao Tian, Hua Wu.</li> <li><strong><a href="model_doc/ernie_m">ErnieM</a></strong> (from Baidu) released with the paper <a href="https://arxiv.org/abs/2012.15674" rel="nofollow">ERNIE-M: Enhanced Multilingual Representation by Aligning Cross-lingual Semantics with Monolingual Corpora</a> by Xuan Ouyang, Shuohuan Wang, Chao Pang, Yu Sun, Hao Tian, Hua Wu, Haifeng Wang.</li> <li><strong><a href="model_doc/esm">ESM</a></strong> (from Meta AI) are transformer protein language models. <strong>ESM-1b</strong> was released with the paper <a href="https://www.pnas.org/content/118/15/e2016239118" rel="nofollow">Biological structure and function emerge from scaling unsupervised learning to 250 million protein sequences</a> by Alexander Rives, Joshua Meier, Tom Sercu, Siddharth Goyal, Zeming Lin, Jason Liu, Demi Guo, Myle Ott, C. Lawrence Zitnick, Jerry Ma, and Rob Fergus. <strong>ESM-1v</strong> was released with the paper <a href="https://doi.org/10.1101/2021.07.09.450648" rel="nofollow">Language models enable zero-shot prediction of the effects of mutations on protein function</a> by Joshua Meier, Roshan Rao, Robert Verkuil, Jason Liu, Tom Sercu and Alexander Rives. <strong>ESM-2 and ESMFold</strong> were released with the paper <a href="https://doi.org/10.1101/2022.07.20.500902" rel="nofollow">Language models of protein sequences at the scale of evolution enable accurate structure prediction</a> by Zeming Lin, Halil Akin, Roshan Rao, Brian Hie, Zhongkai Zhu, Wenting Lu, Allan dos Santos Costa, Maryam Fazel-Zarandi, Tom Sercu, Sal Candido, Alexander Rives.</li> <li><strong><a href="model_doc/flan-t5">FLAN-T5</a></strong> (from Google AI) released in the repository <a href="https://github.com/google-research/t5x/blob/main/docs/models.md#flan-t5-checkpoints" rel="nofollow">google-research/t5x</a> by Hyung Won Chung, Le Hou, Shayne Longpre, Barret Zoph, Yi Tay, William Fedus, Eric Li, Xuezhi Wang, Mostafa Dehghani, Siddhartha Brahma, Albert Webson, Shixiang Shane Gu, Zhuyun Dai, Mirac Suzgun, Xinyun Chen, Aakanksha Chowdhery, Sharan Narang, Gaurav Mishra, Adams Yu, Vincent Zhao, Yanping Huang, Andrew Dai, Hongkun Yu, Slav Petrov, Ed H. Chi, Jeff Dean, Jacob Devlin, Adam Roberts, Denny Zhou, Quoc V. Le, and Jason Wei</li> <li><strong><a href="model_doc/flan-ul2">FLAN-UL2</a></strong> (from Google AI) released in the repository <a href="https://github.com/google-research/t5x/blob/main/docs/models.md#flan-ul2-checkpoints" rel="nofollow">google-research/t5x</a> by Hyung Won Chung, Le Hou, Shayne Longpre, Barret Zoph, Yi Tay, William Fedus, Eric Li, Xuezhi Wang, Mostafa Dehghani, Siddhartha Brahma, Albert Webson, Shixiang Shane Gu, Zhuyun Dai, Mirac Suzgun, Xinyun Chen, Aakanksha Chowdhery, Sharan Narang, Gaurav Mishra, Adams Yu, Vincent Zhao, Yanping Huang, Andrew Dai, Hongkun Yu, Slav Petrov, Ed H. Chi, Jeff Dean, Jacob Devlin, Adam Roberts, Denny Zhou, Quoc V. Le, and Jason Wei</li> <li><strong><a href="model_doc/flaubert">FlauBERT</a></strong> (from CNRS) released with the paper <a href="https://arxiv.org/abs/1912.05372" rel="nofollow">FlauBERT: Unsupervised Language Model Pre-training for French</a> by Hang Le, Loïc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoît Crabbé, Laurent Besacier, Didier Schwab.</li> <li><strong><a href="model_doc/flava">FLAVA</a></strong> (from Facebook AI) released with the paper <a href="https://arxiv.org/abs/2112.04482" rel="nofollow">FLAVA: A Foundational Language And Vision Alignment Model</a> by Amanpreet Singh, Ronghang Hu, Vedanuj Goswami, Guillaume Couairon, Wojciech Galuba, Marcus Rohrbach, and Douwe Kiela.</li> <li><strong><a href="model_doc/fnet">FNet</a></strong> (from Google Research) released with the paper <a href="https://arxiv.org/abs/2105.03824" rel="nofollow">FNet: Mixing Tokens with Fourier Transforms</a> by James Lee-Thorp, Joshua Ainslie, Ilya Eckstein, Santiago Ontanon.</li> <li><strong><a href="model_doc/focalnet">FocalNet</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2203.11926" rel="nofollow">Focal Modulation Networks</a> by Jianwei Yang, Chunyuan Li, Xiyang Dai, Lu Yuan, Jianfeng Gao.</li> <li><strong><a href="model_doc/funnel">Funnel Transformer</a></strong> (from CMU/Google Brain) released with the paper <a href="https://arxiv.org/abs/2006.03236" rel="nofollow">Funnel-Transformer: Filtering out Sequential Redundancy for Efficient Language Processing</a> by Zihang Dai, Guokun Lai, Yiming Yang, Quoc V. Le.</li> <li><strong><a href="model_doc/git">GIT</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2205.14100" rel="nofollow">GIT: A Generative Image-to-text Transformer for Vision and Language</a> by Jianfeng Wang, Zhengyuan Yang, Xiaowei Hu, Linjie Li, Kevin Lin, Zhe Gan, Zicheng Liu, Ce Liu, Lijuan Wang.</li> <li><strong><a href="model_doc/glpn">GLPN</a></strong> (from KAIST) released with the paper <a href="https://arxiv.org/abs/2201.07436" rel="nofollow">Global-Local Path Networks for Monocular Depth Estimation with Vertical CutDepth</a> by Doyeon Kim, Woonghyun Ga, Pyungwhan Ahn, Donggyu Joo, Sehwan Chun, Junmo Kim.</li> <li><strong><a href="model_doc/openai-gpt">GPT</a></strong> (from OpenAI) released with the paper <a href="https://blog.openai.com/language-unsupervised/" rel="nofollow">Improving Language Understanding by Generative Pre-Training</a> by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever.</li> <li><strong><a href="model_doc/gpt_neo">GPT Neo</a></strong> (from EleutherAI) released in the repository <a href="https://github.com/EleutherAI/gpt-neo" rel="nofollow">EleutherAI/gpt-neo</a> by Sid Black, Stella Biderman, Leo Gao, Phil Wang and Connor Leahy.</li> <li><strong><a href="model_doc/gpt_neox">GPT NeoX</a></strong> (from EleutherAI) released with the paper <a href="https://arxiv.org/abs/2204.06745" rel="nofollow">GPT-NeoX-20B: An Open-Source Autoregressive Language Model</a> by Sid Black, Stella Biderman, Eric Hallahan, Quentin Anthony, Leo Gao, Laurence Golding, Horace He, Connor Leahy, Kyle McDonell, Jason Phang, Michael Pieler, USVSN Sai Prashanth, Shivanshu Purohit, Laria Reynolds, Jonathan Tow, Ben Wang, Samuel Weinbach</li> <li><strong><a href="model_doc/gpt_neox_japanese">GPT NeoX Japanese</a></strong> (from ABEJA) released by Shinya Otani, Takayoshi Makabe, Anuj Arora, and Kyo Hattori.</li> <li><strong><a href="model_doc/gpt2">GPT-2</a></strong> (from OpenAI) released with the paper <a href="https://blog.openai.com/better-language-models/" rel="nofollow">Language Models are Unsupervised Multitask Learners</a> by Alec Radford<em>, Jeffrey Wu</em>, Rewon Child, David Luan, Dario Amodei<strong>and Ilya Sutskever</strong>.</li> <li><strong><a href="model_doc/gptj">GPT-J</a></strong> (from EleutherAI) released in the repository <a href="https://github.com/kingoflolz/mesh-transformer-jax/" rel="nofollow">kingoflolz/mesh-transformer-jax</a> by Ben Wang and Aran Komatsuzaki.</li> <li><strong><a href="model_doc/gpt-sw3">GPT-Sw3</a></strong> (from AI-Sweden) released with the paper <a href="http://www.lrec-conf.org/proceedings/lrec2022/pdf/2022.lrec-1.376.pdf" rel="nofollow">Lessons Learned from GPT-SW3: Building the First Large-Scale Generative Language Model for Swedish</a> by Ariel Ekgren, Amaru Cuba Gyllensten, Evangelia Gogoulou, Alice Heiman, Severine Verlinden, Joey Öhman, Fredrik Carlsson, Magnus Sahlgren.</li> <li><strong><a href="model_doc/gpt_bigcode">GPTBigCode</a></strong> (from BigCode) released with the paper <a href="https://arxiv.org/abs/2301.03988" rel="nofollow">SantaCoder: don’t reach for the stars!</a> by Loubna Ben Allal, Raymond Li, Denis Kocetkov, Chenghao Mou, Christopher Akiki, Carlos Munoz Ferrandis, Niklas Muennighoff, Mayank Mishra, Alex Gu, Manan Dey, Logesh Kumar Umapathi, Carolyn Jane Anderson, Yangtian Zi, Joel Lamy Poirier, Hailey Schoelkopf, Sergey Troshin, Dmitry Abulkhanov, Manuel Romero, Michael Lappert, Francesco De Toni, Bernardo García del Río, Qian Liu, Shamik Bose, Urvashi Bhattacharyya, Terry Yue Zhuo, Ian Yu, Paulo Villegas, Marco Zocca, Sourab Mangrulkar, David Lansky, Huu Nguyen, Danish Contractor, Luis Villa, Jia Li, Dzmitry Bahdanau, Yacine Jernite, Sean Hughes, Daniel Fried, Arjun Guha, Harm de Vries, Leandro von Werra.</li> <li><strong><a href="model_doc/gptsan-japanese">GPTSAN-japanese</a></strong> released in the repository <a href="https://github.com/tanreinama/GPTSAN/blob/main/report/model.md" rel="nofollow">tanreinama/GPTSAN</a> by Toshiyuki Sakamoto(tanreinama).</li> <li><strong><a href="model_doc/graphormer">Graphormer</a></strong> (from Microsoft) released with the paper <a href="https://arxiv.org/abs/2106.05234" rel="nofollow">Do Transformers Really Perform Bad for Graph Representation?</a> by Chengxuan Ying, Tianle Cai, Shengjie Luo, Shuxin Zheng, Guolin Ke, Di He, Yanming Shen, Tie-Yan Liu.</li> <li><strong><a href="model_doc/groupvit">GroupViT</a></strong> (from UCSD, NVIDIA) released with the paper <a href="https://arxiv.org/abs/2202.11094" rel="nofollow">GroupViT: Semantic Segmentation Emerges from Text Supervision</a> by Jiarui Xu, Shalini De Mello, Sifei Liu, Wonmin Byeon, Thomas Breuel, Jan Kautz, Xiaolong Wang.</li> <li><strong><a href="model_doc/hubert">Hubert</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2106.07447" rel="nofollow">HuBERT: Self-Supervised Speech Representation Learning by Masked Prediction of Hidden Units</a> by Wei-Ning Hsu, Benjamin Bolte, Yao-Hung Hubert Tsai, Kushal Lakhotia, Ruslan Salakhutdinov, Abdelrahman Mohamed.</li> <li><strong><a href="model_doc/ibert">I-BERT</a></strong> (from Berkeley) released with the paper <a href="https://arxiv.org/abs/2101.01321" rel="nofollow">I-BERT: Integer-only BERT Quantization</a> by Sehoon Kim, Amir Gholami, Zhewei Yao, Michael W. Mahoney, Kurt Keutzer.</li> <li><strong><a href="model_doc/imagegpt">ImageGPT</a></strong> (from OpenAI) released with the paper <a href="https://openai.com/blog/image-gpt/" rel="nofollow">Generative Pretraining from Pixels</a> by Mark Chen, Alec Radford, Rewon Child, Jeffrey Wu, Heewoo Jun, David Luan, Ilya Sutskever.</li> <li><strong><a href="model_doc/informer">Informer</a></strong> (from Beihang University, UC Berkeley, Rutgers University, SEDD Company) released with the paper <a href="https://arxiv.org/abs/2012.07436" rel="nofollow">Informer: Beyond Efficient Transformer for Long Sequence Time-Series Forecasting</a> by Haoyi Zhou, Shanghang Zhang, Jieqi Peng, Shuai Zhang, Jianxin Li, Hui Xiong, and Wancai Zhang.</li> <li><strong><a href="model_doc/jukebox">Jukebox</a></strong> (from OpenAI) released with the paper <a href="https://arxiv.org/pdf/2005.00341.pdf" rel="nofollow">Jukebox: A Generative Model for Music</a> by Prafulla Dhariwal, Heewoo Jun, Christine Payne, Jong Wook Kim, Alec Radford, Ilya Sutskever.</li> <li><strong><a href="model_doc/layoutlm">LayoutLM</a></strong> (from Microsoft Research Asia) released with the paper <a href="https://arxiv.org/abs/1912.13318" rel="nofollow">LayoutLM: Pre-training of Text and Layout for Document Image Understanding</a> by Yiheng Xu, Minghao Li, Lei Cui, Shaohan Huang, Furu Wei, Ming Zhou.</li> <li><strong><a href="model_doc/layoutlmv2">LayoutLMv2</a></strong> (from Microsoft Research Asia) released with the paper <a href="https://arxiv.org/abs/2012.14740" rel="nofollow">LayoutLMv2: Multi-modal Pre-training for Visually-Rich Document Understanding</a> by Yang Xu, Yiheng Xu, Tengchao Lv, Lei Cui, Furu Wei, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Wanxiang Che, Min Zhang, Lidong Zhou.</li> <li><strong><a href="model_doc/layoutlmv3">LayoutLMv3</a></strong> (from Microsoft Research Asia) released with the paper <a href="https://arxiv.org/abs/2204.08387" rel="nofollow">LayoutLMv3: Pre-training for Document AI with Unified Text and Image Masking</a> by Yupan Huang, Tengchao Lv, Lei Cui, Yutong Lu, Furu Wei.</li> <li><strong><a href="model_doc/layoutxlm">LayoutXLM</a></strong> (from Microsoft Research Asia) released with the paper <a href="https://arxiv.org/abs/2104.08836" rel="nofollow">LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding</a> by Yiheng Xu, Tengchao Lv, Lei Cui, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Furu Wei.</li> <li><strong><a href="model_doc/led">LED</a></strong> (from AllenAI) released with the paper <a href="https://arxiv.org/abs/2004.05150" rel="nofollow">Longformer: The Long-Document Transformer</a> by Iz Beltagy, Matthew E. Peters, Arman Cohan.</li> <li><strong><a href="model_doc/levit">LeViT</a></strong> (from Meta AI) released with the paper <a href="https://arxiv.org/abs/2104.01136" rel="nofollow">LeViT: A Vision Transformer in ConvNet’s Clothing for Faster Inference</a> by Ben Graham, Alaaeldin El-Nouby, Hugo Touvron, Pierre Stock, Armand Joulin, Hervé Jégou, Matthijs Douze.</li> <li><strong><a href="model_doc/lilt">LiLT</a></strong> (from South China University of Technology) released with the paper <a href="https://arxiv.org/abs/2202.13669" rel="nofollow">LiLT: A Simple yet Effective Language-Independent Layout Transformer for Structured Document Understanding</a> by Jiapeng Wang, Lianwen Jin, Kai Ding.</li> <li><strong><a href="model_doc/llama">LLaMA</a></strong> (from The FAIR team of Meta AI) released with the paper <a href="https://arxiv.org/abs/2302.13971" rel="nofollow">LLaMA: Open and Efficient Foundation Language Models</a> by Hugo Touvron, Thibaut Lavril, Gautier Izacard, Xavier Martinet, Marie-Anne Lachaux, Timothée Lacroix, Baptiste Rozière, Naman Goyal, Eric Hambro, Faisal Azhar, Aurelien Rodriguez, Armand Joulin, Edouard Grave, Guillaume Lample.</li> <li><strong><a href="model_doc/longformer">Longformer</a></strong> (from AllenAI) released with the paper <a href="https://arxiv.org/abs/2004.05150" rel="nofollow">Longformer: The Long-Document Transformer</a> by Iz Beltagy, Matthew E. Peters, Arman Cohan.</li> <li><strong><a href="model_doc/longt5">LongT5</a></strong> (from Google AI) released with the paper <a href="https://arxiv.org/abs/2112.07916" rel="nofollow">LongT5: Efficient Text-To-Text Transformer for Long Sequences</a> by Mandy Guo, Joshua Ainslie, David Uthus, Santiago Ontanon, Jianmo Ni, Yun-Hsuan Sung, Yinfei Yang.</li> <li><strong><a href="model_doc/luke">LUKE</a></strong> (from Studio Ousia) released with the paper <a href="https://arxiv.org/abs/2010.01057" rel="nofollow">LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention</a> by Ikuya Yamada, Akari Asai, Hiroyuki Shindo, Hideaki Takeda, Yuji Matsumoto.</li> <li><strong><a href="model_doc/lxmert">LXMERT</a></strong> (from UNC Chapel Hill) released with the paper <a href="https://arxiv.org/abs/1908.07490" rel="nofollow">LXMERT: Learning Cross-Modality Encoder Representations from Transformers for Open-Domain Question Answering</a> by Hao Tan and Mohit Bansal.</li> <li><strong><a href="model_doc/mctct">M-CTC-T</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2111.00161" rel="nofollow">Pseudo-Labeling For Massively Multilingual Speech Recognition</a> by Loren Lugosch, Tatiana Likhomanenko, Gabriel Synnaeve, and Ronan Collobert.</li> <li><strong><a href="model_doc/m2m_100">M2M100</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2010.11125" rel="nofollow">Beyond English-Centric Multilingual Machine Translation</a> by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin.</li> <li><strong><a href="model_doc/marian">MarianMT</a></strong> Machine translation models trained using <a href="http://opus.nlpl.eu/" rel="nofollow">OPUS</a> data by Jörg Tiedemann. The <a href="https://marian-nmt.github.io/" rel="nofollow">Marian Framework</a> is being developed by the Microsoft Translator Team.</li> <li><strong><a href="model_doc/markuplm">MarkupLM</a></strong> (from Microsoft Research Asia) released with the paper <a href="https://arxiv.org/abs/2110.08518" rel="nofollow">MarkupLM: Pre-training of Text and Markup Language for Visually-rich Document Understanding</a> by Junlong Li, Yiheng Xu, Lei Cui, Furu Wei.</li> <li><strong><a href="model_doc/mask2former">Mask2Former</a></strong> (from FAIR and UIUC) released with the paper <a href="https://arxiv.org/abs/2112.01527" rel="nofollow">Masked-attention Mask Transformer for Universal Image Segmentation</a> by Bowen Cheng, Ishan Misra, Alexander G. Schwing, Alexander Kirillov, Rohit Girdhar.</li> <li><strong><a href="model_doc/maskformer">MaskFormer</a></strong> (from Meta and UIUC) released with the paper <a href="https://arxiv.org/abs/2107.06278" rel="nofollow">Per-Pixel Classification is Not All You Need for Semantic Segmentation</a> by Bowen Cheng, Alexander G. Schwing, Alexander Kirillov.</li> <li><strong><a href="model_doc/matcha">MatCha</a></strong> (from Google AI) released with the paper <a href="https://arxiv.org/abs/2212.09662" rel="nofollow">MatCha: Enhancing Visual Language Pretraining with Math Reasoning and Chart Derendering</a> by Fangyu Liu, Francesco Piccinno, Syrine Krichene, Chenxi Pang, Kenton Lee, Mandar Joshi, Yasemin Altun, Nigel Collier, Julian Martin Eisenschlos.</li> <li><strong><a href="model_doc/mbart">mBART</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2001.08210" rel="nofollow">Multilingual Denoising Pre-training for Neural Machine Translation</a> by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer.</li> <li><strong><a href="model_doc/mbart">mBART-50</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2008.00401" rel="nofollow">Multilingual Translation with Extensible Multilingual Pretraining and Finetuning</a> by Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav Chaudhary, Jiatao Gu, Angela Fan.</li> <li><strong><a href="model_doc/mega">MEGA</a></strong> (from Meta/USC/CMU/SJTU) released with the paper <a href="https://arxiv.org/abs/2209.10655" rel="nofollow">Mega: Moving Average Equipped Gated Attention</a> by Xuezhe Ma, Chunting Zhou, Xiang Kong, Junxian He, Liangke Gui, Graham Neubig, Jonathan May, and Luke Zettlemoyer.</li> <li><strong><a href="model_doc/megatron-bert">Megatron-BERT</a></strong> (from NVIDIA) released with the paper <a href="https://arxiv.org/abs/1909.08053" rel="nofollow">Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism</a> by Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro.</li> <li><strong><a href="model_doc/megatron_gpt2">Megatron-GPT2</a></strong> (from NVIDIA) released with the paper <a href="https://arxiv.org/abs/1909.08053" rel="nofollow">Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism</a> by Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley, Jared Casper and Bryan Catanzaro.</li> <li><strong><a href="model_doc/mgp-str">MGP-STR</a></strong> (from Alibaba Research) released with the paper <a href="https://arxiv.org/abs/2209.03592" rel="nofollow">Multi-Granularity Prediction for Scene Text Recognition</a> by Peng Wang, Cheng Da, and Cong Yao.</li> <li><strong><a href="model_doc/mluke">mLUKE</a></strong> (from Studio Ousia) released with the paper <a href="https://arxiv.org/abs/2110.08151" rel="nofollow">mLUKE: The Power of Entity Representations in Multilingual Pretrained Language Models</a> by Ryokan Ri, Ikuya Yamada, and Yoshimasa Tsuruoka.</li> <li><strong><a href="model_doc/mms">MMS</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2305.13516" rel="nofollow">Scaling Speech Technology to 1,000+ Languages</a> by Vineel Pratap, Andros Tjandra, Bowen Shi, Paden Tomasello, Arun Babu, Sayani Kundu, Ali Elkahky, Zhaoheng Ni, Apoorv Vyas, Maryam Fazel-Zarandi, Alexei Baevski, Yossi Adi, Xiaohui Zhang, Wei-Ning Hsu, Alexis Conneau, Michael Auli.</li> <li><strong><a href="model_doc/mobilebert">MobileBERT</a></strong> (from CMU/Google Brain) released with the paper <a href="https://arxiv.org/abs/2004.02984" rel="nofollow">MobileBERT: a Compact Task-Agnostic BERT for Resource-Limited Devices</a> by Zhiqing Sun, Hongkun Yu, Xiaodan Song, Renjie Liu, Yiming Yang, and Denny Zhou.</li> <li><strong><a href="model_doc/mobilenet_v1">MobileNetV1</a></strong> (from Google Inc.) released with the paper <a href="https://arxiv.org/abs/1704.04861" rel="nofollow">MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications</a> by Andrew G. Howard, Menglong Zhu, Bo Chen, Dmitry Kalenichenko, Weijun Wang, Tobias Weyand, Marco Andreetto, Hartwig Adam.</li> <li><strong><a href="model_doc/mobilenet_v2">MobileNetV2</a></strong> (from Google Inc.) released with the paper <a href="https://arxiv.org/abs/1801.04381" rel="nofollow">MobileNetV2: Inverted Residuals and Linear Bottlenecks</a> by Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen.</li> <li><strong><a href="model_doc/mobilevit">MobileViT</a></strong> (from Apple) released with the paper <a href="https://arxiv.org/abs/2110.02178" rel="nofollow">MobileViT: Light-weight, General-purpose, and Mobile-friendly Vision Transformer</a> by Sachin Mehta and Mohammad Rastegari.</li> <li><strong><a href="model_doc/mobilevitv2">MobileViTV2</a></strong> (from Apple) released with the paper <a href="https://arxiv.org/abs/2206.02680" rel="nofollow">Separable Self-attention for Mobile Vision Transformers</a> by Sachin Mehta and Mohammad Rastegari.</li> <li><strong><a href="model_doc/mpnet">MPNet</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2004.09297" rel="nofollow">MPNet: Masked and Permuted Pre-training for Language Understanding</a> by Kaitao Song, Xu Tan, Tao Qin, Jianfeng Lu, Tie-Yan Liu.</li> <li><strong><a href="model_doc/mt5">MT5</a></strong> (from Google AI) released with the paper <a href="https://arxiv.org/abs/2010.11934" rel="nofollow">mT5: A massively multilingual pre-trained text-to-text transformer</a> by Linting Xue, Noah Constant, Adam Roberts, Mihir Kale, Rami Al-Rfou, Aditya Siddhant, Aditya Barua, Colin Raffel.</li> <li><strong><a href="model_doc/mvp">MVP</a></strong> (from RUC AI Box) released with the paper <a href="https://arxiv.org/abs/2206.12131" rel="nofollow">MVP: Multi-task Supervised Pre-training for Natural Language Generation</a> by Tianyi Tang, Junyi Li, Wayne Xin Zhao and Ji-Rong Wen.</li> <li><strong><a href="model_doc/nat">NAT</a></strong> (from SHI Labs) released with the paper <a href="https://arxiv.org/abs/2204.07143" rel="nofollow">Neighborhood Attention Transformer</a> by Ali Hassani, Steven Walton, Jiachen Li, Shen Li, and Humphrey Shi.</li> <li><strong><a href="model_doc/nezha">Nezha</a></strong> (from Huawei Noah’s Ark Lab) released with the paper <a href="https://arxiv.org/abs/1909.00204" rel="nofollow">NEZHA: Neural Contextualized Representation for Chinese Language Understanding</a> by Junqiu Wei, Xiaozhe Ren, Xiaoguang Li, Wenyong Huang, Yi Liao, Yasheng Wang, Jiashu Lin, Xin Jiang, Xiao Chen and Qun Liu.</li> <li><strong><a href="model_doc/nllb">NLLB</a></strong> (from Meta) released with the paper <a href="https://arxiv.org/abs/2207.04672" rel="nofollow">No Language Left Behind: Scaling Human-Centered Machine Translation</a> by the NLLB team.</li> <li><strong><a href="model_doc/nllb-moe">NLLB-MOE</a></strong> (from Meta) released with the paper <a href="https://arxiv.org/abs/2207.04672" rel="nofollow">No Language Left Behind: Scaling Human-Centered Machine Translation</a> by the NLLB team.</li> <li><strong><a href="model_doc/nystromformer">Nyströmformer</a></strong> (from the University of Wisconsin - Madison) released with the paper <a href="https://arxiv.org/abs/2102.03902" rel="nofollow">Nyströmformer: A Nyström-Based Algorithm for Approximating Self-Attention</a> by Yunyang Xiong, Zhanpeng Zeng, Rudrasis Chakraborty, Mingxing Tan, Glenn Fung, Yin Li, Vikas Singh.</li> <li><strong><a href="model_doc/oneformer">OneFormer</a></strong> (from SHI Labs) released with the paper <a href="https://arxiv.org/abs/2211.06220" rel="nofollow">OneFormer: One Transformer to Rule Universal Image Segmentation</a> by Jitesh Jain, Jiachen Li, MangTik Chiu, Ali Hassani, Nikita Orlov, Humphrey Shi.</li> <li><strong><a href="model_doc/open-llama">OpenLlama</a></strong> (from <a href="https://huggingface.co/s-JoL" rel="nofollow">s-JoL</a>) released in <a href="https://github.com/s-JoL/Open-Llama" rel="nofollow">Open-Llama</a>.</li> <li><strong><a href="master/model_doc/opt">OPT</a></strong> (from Meta AI) released with the paper <a href="https://arxiv.org/abs/2205.01068" rel="nofollow">OPT: Open Pre-trained Transformer Language Models</a> by Susan Zhang, Stephen Roller, Naman Goyal, Mikel Artetxe, Moya Chen, Shuohui Chen et al.</li> <li><strong><a href="model_doc/owlvit">OWL-ViT</a></strong> (from Google AI) released with the paper <a href="https://arxiv.org/abs/2205.06230" rel="nofollow">Simple Open-Vocabulary Object Detection with Vision Transformers</a> by Matthias Minderer, Alexey Gritsenko, Austin Stone, Maxim Neumann, Dirk Weissenborn, Alexey Dosovitskiy, Aravindh Mahendran, Anurag Arnab, Mostafa Dehghani, Zhuoran Shen, Xiao Wang, Xiaohua Zhai, Thomas Kipf, and Neil Houlsby.</li> <li><strong><a href="model_doc/pegasus">Pegasus</a></strong> (from Google) released with the paper <a href="https://arxiv.org/abs/1912.08777" rel="nofollow">PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization</a> by Jingqing Zhang, Yao Zhao, Mohammad Saleh and Peter J. Liu.</li> <li><strong><a href="model_doc/pegasus_x">PEGASUS-X</a></strong> (from Google) released with the paper <a href="https://arxiv.org/abs/2208.04347" rel="nofollow">Investigating Efficiently Extending Transformers for Long Input Summarization</a> by Jason Phang, Yao Zhao, and Peter J. Liu.</li> <li><strong><a href="model_doc/perceiver">Perceiver IO</a></strong> (from Deepmind) released with the paper <a href="https://arxiv.org/abs/2107.14795" rel="nofollow">Perceiver IO: A General Architecture for Structured Inputs &amp; Outputs</a> by Andrew Jaegle, Sebastian Borgeaud, Jean-Baptiste Alayrac, Carl Doersch, Catalin Ionescu, David Ding, Skanda Koppula, Daniel Zoran, Andrew Brock, Evan Shelhamer, Olivier Hénaff, Matthew M. Botvinick, Andrew Zisserman, Oriol Vinyals, João Carreira.</li> <li><strong><a href="model_doc/phobert">PhoBERT</a></strong> (from VinAI Research) released with the paper <a href="https://www.aclweb.org/anthology/2020.findings-emnlp.92/" rel="nofollow">PhoBERT: Pre-trained language models for Vietnamese</a> by Dat Quoc Nguyen and Anh Tuan Nguyen.</li> <li><strong><a href="model_doc/pix2struct">Pix2Struct</a></strong> (from Google) released with the paper <a href="https://arxiv.org/abs/2210.03347" rel="nofollow">Pix2Struct: Screenshot Parsing as Pretraining for Visual Language Understanding</a> by Kenton Lee, Mandar Joshi, Iulia Turc, Hexiang Hu, Fangyu Liu, Julian Eisenschlos, Urvashi Khandelwal, Peter Shaw, Ming-Wei Chang, Kristina Toutanova.</li> <li><strong><a href="model_doc/plbart">PLBart</a></strong> (from UCLA NLP) released with the paper <a href="https://arxiv.org/abs/2103.06333" rel="nofollow">Unified Pre-training for Program Understanding and Generation</a> by Wasi Uddin Ahmad, Saikat Chakraborty, Baishakhi Ray, Kai-Wei Chang.</li> <li><strong><a href="model_doc/poolformer">PoolFormer</a></strong> (from Sea AI Labs) released with the paper <a href="https://arxiv.org/abs/2111.11418" rel="nofollow">MetaFormer is Actually What You Need for Vision</a> by Yu, Weihao and Luo, Mi and Zhou, Pan and Si, Chenyang and Zhou, Yichen and Wang, Xinchao and Feng, Jiashi and Yan, Shuicheng.</li> <li><strong><a href="model_doc/prophetnet">ProphetNet</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2001.04063" rel="nofollow">ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training</a> by Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang and Ming Zhou.</li> <li><strong><a href="model_doc/qdqbert">QDQBert</a></strong> (from NVIDIA) released with the paper <a href="https://arxiv.org/abs/2004.09602" rel="nofollow">Integer Quantization for Deep Learning Inference: Principles and Empirical Evaluation</a> by Hao Wu, Patrick Judd, Xiaojie Zhang, Mikhail Isaev and Paulius Micikevicius.</li> <li><strong><a href="model_doc/rag">RAG</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2005.11401" rel="nofollow">Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks</a> by Patrick Lewis, Ethan Perez, Aleksandara Piktus, Fabio Petroni, Vladimir Karpukhin, Naman Goyal, Heinrich Küttler, Mike Lewis, Wen-tau Yih, Tim Rocktäschel, Sebastian Riedel, Douwe Kiela.</li> <li><strong><a href="model_doc/realm.html">REALM</a></strong> (from Google Research) released with the paper <a href="https://arxiv.org/abs/2002.08909" rel="nofollow">REALM: Retrieval-Augmented Language Model Pre-Training</a> by Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Ming-Wei Chang.</li> <li><strong><a href="model_doc/reformer">Reformer</a></strong> (from Google Research) released with the paper <a href="https://arxiv.org/abs/2001.04451" rel="nofollow">Reformer: The Efficient Transformer</a> by Nikita Kitaev, Łukasz Kaiser, Anselm Levskaya.</li> <li><strong><a href="model_doc/regnet">RegNet</a></strong> (from META Platforms) released with the paper <a href="https://arxiv.org/abs/2003.13678" rel="nofollow">Designing Network Design Space</a> by Ilija Radosavovic, Raj Prateek Kosaraju, Ross Girshick, Kaiming He, Piotr Dollár.</li> <li><strong><a href="model_doc/rembert">RemBERT</a></strong> (from Google Research) released with the paper <a href="https://arxiv.org/abs/2010.12821" rel="nofollow">Rethinking embedding coupling in pre-trained language models</a> by Hyung Won Chung, Thibault Févry, Henry Tsai, M. Johnson, Sebastian Ruder.</li> <li><strong><a href="model_doc/resnet">ResNet</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/1512.03385" rel="nofollow">Deep Residual Learning for Image Recognition</a> by Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun.</li> <li><strong><a href="model_doc/roberta">RoBERTa</a></strong> (from Facebook), released together with the paper <a href="https://arxiv.org/abs/1907.11692" rel="nofollow">RoBERTa: A Robustly Optimized BERT Pretraining Approach</a> by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov.</li> <li><strong><a href="model_doc/roberta-prelayernorm">RoBERTa-PreLayerNorm</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/1904.01038" rel="nofollow">fairseq: A Fast, Extensible Toolkit for Sequence Modeling</a> by Myle Ott, Sergey Edunov, Alexei Baevski, Angela Fan, Sam Gross, Nathan Ng, David Grangier, Michael Auli.</li> <li><strong><a href="model_doc/roc_bert">RoCBert</a></strong> (from WeChatAI) released with the paper <a href="https://aclanthology.org/2022.acl-long.65.pdf" rel="nofollow">RoCBert: Robust Chinese Bert with Multimodal Contrastive Pretraining</a> by HuiSu, WeiweiShi, XiaoyuShen, XiaoZhou, TuoJi, JiaruiFang, JieZhou.</li> <li><strong><a href="model_doc/roformer">RoFormer</a></strong> (from ZhuiyiTechnology), released together with the paper <a href="https://arxiv.org/abs/2104.09864" rel="nofollow">RoFormer: Enhanced Transformer with Rotary Position Embedding</a> by Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu.</li> <li><strong><a href="model_doc/rwkv">RWKV</a></strong> (from Bo Peng), released on <a href="https://github.com/BlinkDL/RWKV-LM" rel="nofollow">this repo</a> by Bo Peng.</li> <li><strong><a href="model_doc/segformer">SegFormer</a></strong> (from NVIDIA) released with the paper <a href="https://arxiv.org/abs/2105.15203" rel="nofollow">SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers</a> by Enze Xie, Wenhai Wang, Zhiding Yu, Anima Anandkumar, Jose M. Alvarez, Ping Luo.</li> <li><strong><a href="model_doc/sam">Segment Anything</a></strong> (from Meta AI) released with the paper <a href="https://arxiv.org/pdf/2304.02643v1.pdf" rel="nofollow">Segment Anything</a> by Alexander Kirillov, Eric Mintun, Nikhila Ravi, Hanzi Mao, Chloe Rolland, Laura Gustafson, Tete Xiao, Spencer Whitehead, Alex Berg, Wan-Yen Lo, Piotr Dollar, Ross Girshick.</li> <li><strong><a href="model_doc/sew">SEW</a></strong> (from ASAPP) released with the paper <a href="https://arxiv.org/abs/2109.06870" rel="nofollow">Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition</a> by Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi.</li> <li><strong><a href="model_doc/sew_d">SEW-D</a></strong> (from ASAPP) released with the paper <a href="https://arxiv.org/abs/2109.06870" rel="nofollow">Performance-Efficiency Trade-offs in Unsupervised Pre-training for Speech Recognition</a> by Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi.</li> <li><strong><a href="model_doc/speecht5">SpeechT5</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2110.07205" rel="nofollow">SpeechT5: Unified-Modal Encoder-Decoder Pre-Training for Spoken Language Processing</a> by Junyi Ao, Rui Wang, Long Zhou, Chengyi Wang, Shuo Ren, Yu Wu, Shujie Liu, Tom Ko, Qing Li, Yu Zhang, Zhihua Wei, Yao Qian, Jinyu Li, Furu Wei.</li> <li><strong><a href="model_doc/speech_to_text">SpeechToTextTransformer</a></strong> (from Facebook), released together with the paper <a href="https://arxiv.org/abs/2010.05171" rel="nofollow">fairseq S2T: Fast Speech-to-Text Modeling with fairseq</a> by Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Dmytro Okhonko, Juan Pino.</li> <li><strong><a href="model_doc/speech_to_text_2">SpeechToTextTransformer2</a></strong> (from Facebook), released together with the paper <a href="https://arxiv.org/abs/2104.06678" rel="nofollow">Large-Scale Self- and Semi-Supervised Learning for Speech Translation</a> by Changhan Wang, Anne Wu, Juan Pino, Alexei Baevski, Michael Auli, Alexis Conneau.</li> <li><strong><a href="model_doc/splinter">Splinter</a></strong> (from Tel Aviv University), released together with the paper <a href="https://arxiv.org/abs/2101.00438" rel="nofollow">Few-Shot Question Answering by Pretraining Span Selection</a> by Ori Ram, Yuval Kirstain, Jonathan Berant, Amir Globerson, Omer Levy.</li> <li><strong><a href="model_doc/squeezebert">SqueezeBERT</a></strong> (from Berkeley) released with the paper <a href="https://arxiv.org/abs/2006.11316" rel="nofollow">SqueezeBERT: What can computer vision teach NLP about efficient neural networks?</a> by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer.</li> <li><strong><a href="model_doc/swiftformer">SwiftFormer</a></strong> (from MBZUAI) released with the paper <a href="https://arxiv.org/abs/2303.15446" rel="nofollow">SwiftFormer: Efficient Additive Attention for Transformer-based Real-time Mobile Vision Applications</a> by Abdelrahman Shaker, Muhammad Maaz, Hanoona Rasheed, Salman Khan, Ming-Hsuan Yang, Fahad Shahbaz Khan.</li> <li><strong><a href="model_doc/swin">Swin Transformer</a></strong> (from Microsoft) released with the paper <a href="https://arxiv.org/abs/2103.14030" rel="nofollow">Swin Transformer: Hierarchical Vision Transformer using Shifted Windows</a> by Ze Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo.</li> <li><strong><a href="model_doc/swinv2">Swin Transformer V2</a></strong> (from Microsoft) released with the paper <a href="https://arxiv.org/abs/2111.09883" rel="nofollow">Swin Transformer V2: Scaling Up Capacity and Resolution</a> by Ze Liu, Han Hu, Yutong Lin, Zhuliang Yao, Zhenda Xie, Yixuan Wei, Jia Ning, Yue Cao, Zheng Zhang, Li Dong, Furu Wei, Baining Guo.</li> <li><strong><a href="model_doc/swin2sr">Swin2SR</a></strong> (from University of Würzburg) released with the paper <a href="https://arxiv.org/abs/2209.11345" rel="nofollow">Swin2SR: SwinV2 Transformer for Compressed Image Super-Resolution and Restoration</a> by Marcos V. Conde, Ui-Jin Choi, Maxime Burchi, Radu Timofte.</li> <li><strong><a href="model_doc/switch_transformers">SwitchTransformers</a></strong> (from Google) released with the paper <a href="https://arxiv.org/abs/2101.03961" rel="nofollow">Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity</a> by William Fedus, Barret Zoph, Noam Shazeer.</li> <li><strong><a href="model_doc/t5">T5</a></strong> (from Google AI) released with the paper <a href="https://arxiv.org/abs/1910.10683" rel="nofollow">Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer</a> by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.</li> <li><strong><a href="model_doc/t5v1.1">T5v1.1</a></strong> (from Google AI) released in the repository <a href="https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511" rel="nofollow">google-research/text-to-text-transfer-transformer</a> by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.</li> <li><strong><a href="model_doc/table-transformer">Table Transformer</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2110.00061" rel="nofollow">PubTables-1M: Towards Comprehensive Table Extraction From Unstructured Documents</a> by Brandon Smock, Rohith Pesala, Robin Abraham.</li> <li><strong><a href="model_doc/tapas">TAPAS</a></strong> (from Google AI) released with the paper <a href="https://arxiv.org/abs/2004.02349" rel="nofollow">TAPAS: Weakly Supervised Table Parsing via Pre-training</a> by Jonathan Herzig, Paweł Krzysztof Nowak, Thomas Müller, Francesco Piccinno and Julian Martin Eisenschlos.</li> <li><strong><a href="model_doc/tapex">TAPEX</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2107.07653" rel="nofollow">TAPEX: Table Pre-training via Learning a Neural SQL Executor</a> by Qian Liu, Bei Chen, Jiaqi Guo, Morteza Ziyadi, Zeqi Lin, Weizhu Chen, Jian-Guang Lou.</li> <li><strong><a href="model_doc/time_series_transformer">Time Series Transformer</a></strong> (from HuggingFace).</li> <li><strong><a href="model_doc/timesformer">TimeSformer</a></strong> (from Facebook) released with the paper <a href="https://arxiv.org/abs/2102.05095" rel="nofollow">Is Space-Time Attention All You Need for Video Understanding?</a> by Gedas Bertasius, Heng Wang, Lorenzo Torresani.</li> <li><strong><a href="model_doc/trajectory_transformers">Trajectory Transformer</a></strong> (from the University of California at Berkeley) released with the paper <a href="https://arxiv.org/abs/2106.02039" rel="nofollow">Offline Reinforcement Learning as One Big Sequence Modeling Problem</a> by Michael Janner, Qiyang Li, Sergey Levine</li> <li><strong><a href="model_doc/transfo-xl">Transformer-XL</a></strong> (from Google/CMU) released with the paper <a href="https://arxiv.org/abs/1901.02860" rel="nofollow">Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context</a> by Zihang Dai<em>, Zhilin Yang</em>, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.</li> <li><strong><a href="model_doc/trocr">TrOCR</a></strong> (from Microsoft), released together with the paper <a href="https://arxiv.org/abs/2109.10282" rel="nofollow">TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models</a> by Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei.</li> <li><strong><a href="model_doc/tvlt">TVLT</a></strong> (from UNC Chapel Hill) released with the paper <a href="https://arxiv.org/abs/2209.14156" rel="nofollow">TVLT: Textless Vision-Language Transformer</a> by Zineng Tang, Jaemin Cho, Yixin Nie, Mohit Bansal.</li> <li><strong><a href="model_doc/ul2">UL2</a></strong> (from Google Research) released with the paper <a href="https://arxiv.org/abs/2205.05131v1" rel="nofollow">Unifying Language Learning Paradigms</a> by Yi Tay, Mostafa Dehghani, Vinh Q. Tran, Xavier Garcia, Dara Bahri, Tal Schuster, Huaixiu Steven Zheng, Neil Houlsby, Donald Metzler</li> <li><strong><a href="model_doc/unispeech">UniSpeech</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2101.07597" rel="nofollow">UniSpeech: Unified Speech Representation Learning with Labeled and Unlabeled Data</a> by Chengyi Wang, Yu Wu, Yao Qian, Kenichi Kumatani, Shujie Liu, Furu Wei, Michael Zeng, Xuedong Huang.</li> <li><strong><a href="model_doc/unispeech-sat">UniSpeechSat</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2110.05752" rel="nofollow">UNISPEECH-SAT: UNIVERSAL SPEECH REPRESENTATION LEARNING WITH SPEAKER AWARE PRE-TRAINING</a> by Sanyuan Chen, Yu Wu, Chengyi Wang, Zhengyang Chen, Zhuo Chen, Shujie Liu, Jian Wu, Yao Qian, Furu Wei, Jinyu Li, Xiangzhan Yu.</li> <li><strong><a href="model_doc/upernet">UPerNet</a></strong> (from Peking University) released with the paper <a href="https://arxiv.org/abs/1807.10221" rel="nofollow">Unified Perceptual Parsing for Scene Understanding</a> by Tete Xiao, Yingcheng Liu, Bolei Zhou, Yuning Jiang, Jian Sun.</li> <li><strong><a href="model_doc/van">VAN</a></strong> (from Tsinghua University and Nankai University) released with the paper <a href="https://arxiv.org/abs/2202.09741" rel="nofollow">Visual Attention Network</a> by Meng-Hao Guo, Cheng-Ze Lu, Zheng-Ning Liu, Ming-Ming Cheng, Shi-Min Hu.</li> <li><strong><a href="model_doc/videomae">VideoMAE</a></strong> (from Multimedia Computing Group, Nanjing University) released with the paper <a href="https://arxiv.org/abs/2203.12602" rel="nofollow">VideoMAE: Masked Autoencoders are Data-Efficient Learners for Self-Supervised Video Pre-Training</a> by Zhan Tong, Yibing Song, Jue Wang, Limin Wang.</li> <li><strong><a href="model_doc/vilt">ViLT</a></strong> (from NAVER AI Lab/Kakao Enterprise/Kakao Brain) released with the paper <a href="https://arxiv.org/abs/2102.03334" rel="nofollow">ViLT: Vision-and-Language Transformer Without Convolution or Region Supervision</a> by Wonjae Kim, Bokyung Son, Ildoo Kim.</li> <li><strong><a href="model_doc/vit">Vision Transformer (ViT)</a></strong> (from Google AI) released with the paper <a href="https://arxiv.org/abs/2010.11929" rel="nofollow">An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale</a> by Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby.</li> <li><strong><a href="model_doc/visual_bert">VisualBERT</a></strong> (from UCLA NLP) released with the paper <a href="https://arxiv.org/pdf/1908.03557" rel="nofollow">VisualBERT: A Simple and Performant Baseline for Vision and Language</a> by Liunian Harold Li, Mark Yatskar, Da Yin, Cho-Jui Hsieh, Kai-Wei Chang.</li> <li><strong><a href="model_doc/vit_hybrid">ViT Hybrid</a></strong> (from Google AI) released with the paper <a href="https://arxiv.org/abs/2010.11929" rel="nofollow">An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale</a> by Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby.</li> <li><strong><a href="model_doc/vit_mae">ViTMAE</a></strong> (from Meta AI) released with the paper <a href="https://arxiv.org/abs/2111.06377" rel="nofollow">Masked Autoencoders Are Scalable Vision Learners</a> by Kaiming He, Xinlei Chen, Saining Xie, Yanghao Li, Piotr Dollár, Ross Girshick.</li> <li><strong><a href="model_doc/vit_msn">ViTMSN</a></strong> (from Meta AI) released with the paper <a href="https://arxiv.org/abs/2204.07141" rel="nofollow">Masked Siamese Networks for Label-Efficient Learning</a> by Mahmoud Assran, Mathilde Caron, Ishan Misra, Piotr Bojanowski, Florian Bordes, Pascal Vincent, Armand Joulin, Michael Rabbat, Nicolas Ballas.</li> <li><strong><a href="model_doc/wav2vec2">Wav2Vec2</a></strong> (from Facebook AI) released with the paper <a href="https://arxiv.org/abs/2006.11477" rel="nofollow">wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations</a> by Alexei Baevski, Henry Zhou, Abdelrahman Mohamed, Michael Auli.</li> <li><strong><a href="model_doc/wav2vec2-conformer">Wav2Vec2-Conformer</a></strong> (from Facebook AI) released with the paper <a href="https://arxiv.org/abs/2010.05171" rel="nofollow">FAIRSEQ S2T: Fast Speech-to-Text Modeling with FAIRSEQ</a> by Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Sravya Popuri, Dmytro Okhonko, Juan Pino.</li> <li><strong><a href="model_doc/wav2vec2_phoneme">Wav2Vec2Phoneme</a></strong> (from Facebook AI) released with the paper <a href="https://arxiv.org/abs/2109.11680" rel="nofollow">Simple and Effective Zero-shot Cross-lingual Phoneme Recognition</a> by Qiantong Xu, Alexei Baevski, Michael Auli.</li> <li><strong><a href="model_doc/wavlm">WavLM</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2110.13900" rel="nofollow">WavLM: Large-Scale Self-Supervised Pre-Training for Full Stack Speech Processing</a> by Sanyuan Chen, Chengyi Wang, Zhengyang Chen, Yu Wu, Shujie Liu, Zhuo Chen, Jinyu Li, Naoyuki Kanda, Takuya Yoshioka, Xiong Xiao, Jian Wu, Long Zhou, Shuo Ren, Yanmin Qian, Yao Qian, Jian Wu, Michael Zeng, Furu Wei.</li> <li><strong><a href="model_doc/whisper">Whisper</a></strong> (from OpenAI) released with the paper <a href="https://cdn.openai.com/papers/whisper.pdf" rel="nofollow">Robust Speech Recognition via Large-Scale Weak Supervision</a> by Alec Radford, Jong Wook Kim, Tao Xu, Greg Brockman, Christine McLeavey, Ilya Sutskever.</li> <li><strong><a href="model_doc/xclip">X-CLIP</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2208.02816" rel="nofollow">Expanding Language-Image Pretrained Models for General Video Recognition</a> by Bolin Ni, Houwen Peng, Minghao Chen, Songyang Zhang, Gaofeng Meng, Jianlong Fu, Shiming Xiang, Haibin Ling.</li> <li><strong><a href="model_doc/xmod">X-MOD</a></strong> (from Meta AI) released with the paper <a href="http://dx.doi.org/10.18653/v1/2022.naacl-main.255" rel="nofollow">Lifting the Curse of Multilinguality by Pre-training Modular Transformers</a> by Jonas Pfeiffer, Naman Goyal, Xi Lin, Xian Li, James Cross, Sebastian Riedel, Mikel Artetxe.</li> <li><strong><a href="model_doc/xglm">XGLM</a></strong> (From Facebook AI) released with the paper <a href="https://arxiv.org/abs/2112.10668" rel="nofollow">Few-shot Learning with Multilingual Language Models</a> by Xi Victoria Lin, Todor Mihaylov, Mikel Artetxe, Tianlu Wang, Shuohui Chen, Daniel Simig, Myle Ott, Naman Goyal, Shruti Bhosale, Jingfei Du, Ramakanth Pasunuru, Sam Shleifer, Punit Singh Koura, Vishrav Chaudhary, Brian O’Horo, Jeff Wang, Luke Zettlemoyer, Zornitsa Kozareva, Mona Diab, Veselin Stoyanov, Xian Li.</li> <li><strong><a href="model_doc/xlm">XLM</a></strong> (from Facebook) released together with the paper <a href="https://arxiv.org/abs/1901.07291" rel="nofollow">Cross-lingual Language Model Pretraining</a> by Guillaume Lample and Alexis Conneau.</li> <li><strong><a href="model_doc/xlm-prophetnet">XLM-ProphetNet</a></strong> (from Microsoft Research) released with the paper <a href="https://arxiv.org/abs/2001.04063" rel="nofollow">ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training</a> by Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei Zhang and Ming Zhou.</li> <li><strong><a href="model_doc/xlm-roberta">XLM-RoBERTa</a></strong> (from Facebook AI), released together with the paper <a href="https://arxiv.org/abs/1911.02116" rel="nofollow">Unsupervised Cross-lingual Representation Learning at Scale</a> by Alexis Conneau<em>, Kartikay Khandelwal</em>, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmán, Edouard Grave, Myle Ott, Luke Zettlemoyer and Veselin Stoyanov.</li> <li><strong><a href="model_doc/xlm-roberta-xl">XLM-RoBERTa-XL</a></strong> (from Facebook AI), released together with the paper <a href="https://arxiv.org/abs/2105.00572" rel="nofollow">Larger-Scale Transformers for Multilingual Masked Language Modeling</a> by Naman Goyal, Jingfei Du, Myle Ott, Giri Anantharaman, Alexis Conneau.</li> <li><strong><a href="model_doc/xlm-v">XLM-V</a></strong> (from Meta AI) released with the paper <a href="https://arxiv.org/abs/2301.10472" rel="nofollow">XLM-V: Overcoming the Vocabulary Bottleneck in Multilingual Masked Language Models</a> by Davis Liang, Hila Gonen, Yuning Mao, Rui Hou, Naman Goyal, Marjan Ghazvininejad, Luke Zettlemoyer, Madian Khabsa.</li> <li><strong><a href="model_doc/xlnet">XLNet</a></strong> (from Google/CMU) released with the paper <a href="https://arxiv.org/abs/1906.08237" rel="nofollow">​XLNet: Generalized Autoregressive Pretraining for Language Understanding</a> by Zhilin Yang<em>, Zihang Dai</em>, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le.</li> <li><strong><a href="model_doc/xls_r">XLS-R</a></strong> (from Facebook AI) released with the paper <a href="https://arxiv.org/abs/2111.09296" rel="nofollow">XLS-R: Self-supervised Cross-lingual Speech Representation Learning at Scale</a> by Arun Babu, Changhan Wang, Andros Tjandra, Kushal Lakhotia, Qiantong Xu, Naman Goyal, Kritika Singh, Patrick von Platen, Yatharth Saraf, Juan Pino, Alexei Baevski, Alexis Conneau, Michael Auli.</li> <li><strong><a href="model_doc/xlsr_wav2vec2">XLSR-Wav2Vec2</a></strong> (from Facebook AI) released with the paper <a href="https://arxiv.org/abs/2006.13979" rel="nofollow">Unsupervised Cross-Lingual Representation Learning For Speech Recognition</a> by Alexis Conneau, Alexei Baevski, Ronan Collobert, Abdelrahman Mohamed, Michael Auli.</li> <li><strong><a href="model_doc/yolos">YOLOS</a></strong> (from Huazhong University of Science &amp; Technology) released with the paper <a href="https://arxiv.org/abs/2106.00666" rel="nofollow">You Only Look at One Sequence: Rethinking Transformer in Vision through Object Detection</a> by Yuxin Fang, Bencheng Liao, Xinggang Wang, Jiemin Fang, Jiyang Qi, Rui Wu, Jianwei Niu, Wenyu Liu.</li> <li><strong><a href="model_doc/yoso">YOSO</a></strong> (from the University of Wisconsin - Madison) released with the paper <a href="https://arxiv.org/abs/2111.09714" rel="nofollow">You Only Sample (Almost) Once: Linear Cost Self-Attention Via Bernoulli Sampling</a> by Zhanpeng Zeng, Yunyang Xiong, Sathya N. Ravi, Shailesh Acharya, Glenn Fung, Vikas Singh.</li></ol> <h3 class="relative group"><a id="supported-frameworks" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#supported-frameworks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Supported frameworks</span></h3> <p>The table below represents the current support in the library for each of those models, whether they have a Python tokenizer (called “slow”). A “fast” tokenizer backed by the 🤗 Tokenizers library, whether they have support in Jax (via Flax), PyTorch, and/or TensorFlow.</p> <table><thead><tr><th align="center">Model</th> <th align="center">Tokenizer slow</th> <th align="center">Tokenizer fast</th> <th align="center">PyTorch support</th> <th align="center">TensorFlow support</th> <th align="center">Flax Support</th></tr></thead> <tbody><tr><td align="center">ALBERT</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">ALIGN</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">AltCLIP</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Audio Spectrogram Transformer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Autoformer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">BART</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">BEiT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td></tr> <tr><td align="center">BERT</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">Bert Generation</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">BigBird</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td></tr> <tr><td align="center">BigBird-Pegasus</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">BioGpt</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">BiT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Blenderbot</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">BlenderbotSmall</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">BLIP</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">BLIP-2</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">BLOOM</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">BridgeTower</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">CamemBERT</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">CANINE</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Chinese-CLIP</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">CLAP</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">CLIP</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">CLIPSeg</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">CodeGen</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Conditional DETR</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">ConvBERT</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">ConvNeXT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">ConvNeXTV2</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">CPM-Ant</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">CTRL</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">CvT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">Data2VecAudio</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Data2VecText</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Data2VecVision</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">DeBERTa</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">DeBERTa-v2</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">Decision Transformer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Deformable DETR</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">DeiT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">DETA</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">DETR</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">DiNAT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">DistilBERT</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">DonutSwin</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">DPR</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">DPT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">EfficientFormer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">EfficientNet</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">ELECTRA</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">Encoder decoder</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">ERNIE</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">ErnieM</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">ESM</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">FairSeq Machine-Translation</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">FlauBERT</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">FLAVA</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">FNet</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">FocalNet</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Funnel Transformer</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">GIT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">GLPN</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">GPT Neo</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td></tr> <tr><td align="center">GPT NeoX</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">GPT NeoX Japanese</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">GPT-J</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">GPT-Sw3</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">GPTBigCode</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">GPTSAN-japanese</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Graphormer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">GroupViT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">Hubert</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">I-BERT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">ImageGPT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Informer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Jukebox</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">LayoutLM</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">LayoutLMv2</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">LayoutLMv3</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">LED</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">LeViT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">LiLT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">LLaMA</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Longformer</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">LongT5</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td></tr> <tr><td align="center">LUKE</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">LXMERT</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">M-CTC-T</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">M2M100</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Marian</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">MarkupLM</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Mask2Former</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">MaskFormer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">MaskFormerSwin</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">mBART</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">MEGA</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Megatron-BERT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">MGP-STR</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">MobileBERT</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">MobileNetV1</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">MobileNetV2</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">MobileViT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">MobileViTV2</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">MPNet</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">MT5</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">MVP</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">NAT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Nezha</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">NLLB-MOE</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Nyströmformer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">OneFormer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">OpenAI GPT</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">OpenAI GPT-2</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">OpenLlama</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">OPT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">OWL-ViT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Pegasus</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">PEGASUS-X</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Perceiver</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Pix2Struct</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">PLBart</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">PoolFormer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">ProphetNet</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">QDQBert</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">RAG</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">REALM</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Reformer</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">RegNet</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">RemBERT</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">ResNet</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">RetriBERT</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">RoBERTa</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">RoBERTa-PreLayerNorm</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">RoCBert</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">RoFormer</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">RWKV</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">SAM</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">SegFormer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">SEW</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">SEW-D</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Speech Encoder decoder</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td></tr> <tr><td align="center">Speech2Text</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">Speech2Text2</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">SpeechT5</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Splinter</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">SqueezeBERT</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">SwiftFormer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Swin Transformer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">Swin Transformer V2</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Swin2SR</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">SwitchTransformers</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">T5</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">Table Transformer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">TAPAS</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">Time Series Transformer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">TimeSformer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">TimmBackbone</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Trajectory Transformer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Transformer-XL</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">TrOCR</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">TVLT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">UniSpeech</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">UniSpeechSat</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">UPerNet</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">VAN</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">VideoMAE</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">ViLT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Vision Encoder decoder</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">VisionTextDualEncoder</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">VisualBERT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">ViT</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">ViT Hybrid</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">ViTMAE</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">ViTMSN</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Wav2Vec2</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">Wav2Vec2-Conformer</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">WavLM</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">Whisper</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">X-CLIP</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">X-MOD</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">XGLM</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">XLM</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">XLM-ProphetNet</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">XLM-RoBERTa</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td></tr> <tr><td align="center">XLM-RoBERTa-XL</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">XLNet</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">✅</td> <td align="center">❌</td></tr> <tr><td align="center">YOLOS</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr> <tr><td align="center">YOSO</td> <td align="center">❌</td> <td align="center">❌</td> <td align="center">✅</td> <td align="center">❌</td> <td align="center">❌</td></tr></tbody></table> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"> <a href="/docs/transformers/quicktour" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Quick tour<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers&quot;,&quot;url&quot;:&quot;#transformers&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;If you are looking for custom support from the Hugging Face team&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;if-you-are-looking-for-custom-support-from-the-hugging-face-team&quot;,&quot;url&quot;:&quot;#if-you-are-looking-for-custom-support-from-the-hugging-face-team&quot;},{&quot;title&quot;:&quot;Contents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contents&quot;,&quot;url&quot;:&quot;#contents&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Supported models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;supported-models&quot;,&quot;url&quot;:&quot;#supported-models&quot;},{&quot;title&quot;:&quot;Supported frameworks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;supported-frameworks&quot;,&quot;url&quot;:&quot;#supported-frameworks&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#transformers" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-transformers">🤗 <wbr>Transformers</a> <a href="#if-you-are-looking-for-custom-support-from-the-hugging-face-team" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-if-you-are-looking-for-custom-support-from-the-hugging-face-team"><wbr>If you are looking for custom support from the <wbr>Hugging <wbr>Face team</a> <a href="#contents" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-contents"><wbr>Contents</a> <a href="#supported-models" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-supported-models"><wbr>Supported models</a> <a href="#supported-frameworks" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-supported-frameworks"><wbr>Supported frameworks</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/index" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/index"); } </script> <iframe name="__privateStripeMetricsController3490" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Findex&amp;title=%F0%9F%A4%97%20Transformers&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:03.278Z
Quick tour
https://huggingface.co/docs/transformers/quicktour
Get up and running with 🤗 Transformers! Whether you’re a developer or an everyday user, this quick tour will help you get started and show you how to use the [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) for inference, load a pretrained model and preprocessor with an [AutoClass](./model_doc/auto), and quickly train a model with PyTorch or TensorFlow. If you’re a beginner, we recommend checking out our tutorials or [course](https://huggingface.co/course/chapter1/1) next for more in-depth explanations of the concepts introduced here. Before you begin, make sure you have all the necessary libraries installed: ``` !pip install transformers datasets``` You’ll also need to install your preferred machine learning framework: ## [](#pipeline)Pipeline The [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) is the easiest and fastest way to use a pretrained model for inference. You can use the [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) out-of-the-box for many tasks across different modalities, some of which are shown in the table below: For a complete list of available tasks, check out the [pipeline API reference](./main_classes/pipelines). | **Task** | **Description** | **Modality** | **Pipeline identifier** | | --- | --- | --- | --- | | Text classification | assign a label to a given sequence of text | NLP | pipeline(task=“sentiment-analysis”) | | Text generation | generate text given a prompt | NLP | pipeline(task=“text-generation”) | | Summarization | generate a summary of a sequence of text or document | NLP | pipeline(task=“summarization”) | | Image classification | assign a label to an image | Computer vision | pipeline(task=“image-classification”) | | Image segmentation | assign a label to each individual pixel of an image (supports semantic, panoptic, and instance segmentation) | Computer vision | pipeline(task=“image-segmentation”) | | Object detection | predict the bounding boxes and classes of objects in an image | Computer vision | pipeline(task=“object-detection”) | | Audio classification | assign a label to some audio data | Audio | pipeline(task=“audio-classification”) | | Automatic speech recognition | transcribe speech into text | Audio | pipeline(task=“automatic-speech-recognition”) | | Visual question answering | answer a question about the image, given an image and a question | Multimodal | pipeline(task=“vqa”) | | Document question answering | answer a question about a document, given an image and a question | Multimodal | pipeline(task=“document-question-answering”) | | Image captioning | generate a caption for a given image | Multimodal | pipeline(task=“image-to-text”) | Start by creating an instance of [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) and specifying a task you want to use it for. In this guide, you’ll use the [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) for sentiment analysis as an example: ``` >>> from transformers import pipeline >>> classifier = pipeline("sentiment-analysis")``` The [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.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: ``` >>> classifier("We are very happy to show you the 🤗 Transformers library.") [{'label': 'POSITIVE', 'score': 0.9998}]``` If you have more than one input, pass your inputs as a list to the [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) to return a list of dictionaries: ``` >>> results = classifier(["We are very happy to show you the 🤗 Transformers library.", "We hope you don't hate it."]) >>> 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``` The [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) can also iterate over an entire dataset for any task you like. For this example, let’s choose automatic speech recognition as our task: ``` >>> import torch >>> from transformers import pipeline >>> speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")``` Load an audio dataset (see the 🤗 Datasets [Quick Start](https://huggingface.co/docs/datasets/quickstart#audio) for more details) you’d like to iterate over. For example, load the [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) dataset: ``` >>> from datasets import load_dataset, Audio >>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train")``` You need to make sure the sampling rate of the dataset matches the sampling rate [`facebook/wav2vec2-base-960h`](https://huggingface.co/facebook/wav2vec2-base-960h) was trained on: ``` >>> dataset = dataset.cast_column("audio", Audio(sampling_rate=speech_recognizer.feature_extractor.sampling_rate))``` The audio files are automatically loaded and resampled when calling the `"audio"` column. Extract the raw waveform arrays from the first 4 samples and pass it as a list to the pipeline: ``` >>> result = speech_recognizer(dataset[:4]["audio"]) >>> print([d["text"] for d in result]) ['I WOULD LIKE TO SET UP A JOINT ACCOUNT WITH MY PARTNER HOW DO I PROCEED WITH DOING THAT', "FONDERING HOW I'D SET UP A JOIN TO HELL T 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 APSO 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 AN I'M MISSING SOMETHING UQUETTE HAD PREFERRED TO JUST DO IT OVER THE PHONE OF POSSIBLE THINGS", 'HOW DO I FURN A JOINA COUT']``` For larger datasets where the inputs are big (like in speech or vision), you’ll want to pass a generator instead of a list to load all the inputs in memory. Take a look at the [pipeline API reference](./main_classes/pipelines) for more information. ### [](#use-another-model-and-tokenizer-in-the-pipeline)Use another model and tokenizer in the pipeline The [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) can accommodate any model from the [Hub](https://huggingface.co/models), making it easy to adapt the [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) for other use-cases. For example, if you’d like a model capable of handling French text, use the tags on the 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) finetuned for sentiment analysis you can use for French text: ``` >>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment"``` Use [AutoModelForSequenceClassification](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModelForSequenceClassification) and [AutoTokenizer](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer) to load the pretrained model and it’s associated tokenizer (more on an `AutoClass` in the next section): ``` >>> from transformers import AutoTokenizer, AutoModelForSequenceClassification >>> model = AutoModelForSequenceClassification.from_pretrained(model_name) >>> tokenizer = AutoTokenizer.from_pretrained(model_name)``` Use [TFAutoModelForSequenceClassification](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.TFAutoModelForSequenceClassification) and [AutoTokenizer](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer) to load the pretrained model and it’s associated tokenizer (more on an `TFAutoClass` in the next section): ``` >>> from transformers import AutoTokenizer, TFAutoModelForSequenceClassification >>> model = TFAutoModelForSequenceClassification.from_pretrained(model_name) >>> tokenizer = AutoTokenizer.from_pretrained(model_name)``` Specify the model and tokenizer in the [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline), and now you can apply the `classifier` on French text: ``` >>> classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) >>> classifier("Nous sommes très heureux de vous présenter la bibliothèque 🤗 Transformers.") [{'label': '5 stars', 'score': 0.7273}]``` If you can’t find a model for your use-case, you’ll need to finetune a pretrained model on your data. Take a look at our [finetuning tutorial](./training) to learn how. Finally, after you’ve finetuned your pretrained model, please consider [sharing](./model_sharing) the model with the community on the Hub to democratize machine learning for everyone! 🤗 ## [](#autoclass)AutoClass Under the hood, the [AutoModelForSequenceClassification](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModelForSequenceClassification) and [AutoTokenizer](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer) classes work together to power the [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) you used above. An [AutoClass](./model_doc/auto) is a shortcut that automatically retrieves the architecture of a pretrained model from its name or path. You only need to select the appropriate `AutoClass` for your task and it’s associated preprocessing class. Let’s return to the example from the previous section and see how you can use the `AutoClass` to replicate the results of the [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline). ### [](#autotokenizer)AutoTokenizer A tokenizer is responsible for preprocessing text into an array of numbers as inputs to a model. There are multiple rules that govern the tokenization process, including how to split a word and at what level words should be split (learn more about tokenization in the [tokenizer summary](./tokenizer_summary)). The most important thing to remember is you need to instantiate a tokenizer with the same model name to ensure you’re using the same tokenization rules a model was pretrained with. Load a tokenizer with [AutoTokenizer](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer): ``` >>> from transformers import AutoTokenizer >>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" >>> tokenizer = AutoTokenizer.from_pretrained(model_name)``` Pass your text to the tokenizer: ``` >>> encoding = tokenizer("We are very happy to show you the 🤗 Transformers library.") >>> print(encoding) {'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]}``` The tokenizer returns a dictionary containing: - [input\_ids](./glossary#input-ids): numerical representations of your tokens. - [attention\_mask](.glossary#attention-mask): indicates which tokens should be attended to. A tokenizer can also accept a list of inputs, and pad and truncate the text to return a batch with uniform length: ``` >>> 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, ... return_tensors="pt", ... )``` ``` >>> 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, ... return_tensors="tf", ... )``` Check out the [preprocess](./preprocessing) tutorial for more details about tokenization, and how to use an [AutoImageProcessor](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoImageProcessor), [AutoFeatureExtractor](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoFeatureExtractor) and [AutoProcessor](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoProcessor) to preprocess image, audio, and multimodal inputs. ### [](#automodel)AutoModel 🤗 Transformers provides a simple and unified way to load pretrained instances. This means you can load an [AutoModel](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModel) like you would load an [AutoTokenizer](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer). The only difference is selecting the correct [AutoModel](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModel) for the task. For text (or sequence) classification, you should load [AutoModelForSequenceClassification](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModelForSequenceClassification): ``` >>> from transformers import AutoModelForSequenceClassification >>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" >>> pt_model = AutoModelForSequenceClassification.from_pretrained(model_name)``` See the [task summary](./task_summary) for tasks supported by an [AutoModel](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModel) class. Now pass your preprocessed batch of inputs directly to the model. You just have to unpack the dictionary by adding `**`: ``` >>> pt_outputs = pt_model(**pt_batch)``` The model outputs the final activations in the `logits` attribute. Apply the softmax function to the `logits` to retrieve the probabilities: ``` >>> from torch import nn >>> pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-1) >>> print(pt_predictions) tensor([[0.0021, 0.0018, 0.0115, 0.2121, 0.7725], [0.2084, 0.1826, 0.1969, 0.1755, 0.2365]], grad_fn=<SoftmaxBackward0>)``` 🤗 Transformers provides a simple and unified way to load pretrained instances. This means you can load an [TFAutoModel](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.TFAutoModel) like you would load an [AutoTokenizer](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer). The only difference is selecting the correct [TFAutoModel](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.TFAutoModel) for the task. For text (or sequence) classification, you should load [TFAutoModelForSequenceClassification](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.TFAutoModelForSequenceClassification): ``` >>> from transformers import TFAutoModelForSequenceClassification >>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment" >>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(model_name)``` See the [task summary](./task_summary) for tasks supported by an [AutoModel](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModel) class. Now pass your preprocessed batch of inputs directly to the model by passing the dictionary keys directly to the tensors: ``` >>> 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: ``` >>> import tensorflow as tf >>> tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-1) >>> tf_predictions``` All 🤗 Transformers models (PyTorch or TensorFlow) output the tensors _before_ the final activation function (like softmax) because the final activation function is often fused with the loss. Model outputs are special dataclasses so their attributes are autocompleted in an IDE. The model outputs behave like a tuple or a dictionary (you can index with an integer, a slice or a string) in which case, attributes that are None are ignored. ### [](#save-a-model)Save a model Once your model is fine-tuned, you can save it with its tokenizer using [PreTrainedModel.save\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.save_pretrained): ``` >>> pt_save_directory = "./pt_save_pretrained" >>> tokenizer.save_pretrained(pt_save_directory) >>> pt_model.save_pretrained(pt_save_directory)``` When you are ready to use the model again, reload it with [PreTrainedModel.from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained): ``` >>> pt_model = AutoModelForSequenceClassification.from_pretrained("./pt_save_pretrained")``` Once your model is fine-tuned, you can save it with its tokenizer using [TFPreTrainedModel.save\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.save_pretrained): ``` >>> tf_save_directory = "./tf_save_pretrained" >>> tokenizer.save_pretrained(tf_save_directory) >>> tf_model.save_pretrained(tf_save_directory)``` When you are ready to use the model again, reload it with [TFPreTrainedModel.from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.from_pretrained): ``` >>> tf_model = TFAutoModelForSequenceClassification.from_pretrained("./tf_save_pretrained")``` 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: ``` >>> from transformers import AutoModel >>> tokenizer = AutoTokenizer.from_pretrained(tf_save_directory) >>> pt_model = AutoModelForSequenceClassification.from_pretrained(tf_save_directory, from_tf=True)``` ``` >>> from transformers import TFAutoModel >>> tokenizer = AutoTokenizer.from_pretrained(pt_save_directory) >>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(pt_save_directory, from_pt=True)``` ## [](#custom-model-builds)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](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoConfig), and then load the pretrained model you want to modify. Within [AutoConfig.from\_pretrained()](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoConfig.from_pretrained), you can specify the attribute you want to change, such as the number of attention heads: ``` >>> from transformers import AutoConfig >>> my_config = AutoConfig.from_pretrained("distilbert-base-uncased", n_heads=12)``` Create a model from your custom configuration with [AutoModel.from\_config()](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.FlaxAutoModelForVision2Seq.from_config): ``` >>> from transformers import AutoModel >>> my_model = AutoModel.from_config(my_config)``` Create a model from your custom configuration with [TFAutoModel.from\_config()](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.FlaxAutoModelForVision2Seq.from_config): ``` >>> from transformers import TFAutoModel >>> my_model = TFAutoModel.from_config(my_config)``` Take a look at the [Create a custom architecture](./create_a_model) guide for more information about building custom configurations. ## [](#trainer-a-pytorch-optimized-training-loop)Trainer - a PyTorch optimized training loop All models are a standard [`torch.nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) so you can use them in any typical training loop. While you can write your own training loop, 🤗 Transformers provides a [Trainer](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer) class for PyTorch, which contains the basic training loop and adds additional functionality for features like distributed training, mixed precision, and more. Depending on your task, you’ll typically pass the following parameters to [Trainer](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer): 1. A [PreTrainedModel](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel) or a [`torch.nn.Module`](https://pytorch.org/docs/stable/nn.html#torch.nn.Module): ``` >>> from transformers import AutoModelForSequenceClassification >>> model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased")``` 2. [TrainingArguments](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.TrainingArguments) contains the model hyperparameters you can change like learning rate, batch size, and the number of epochs to train for. The default values are used if you don’t specify any training arguments: ``` >>> from transformers import TrainingArguments >>> training_args = TrainingArguments( ... output_dir="path/to/save/folder/", ... learning_rate=2e-5, ... per_device_train_batch_size=8, ... per_device_eval_batch_size=8, ... num_train_epochs=2, ... )``` 3. A preprocessing class like a tokenizer, image processor, feature extractor, or processor: ``` >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")``` 4. Load a dataset: ``` >>> from datasets import load_dataset >>> dataset = load_dataset("rotten_tomatoes") ``` 5. Create a function to tokenize the dataset: ``` >>> def tokenize_dataset(dataset): ... return tokenizer(dataset["text"])``` Then apply it over the entire dataset with [map](https://huggingface.co/docs/datasets/v2.12.0/en/package_reference/main_classes#datasets.Dataset.map): ``` >>> dataset = dataset.map(tokenize_dataset, batched=True)``` 6. A [DataCollatorWithPadding](/docs/transformers/v4.30.0/en/main_classes/data_collator#transformers.DataCollatorWithPadding) to create a batch of examples from your dataset: ``` >>> from transformers import DataCollatorWithPadding >>> data_collator = DataCollatorWithPadding(tokenizer=tokenizer)``` Now gather all these classes in [Trainer](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer): ``` >>> from transformers import Trainer >>> trainer = Trainer( ... model=model, ... args=training_args, ... train_dataset=dataset["train"], ... eval_dataset=dataset["test"], ... tokenizer=tokenizer, ... data_collator=data_collator, ... ) ``` When you’re ready, call [train()](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer.train) to start training: For tasks - like translation or summarization - that use a sequence-to-sequence model, use the [Seq2SeqTrainer](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Seq2SeqTrainer) and [Seq2SeqTrainingArguments](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Seq2SeqTrainingArguments) classes instead. You can customize the training loop behavior by subclassing the methods inside [Trainer](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer). This allows you to customize features such as the loss function, optimizer, and scheduler. Take a look at the [Trainer](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer) reference for which methods can be subclassed. The other way to customize the training loop is by using [Callbacks](./main_classes/callbacks). You can use callbacks to integrate with other libraries and inspect the training loop to report on progress or stop the training early. Callbacks do not modify anything in the training loop itself. To customize something like the loss function, you need to subclass the [Trainer](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer) instead. ## [](#train-with-tensorflow)Train with TensorFlow All models are a standard [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) so they can be trained in TensorFlow with the [Keras](https://keras.io/) API. 🤗 Transformers provides the [prepare\_tf\_dataset()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset) method to easily load your dataset as a `tf.data.Dataset` so you can start training right away with Keras’ [`compile`](https://keras.io/api/models/model_training_apis/#compile-method) and [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) methods. 1. You’ll start with a [TFPreTrainedModel](/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel) or a [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model): ``` >>> from transformers import TFAutoModelForSequenceClassification >>> model = TFAutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased")``` 2. A preprocessing class like a tokenizer, image processor, feature extractor, or processor: ``` >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")``` 3. Create a function to tokenize the dataset: ``` >>> def tokenize_dataset(dataset): ... return tokenizer(dataset["text"]) ``` 4. Apply the tokenizer over the entire dataset with [map](https://huggingface.co/docs/datasets/v2.12.0/en/package_reference/main_classes#datasets.Dataset.map) and then pass the dataset and tokenizer to [prepare\_tf\_dataset()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset). You can also change the batch size and shuffle the dataset here if you’d like: ``` >>> dataset = dataset.map(tokenize_dataset) >>> tf_dataset = model.prepare_tf_dataset( ... dataset["train"], batch_size=16, shuffle=True, tokenizer=tokenizer ... ) ``` 5. When you’re ready, you can call `compile` and `fit` to start training. Note that Transformers models all have a default task-relevant loss function, so you don’t need to specify one unless you want to: ``` >>> from tensorflow.keras.optimizers import Adam >>> model.compile(optimizer=Adam(3e-5)) >>> model.fit(tf_dataset) ``` ## [](#whats-next)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!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Quick tour"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/quicktour"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Quick tour</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;quick-tour&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;pipeline&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;use-another-model-and-tokenizer-in-the-pipeline&quot;,&quot;title&quot;:&quot;Use another model and tokenizer in the pipeline&quot;}],&quot;title&quot;:&quot;Pipeline&quot;},{&quot;local&quot;:&quot;autoclass&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;autotokenizer&quot;,&quot;title&quot;:&quot;AutoTokenizer&quot;},{&quot;local&quot;:&quot;automodel&quot;,&quot;title&quot;:&quot;AutoModel&quot;},{&quot;local&quot;:&quot;save-a-model&quot;,&quot;title&quot;:&quot;Save a model&quot;}],&quot;title&quot;:&quot;AutoClass&quot;},{&quot;local&quot;:&quot;custom-model-builds&quot;,&quot;title&quot;:&quot;Custom model builds&quot;},{&quot;local&quot;:&quot;trainer-a-pytorch-optimized-training-loop&quot;,&quot;title&quot;:&quot;Trainer - a PyTorch optimized training loop&quot;},{&quot;local&quot;:&quot;train-with-tensorflow&quot;,&quot;title&quot;:&quot;Train with TensorFlow&quot;},{&quot;local&quot;:&quot;whats-next&quot;,&quot;title&quot;:&quot;What's next?&quot;}],&quot;title&quot;:&quot;Quick tour&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;quicktour&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Quick tour&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Quick tour</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="quick-tour" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#quick-tour"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Quick tour</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"> <div class="relative colab-dropdown "><button class=" " type="button"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></button> </div> <div class="relative colab-dropdown "><button class=" " type="button"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></button> </div></div> <p>Get up and running with 🤗 Transformers! Whether you’re a developer or an everyday user, this quick tour will help you get started and show you how to use the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> for inference, load a pretrained model and preprocessor with an <a href="./model_doc/auto">AutoClass</a>, and quickly train a model with PyTorch or TensorFlow. If you’re a beginner, we recommend checking out our tutorials or <a href="https://huggingface.co/course/chapter1/1" rel="nofollow">course</a> next for more in-depth explanations of the concepts introduced here.</p> <p>Before you begin, make sure you have all the necessary libraries installed:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>!pip install transformers datasets</pre></div> <p>You’ll also need to install your preferred machine learning framework:</p> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install torch</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install tensorflow</pre></div></div></div> </div> <h2 class="relative group"><a id="pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Pipeline</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/tiZFewofSLM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> is the easiest and fastest way to use a pretrained model for inference. You can use the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> out-of-the-box for many tasks across different modalities, some of which are shown in the table below:</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>For a complete list of available tasks, check out the <a href="./main_classes/pipelines">pipeline API reference</a>.</p></div> <table><thead><tr><th><strong>Task</strong></th> <th><strong>Description</strong></th> <th><strong>Modality</strong></th> <th><strong>Pipeline identifier</strong></th></tr></thead> <tbody><tr><td>Text classification</td> <td>assign a label to a given sequence of text</td> <td>NLP</td> <td>pipeline(task=“sentiment-analysis”)</td></tr> <tr><td>Text generation</td> <td>generate text given a prompt</td> <td>NLP</td> <td>pipeline(task=“text-generation”)</td></tr> <tr><td>Summarization</td> <td>generate a summary of a sequence of text or document</td> <td>NLP</td> <td>pipeline(task=“summarization”)</td></tr> <tr><td>Image classification</td> <td>assign a label to an image</td> <td>Computer vision</td> <td>pipeline(task=“image-classification”)</td></tr> <tr><td>Image segmentation</td> <td>assign a label to each individual pixel of an image (supports semantic, panoptic, and instance segmentation)</td> <td>Computer vision</td> <td>pipeline(task=“image-segmentation”)</td></tr> <tr><td>Object detection</td> <td>predict the bounding boxes and classes of objects in an image</td> <td>Computer vision</td> <td>pipeline(task=“object-detection”)</td></tr> <tr><td>Audio classification</td> <td>assign a label to some audio data</td> <td>Audio</td> <td>pipeline(task=“audio-classification”)</td></tr> <tr><td>Automatic speech recognition</td> <td>transcribe speech into text</td> <td>Audio</td> <td>pipeline(task=“automatic-speech-recognition”)</td></tr> <tr><td>Visual question answering</td> <td>answer a question about the image, given an image and a question</td> <td>Multimodal</td> <td>pipeline(task=“vqa”)</td></tr> <tr><td>Document question answering</td> <td>answer a question about a document, given an image and a question</td> <td>Multimodal</td> <td>pipeline(task=“document-question-answering”)</td></tr> <tr><td>Image captioning</td> <td>generate a caption for a given image</td> <td>Multimodal</td> <td>pipeline(task=“image-to-text”)</td></tr></tbody></table> <p>Start by creating an instance of <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> and specifying a task you want to use it for. In this guide, you’ll use the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> for sentiment analysis as an example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline <span class="hljs-meta">&gt;&gt;&gt; </span>classifier = pipeline(<span class="hljs-string">"sentiment-analysis"</span>)</pre></div> <p>The <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> downloads and caches a default <a href="https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english" rel="nofollow">pretrained model</a> and tokenizer for sentiment analysis. Now you can use the <code>classifier</code> on your target text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>classifier(<span class="hljs-string">"We are very happy to show you the 🤗 Transformers library."</span>) [{<span class="hljs-string">'label'</span>: <span class="hljs-string">'POSITIVE'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.9998</span>}]</pre></div> <p>If you have more than one input, pass your inputs as a list to the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> to return a list of dictionaries:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>results = classifier([<span class="hljs-string">"We are very happy to show you the 🤗 Transformers library."</span>, <span class="hljs-string">"We hope you don't hate it."</span>]) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> result <span class="hljs-keyword">in</span> results: <span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">f"label: <span class="hljs-subst">{result[<span class="hljs-string">'label'</span>]}</span>, with score: <span class="hljs-subst">{<span class="hljs-built_in">round</span>(result[<span class="hljs-string">'score'</span>], <span class="hljs-number">4</span>)}</span>"</span>) label: POSITIVE, <span class="hljs-keyword">with</span> score: <span class="hljs-number">0.9998</span> label: NEGATIVE, <span class="hljs-keyword">with</span> score: <span class="hljs-number">0.5309</span></pre></div> <p>The <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> can also iterate over an entire dataset for any task you like. For this example, let’s choose automatic speech recognition as our task:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> torch <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline <span class="hljs-meta">&gt;&gt;&gt; </span>speech_recognizer = pipeline(<span class="hljs-string">"automatic-speech-recognition"</span>, model=<span class="hljs-string">"facebook/wav2vec2-base-960h"</span>)</pre></div> <p>Load an audio dataset (see the 🤗 Datasets <a href="https://huggingface.co/docs/datasets/quickstart#audio" rel="nofollow">Quick Start</a> for more details) you’d like to iterate over. For example, load the <a href="https://huggingface.co/datasets/PolyAI/minds14" rel="nofollow">MInDS-14</a> dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset, Audio <span class="hljs-meta">&gt;&gt;&gt; </span>dataset = load_dataset(<span class="hljs-string">"PolyAI/minds14"</span>, name=<span class="hljs-string">"en-US"</span>, split=<span class="hljs-string">"train"</span>)</pre></div> <p>You need to make sure the sampling rate of the dataset matches the sampling rate <a href="https://huggingface.co/facebook/wav2vec2-base-960h" rel="nofollow"><code>facebook/wav2vec2-base-960h</code></a> was trained on:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>dataset = dataset.cast_column(<span class="hljs-string">"audio"</span>, Audio(sampling_rate=speech_recognizer.feature_extractor.sampling_rate))</pre></div> <p>The audio files are automatically loaded and resampled when calling the <code>"audio"</code> column. Extract the raw waveform arrays from the first 4 samples and pass it as a list to the pipeline:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>result = speech_recognizer(dataset[:<span class="hljs-number">4</span>][<span class="hljs-string">"audio"</span>]) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>([d[<span class="hljs-string">"text"</span>] <span class="hljs-keyword">for</span> d <span class="hljs-keyword">in</span> result]) [<span class="hljs-string">'I WOULD LIKE TO SET UP A JOINT ACCOUNT WITH MY PARTNER HOW DO I PROCEED WITH DOING THAT'</span>, <span class="hljs-string">"FONDERING HOW I'D SET UP A JOIN TO HELL T WITH MY WIFE AND WHERE THE AP MIGHT BE"</span>, <span class="hljs-string">"I I'D LIKE TOY SET UP A JOINT ACCOUNT WITH MY PARTNER I'M NOT SEEING THE OPTION TO DO IT ON THE APSO 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 AN I'M MISSING SOMETHING UQUETTE HAD PREFERRED TO JUST DO IT OVER THE PHONE OF POSSIBLE THINGS"</span>, <span class="hljs-string">'HOW DO I FURN A JOINA COUT'</span>]</pre></div> <p>For larger datasets where the inputs are big (like in speech or vision), you’ll want to pass a generator instead of a list to load all the inputs in memory. Take a look at the <a href="./main_classes/pipelines">pipeline API reference</a> for more information.</p> <h3 class="relative group"><a id="use-another-model-and-tokenizer-in-the-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#use-another-model-and-tokenizer-in-the-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Use another model and tokenizer in the pipeline</span></h3> <p>The <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> can accommodate any model from the <a href="https://huggingface.co/models" rel="nofollow">Hub</a>, making it easy to adapt the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> for other use-cases. For example, if you’d like a model capable of handling French text, use the tags on the Hub to filter for an appropriate model. The top filtered result returns a multilingual <a href="https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment" rel="nofollow">BERT model</a> finetuned for sentiment analysis you can use for French text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>model_name = <span class="hljs-string">"nlptown/bert-base-multilingual-uncased-sentiment"</span></pre></div> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><p>Use <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModelForSequenceClassification">AutoModelForSequenceClassification</a> and <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer">AutoTokenizer</a> to load the pretrained model and it’s associated tokenizer (more on an <code>AutoClass</code> in the next section):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSequenceClassification.from_pretrained(model_name) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(model_name)</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><p>Use <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.TFAutoModelForSequenceClassification">TFAutoModelForSequenceClassification</a> and <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer">AutoTokenizer</a> to load the pretrained model and it’s associated tokenizer (more on an <code>TFAutoClass</code> in the next section):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, TFAutoModelForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model = TFAutoModelForSequenceClassification.from_pretrained(model_name) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(model_name)</pre></div></div></div> </div> <p>Specify the model and tokenizer in the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a>, and now you can apply the <code>classifier</code> on French text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>classifier = pipeline(<span class="hljs-string">"sentiment-analysis"</span>, model=model, tokenizer=tokenizer) <span class="hljs-meta">&gt;&gt;&gt; </span>classifier(<span class="hljs-string">"Nous sommes très heureux de vous présenter la bibliothèque 🤗 Transformers."</span>) [{<span class="hljs-string">'label'</span>: <span class="hljs-string">'5 stars'</span>, <span class="hljs-string">'score'</span>: <span class="hljs-number">0.7273</span>}]</pre></div> <p>If you can’t find a model for your use-case, you’ll need to finetune a pretrained model on your data. Take a look at our <a href="./training">finetuning tutorial</a> to learn how. Finally, after you’ve finetuned your pretrained model, please consider <a href="./model_sharing">sharing</a> the model with the community on the Hub to democratize machine learning for everyone! 🤗</p> <h2 class="relative group"><a id="autoclass" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#autoclass"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>AutoClass</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/AhChOFRegn4" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Under the hood, the <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModelForSequenceClassification">AutoModelForSequenceClassification</a> and <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer">AutoTokenizer</a> classes work together to power the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> you used above. An <a href="./model_doc/auto">AutoClass</a> is a shortcut that automatically retrieves the architecture of a pretrained model from its name or path. You only need to select the appropriate <code>AutoClass</code> for your task and it’s associated preprocessing class.</p> <p>Let’s return to the example from the previous section and see how you can use the <code>AutoClass</code> to replicate the results of the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a>.</p> <h3 class="relative group"><a id="autotokenizer" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#autotokenizer"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>AutoTokenizer</span></h3> <p>A tokenizer is responsible for preprocessing text into an array of numbers as inputs to a model. There are multiple rules that govern the tokenization process, including how to split a word and at what level words should be split (learn more about tokenization in the <a href="./tokenizer_summary">tokenizer summary</a>). The most important thing to remember is you need to instantiate a tokenizer with the same model name to ensure you’re using the same tokenization rules a model was pretrained with.</p> <p>Load a tokenizer with <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer">AutoTokenizer</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>model_name = <span class="hljs-string">"nlptown/bert-base-multilingual-uncased-sentiment"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(model_name)</pre></div> <p>Pass your text to the tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>encoding = tokenizer(<span class="hljs-string">"We are very happy to show you the 🤗 Transformers library."</span>) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(encoding) {<span class="hljs-string">'input_ids'</span>: [<span class="hljs-number">101</span>, <span class="hljs-number">11312</span>, <span class="hljs-number">10320</span>, <span class="hljs-number">12495</span>, <span class="hljs-number">19308</span>, <span class="hljs-number">10114</span>, <span class="hljs-number">11391</span>, <span class="hljs-number">10855</span>, <span class="hljs-number">10103</span>, <span class="hljs-number">100</span>, <span class="hljs-number">58263</span>, <span class="hljs-number">13299</span>, <span class="hljs-number">119</span>, <span class="hljs-number">102</span>], <span class="hljs-string">'token_type_ids'</span>: [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], <span class="hljs-string">'attention_mask'</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]}</pre></div> <p>The tokenizer returns a dictionary containing:</p> <ul><li><a href="./glossary#input-ids">input_ids</a>: numerical representations of your tokens.</li> <li><a href=".glossary#attention-mask">attention_mask</a>: indicates which tokens should be attended to.</li></ul> <p>A tokenizer can also accept a list of inputs, and pad and truncate the text to return a batch with uniform length:</p> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>pt_batch = tokenizer( <span class="hljs-meta">... </span> [<span class="hljs-string">"We are very happy to show you the 🤗 Transformers library."</span>, <span class="hljs-string">"We hope you don't hate it."</span>], <span class="hljs-meta">... </span> padding=<span class="hljs-literal">True</span>, <span class="hljs-meta">... </span> truncation=<span class="hljs-literal">True</span>, <span class="hljs-meta">... </span> max_length=<span class="hljs-number">512</span>, <span class="hljs-meta">... </span> return_tensors=<span class="hljs-string">"pt"</span>, <span class="hljs-meta">... </span>)</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tf_batch = tokenizer( <span class="hljs-meta">... </span> [<span class="hljs-string">"We are very happy to show you the 🤗 Transformers library."</span>, <span class="hljs-string">"We hope you don't hate it."</span>], <span class="hljs-meta">... </span> padding=<span class="hljs-literal">True</span>, <span class="hljs-meta">... </span> truncation=<span class="hljs-literal">True</span>, <span class="hljs-meta">... </span> max_length=<span class="hljs-number">512</span>, <span class="hljs-meta">... </span> return_tensors=<span class="hljs-string">"tf"</span>, <span class="hljs-meta">... </span>)</pre></div></div></div> </div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>Check out the <a href="./preprocessing">preprocess</a> tutorial for more details about tokenization, and how to use an <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoImageProcessor">AutoImageProcessor</a>, <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoFeatureExtractor">AutoFeatureExtractor</a> and <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoProcessor">AutoProcessor</a> to preprocess image, audio, and multimodal inputs.</p></div> <h3 class="relative group"><a id="automodel" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#automodel"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>AutoModel</span></h3> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><p>🤗 Transformers provides a simple and unified way to load pretrained instances. This means you can load an <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModel">AutoModel</a> like you would load an <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer">AutoTokenizer</a>. The only difference is selecting the correct <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModel">AutoModel</a> for the task. For text (or sequence) classification, you should load <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModelForSequenceClassification">AutoModelForSequenceClassification</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model_name = <span class="hljs-string">"nlptown/bert-base-multilingual-uncased-sentiment"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>pt_model = AutoModelForSequenceClassification.from_pretrained(model_name)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>See the <a href="./task_summary">task summary</a> for tasks supported by an <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModel">AutoModel</a> class.</p></div> <p>Now pass your preprocessed batch of inputs directly to the model. You just have to unpack the dictionary by adding <code>**</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>pt_outputs = pt_model(**pt_batch)</pre></div> <p>The model outputs the final activations in the <code>logits</code> attribute. Apply the softmax function to the <code>logits</code> to retrieve the probabilities:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> torch <span class="hljs-keyword">import</span> nn <span class="hljs-meta">&gt;&gt;&gt; </span>pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-<span class="hljs-number">1</span>) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(pt_predictions) tensor([[<span class="hljs-number">0.0021</span>, <span class="hljs-number">0.0018</span>, <span class="hljs-number">0.0115</span>, <span class="hljs-number">0.2121</span>, <span class="hljs-number">0.7725</span>], [<span class="hljs-number">0.2084</span>, <span class="hljs-number">0.1826</span>, <span class="hljs-number">0.1969</span>, <span class="hljs-number">0.1755</span>, <span class="hljs-number">0.2365</span>]], grad_fn=&lt;SoftmaxBackward0&gt;)</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><p>🤗 Transformers provides a simple and unified way to load pretrained instances. This means you can load an <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.TFAutoModel">TFAutoModel</a> like you would load an <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer">AutoTokenizer</a>. The only difference is selecting the correct <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.TFAutoModel">TFAutoModel</a> for the task. For text (or sequence) classification, you should load <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.TFAutoModelForSequenceClassification">TFAutoModelForSequenceClassification</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model_name = <span class="hljs-string">"nlptown/bert-base-multilingual-uncased-sentiment"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tf_model = TFAutoModelForSequenceClassification.from_pretrained(model_name)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>See the <a href="./task_summary">task summary</a> for tasks supported by an <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoModel">AutoModel</a> class.</p></div> <p>Now pass your preprocessed batch of inputs directly to the model by passing the dictionary keys directly to the tensors:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tf_outputs = tf_model(tf_batch)</pre></div> <p>The model outputs the final activations in the <code>logits</code> attribute. Apply the softmax function to the <code>logits</code> to retrieve the probabilities:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf <span class="hljs-meta">&gt;&gt;&gt; </span>tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-<span class="hljs-number">1</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>tf_predictions</pre></div></div></div> </div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>All 🤗 Transformers models (PyTorch or TensorFlow) output the tensors <em>before</em> the final activation function (like softmax) because the final activation function is often fused with the loss. Model outputs are special dataclasses so their attributes are autocompleted in an IDE. The model outputs behave like a tuple or a dictionary (you can index with an integer, a slice or a string) in which case, attributes that are None are ignored.</p></div> <h3 class="relative group"><a id="save-a-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#save-a-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Save a model</span></h3> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><p>Once your model is fine-tuned, you can save it with its tokenizer using <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.save_pretrained">PreTrainedModel.save_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>pt_save_directory = <span class="hljs-string">"./pt_save_pretrained"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.save_pretrained(pt_save_directory) <span class="hljs-meta">&gt;&gt;&gt; </span>pt_model.save_pretrained(pt_save_directory)</pre></div> <p>When you are ready to use the model again, reload it with <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained">PreTrainedModel.from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>pt_model = AutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">"./pt_save_pretrained"</span>)</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><p>Once your model is fine-tuned, you can save it with its tokenizer using <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.save_pretrained">TFPreTrainedModel.save_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tf_save_directory = <span class="hljs-string">"./tf_save_pretrained"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.save_pretrained(tf_save_directory) <span class="hljs-meta">&gt;&gt;&gt; </span>tf_model.save_pretrained(tf_save_directory)</pre></div> <p>When you are ready to use the model again, reload it with <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.from_pretrained">TFPreTrainedModel.from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tf_model = TFAutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">"./tf_save_pretrained"</span>)</pre></div></div></div> </div> <p>One particularly cool 🤗 Transformers feature is the ability to save a model and reload it as either a PyTorch or TensorFlow model. The <code>from_pt</code> or <code>from_tf</code> parameter can convert the model from one framework to the other:</p> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModel <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(tf_save_directory) <span class="hljs-meta">&gt;&gt;&gt; </span>pt_model = AutoModelForSequenceClassification.from_pretrained(tf_save_directory, from_tf=<span class="hljs-literal">True</span>)</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModel <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(pt_save_directory) <span class="hljs-meta">&gt;&gt;&gt; </span>tf_model = TFAutoModelForSequenceClassification.from_pretrained(pt_save_directory, from_pt=<span class="hljs-literal">True</span>)</pre></div></div></div> </div> <h2 class="relative group"><a id="custom-model-builds" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#custom-model-builds"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Custom model builds</span></h2> <p>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.</p> <p>Start by importing <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoConfig">AutoConfig</a>, and then load the pretrained model you want to modify. Within <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoConfig.from_pretrained">AutoConfig.from_pretrained()</a>, you can specify the attribute you want to change, such as the number of attention heads:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoConfig <span class="hljs-meta">&gt;&gt;&gt; </span>my_config = AutoConfig.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>, n_heads=<span class="hljs-number">12</span>)</pre></div> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><p>Create a model from your custom configuration with <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.FlaxAutoModelForVision2Seq.from_config">AutoModel.from_config()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModel <span class="hljs-meta">&gt;&gt;&gt; </span>my_model = AutoModel.from_config(my_config)</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><p>Create a model from your custom configuration with <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.FlaxAutoModelForVision2Seq.from_config">TFAutoModel.from_config()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModel <span class="hljs-meta">&gt;&gt;&gt; </span>my_model = TFAutoModel.from_config(my_config)</pre></div></div></div> </div> <p>Take a look at the <a href="./create_a_model">Create a custom architecture</a> guide for more information about building custom configurations.</p> <h2 class="relative group"><a id="trainer-a-pytorch-optimized-training-loop" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#trainer-a-pytorch-optimized-training-loop"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Trainer - a PyTorch optimized training loop</span></h2> <p>All models are a standard <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module" rel="nofollow"><code>torch.nn.Module</code></a> so you can use them in any typical training loop. While you can write your own training loop, 🤗 Transformers provides a <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a> class for PyTorch, which contains the basic training loop and adds additional functionality for features like distributed training, mixed precision, and more.</p> <p>Depending on your task, you’ll typically pass the following parameters to <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a>:</p> <ol><li><p>A <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel">PreTrainedModel</a> or a <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Module" rel="nofollow"><code>torch.nn.Module</code></a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)</pre></div></li> <li><p><a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a> contains the model hyperparameters you can change like learning rate, batch size, and the number of epochs to train for. The default values are used if you don’t specify any training arguments:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments <span class="hljs-meta">&gt;&gt;&gt; </span>training_args = TrainingArguments( <span class="hljs-meta">... </span> output_dir=<span class="hljs-string">"path/to/save/folder/"</span>, <span class="hljs-meta">... </span> learning_rate=<span class="hljs-number">2e-5</span>, <span class="hljs-meta">... </span> per_device_train_batch_size=<span class="hljs-number">8</span>, <span class="hljs-meta">... </span> per_device_eval_batch_size=<span class="hljs-number">8</span>, <span class="hljs-meta">... </span> num_train_epochs=<span class="hljs-number">2</span>, <span class="hljs-meta">... </span>)</pre></div></li> <li><p>A preprocessing class like a tokenizer, image processor, feature extractor, or processor:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)</pre></div></li> <li><p>Load a dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-meta">&gt;&gt;&gt; </span>dataset = load_dataset(<span class="hljs-string">"rotten_tomatoes"</span>) <span class="hljs-comment"># doctest: +IGNORE_RESULT</span></pre></div></li> <li><p>Create a function to tokenize the dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_dataset</span>(<span class="hljs-params">dataset</span>): <span class="hljs-meta">... </span> <span class="hljs-keyword">return</span> tokenizer(dataset[<span class="hljs-string">"text"</span>])</pre></div> <p>Then apply it over the entire dataset with <a href="https://huggingface.co/docs/datasets/v2.12.0/en/package_reference/main_classes#datasets.Dataset.map" rel="nofollow">map</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>dataset = dataset.<span class="hljs-built_in">map</span>(tokenize_dataset, batched=<span class="hljs-literal">True</span>)</pre></div></li> <li><p>A <a href="/docs/transformers/v4.30.0/en/main_classes/data_collator#transformers.DataCollatorWithPadding">DataCollatorWithPadding</a> to create a batch of examples from your dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorWithPadding <span class="hljs-meta">&gt;&gt;&gt; </span>data_collator = DataCollatorWithPadding(tokenizer=tokenizer)</pre></div></li></ol> <p>Now gather all these classes in <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Trainer <span class="hljs-meta">&gt;&gt;&gt; </span>trainer = Trainer( <span class="hljs-meta">... </span> model=model, <span class="hljs-meta">... </span> args=training_args, <span class="hljs-meta">... </span> train_dataset=dataset[<span class="hljs-string">"train"</span>], <span class="hljs-meta">... </span> eval_dataset=dataset[<span class="hljs-string">"test"</span>], <span class="hljs-meta">... </span> tokenizer=tokenizer, <span class="hljs-meta">... </span> data_collator=data_collator, <span class="hljs-meta">... </span>) <span class="hljs-comment"># doctest: +SKIP</span></pre></div> <p>When you’re ready, call <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer.train">train()</a> to start training:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>trainer.train()</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>For tasks - like translation or summarization - that use a sequence-to-sequence model, use the <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Seq2SeqTrainer">Seq2SeqTrainer</a> and <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Seq2SeqTrainingArguments">Seq2SeqTrainingArguments</a> classes instead.</p></div> <p>You can customize the training loop behavior by subclassing the methods inside <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a>. This allows you to customize features such as the loss function, optimizer, and scheduler. Take a look at the <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a> reference for which methods can be subclassed.</p> <p>The other way to customize the training loop is by using <a href="./main_classes/callbacks">Callbacks</a>. You can use callbacks to integrate with other libraries and inspect the training loop to report on progress or stop the training early. Callbacks do not modify anything in the training loop itself. To customize something like the loss function, you need to subclass the <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a> instead.</p> <h2 class="relative group"><a id="train-with-tensorflow" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#train-with-tensorflow"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Train with TensorFlow</span></h2> <p>All models are a standard <a href="https://www.tensorflow.org/api_docs/python/tf/keras/Model" rel="nofollow"><code>tf.keras.Model</code></a> so they can be trained in TensorFlow with the <a href="https://keras.io/" rel="nofollow">Keras</a> API. 🤗 Transformers provides the <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset">prepare_tf_dataset()</a> method to easily load your dataset as a <code>tf.data.Dataset</code> so you can start training right away with Keras’ <a href="https://keras.io/api/models/model_training_apis/#compile-method" rel="nofollow"><code>compile</code></a> and <a href="https://keras.io/api/models/model_training_apis/#fit-method" rel="nofollow"><code>fit</code></a> methods.</p> <ol><li><p>You’ll start with a <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel">TFPreTrainedModel</a> or a <a href="https://www.tensorflow.org/api_docs/python/tf/keras/Model" rel="nofollow"><code>tf.keras.Model</code></a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model = TFAutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)</pre></div></li> <li><p>A preprocessing class like a tokenizer, image processor, feature extractor, or processor:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)</pre></div></li> <li><p>Create a function to tokenize the dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_dataset</span>(<span class="hljs-params">dataset</span>): <span class="hljs-meta">... </span> <span class="hljs-keyword">return</span> tokenizer(dataset[<span class="hljs-string">"text"</span>]) <span class="hljs-comment"># doctest: +SKIP</span></pre></div></li> <li><p>Apply the tokenizer over the entire dataset with <a href="https://huggingface.co/docs/datasets/v2.12.0/en/package_reference/main_classes#datasets.Dataset.map" rel="nofollow">map</a> and then pass the dataset and tokenizer to <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset">prepare_tf_dataset()</a>. You can also change the batch size and shuffle the dataset here if you’d like:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>dataset = dataset.<span class="hljs-built_in">map</span>(tokenize_dataset) <span class="hljs-comment"># doctest: +SKIP</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tf_dataset = model.prepare_tf_dataset( <span class="hljs-meta">... </span> dataset[<span class="hljs-string">"train"</span>], batch_size=<span class="hljs-number">16</span>, shuffle=<span class="hljs-literal">True</span>, tokenizer=tokenizer <span class="hljs-meta">... </span>) <span class="hljs-comment"># doctest: +SKIP</span></pre></div></li> <li><p>When you’re ready, you can call <code>compile</code> and <code>fit</code> to start training. Note that Transformers models all have a default task-relevant loss function, so you don’t need to specify one unless you want to:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tensorflow.keras.optimizers <span class="hljs-keyword">import</span> Adam <span class="hljs-meta">&gt;&gt;&gt; </span>model.<span class="hljs-built_in">compile</span>(optimizer=Adam(<span class="hljs-number">3e-5</span>)) <span class="hljs-comment"># No loss argument!</span> <span class="hljs-meta">&gt;&gt;&gt; </span>model.fit(tf_dataset) <span class="hljs-comment"># doctest: +SKIP</span></pre></div></li></ol> <h2 class="relative group"><a id="whats-next" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#whats-next"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>What's next?</span></h2> <p>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!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/index" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>🤗 Transformers</a> <a href="/docs/transformers/installation" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Installation<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quick-tour&quot;,&quot;url&quot;:&quot;#quick-tour&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Pipeline&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline&quot;,&quot;url&quot;:&quot;#pipeline&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use another model and tokenizer in the pipeline&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;use-another-model-and-tokenizer-in-the-pipeline&quot;,&quot;url&quot;:&quot;#use-another-model-and-tokenizer-in-the-pipeline&quot;}]},{&quot;title&quot;:&quot;AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass&quot;,&quot;url&quot;:&quot;#autoclass&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;AutoTokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autotokenizer&quot;,&quot;url&quot;:&quot;#autotokenizer&quot;},{&quot;title&quot;:&quot;AutoModel&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;automodel&quot;,&quot;url&quot;:&quot;#automodel&quot;},{&quot;title&quot;:&quot;Save a model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;save-a-model&quot;,&quot;url&quot;:&quot;#save-a-model&quot;}]},{&quot;title&quot;:&quot;Custom model builds&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom-model-builds&quot;,&quot;url&quot;:&quot;#custom-model-builds&quot;},{&quot;title&quot;:&quot;Trainer - a PyTorch optimized training loop&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;trainer-a-pytorch-optimized-training-loop&quot;,&quot;url&quot;:&quot;#trainer-a-pytorch-optimized-training-loop&quot;},{&quot;title&quot;:&quot;Train with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;train-with-tensorflow&quot;,&quot;url&quot;:&quot;#train-with-tensorflow&quot;},{&quot;title&quot;:&quot;What's next?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;whats-next&quot;,&quot;url&quot;:&quot;#whats-next&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#quick-tour" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-quick-tour"><wbr>Quick tour</a> <a href="#pipeline" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-pipeline"><wbr>Pipeline</a> <a href="#use-another-model-and-tokenizer-in-the-pipeline" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-use-another-model-and-tokenizer-in-the-pipeline"><wbr>Use another model and tokenizer in the pipeline</a> <a href="#autoclass" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-autoclass"><wbr>Auto<wbr>Class</a> <a href="#autotokenizer" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-autotokenizer"><wbr>Auto<wbr>Tokenizer</a> <a href="#automodel" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-automodel"><wbr>Auto<wbr>Model</a> <a href="#save-a-model" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-save-a-model"><wbr>Save a model</a> <a href="#custom-model-builds" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-custom-model-builds"><wbr>Custom model builds</a> <a href="#trainer-a-pytorch-optimized-training-loop" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-trainer-a-pytorch-optimized-training-loop"><wbr>Trainer - a <wbr>Py<wbr>Torch optimized training loop</a> <a href="#train-with-tensorflow" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-train-with-tensorflow"><wbr>Train with <wbr>Tensor<wbr>Flow</a> <a href="#whats-next" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-whats-next"><wbr>What's next?</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/quicktour" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/quicktour"); } </script> <iframe name="__privateStripeMetricsController6890" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fquicktour&amp;title=Quick%20tour&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:04.759Z
Installation
https://huggingface.co/docs/transformers/installation
Install 🤗 Transformers for whichever deep learning library you’re working with, setup your cache, and optionally configure 🤗 Transformers to run offline. 🤗 Transformers is tested on Python 3.6+, PyTorch 1.1.0+, TensorFlow 2.0+, and Flax. Follow the installation instructions below for the deep learning library you are using: - [PyTorch](https://pytorch.org/get-started/locally/) installation instructions. - [TensorFlow 2.0](https://www.tensorflow.org/install/pip) installation instructions. - [Flax](https://flax.readthedocs.io/en/latest/) installation instructions. ## [](#install-with-pip)Install with pip You should install 🤗 Transformers in a [virtual environment](https://docs.python.org/3/library/venv.html). If you’re unfamiliar with Python virtual environments, take a look at this [guide](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/). A virtual environment makes it easier to manage different projects, and avoid compatibility issues between dependencies. Start by creating a virtual environment in your project directory: Activate the virtual environment. On Linux and MacOs: Activate Virtual environment on Windows Now you’re ready to install 🤗 Transformers with the following command: For CPU-support only, you can conveniently install 🤗 Transformers and a deep learning library in one line. For example, install 🤗 Transformers and PyTorch with: ``` pip install 'transformers[torch]'``` 🤗 Transformers and TensorFlow 2.0: ``` pip install 'transformers[tf-cpu]'``` M1 / ARM Users You will need to install the following before installing TensorFLow 2.0 ``` brew install cmake brew install pkg-config``` 🤗 Transformers and Flax: ``` pip install 'transformers[flax]'``` Finally, check if 🤗 Transformers has been properly installed by running the following command. It will download a pretrained model: ``` python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))"``` Then print out the label and score: ``` [{'label': 'POSITIVE', 'score': 0.9998704791069031}]``` ## [](#install-from-source)Install from source Install 🤗 Transformers from source with the following command: ``` pip install git+https://github.com/huggingface/transformers``` This command installs the bleeding edge `main` version rather than the latest `stable` version. The `main` version is useful for staying up-to-date with the latest developments. For instance, if a bug has been fixed since the last official release but a new release hasn’t been rolled out yet. However, this means the `main` version may not always be stable. We strive to keep the `main` version operational, and most issues are usually resolved within a few hours or a day. If you run into a problem, please open an [Issue](https://github.com/huggingface/transformers/issues) so we can fix it even sooner! Check if 🤗 Transformers has been properly installed by running the following command: ``` python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))"``` ## [](#editable-install)Editable install You will need an editable install if you’d like to: - Use the `main` version of the source code. - Contribute to 🤗 Transformers and need to test changes in the code. Clone the repository and install 🤗 Transformers with the following commands: ``` git clone https://github.com/huggingface/transformers.git cd transformers pip install -e .``` These commands will link the folder you cloned the repository to and your Python library paths. Python will now look inside the folder you cloned to in addition to the normal library paths. For example, if your Python packages are typically installed in `~/anaconda3/envs/main/lib/python3.7/site-packages/`, Python will also search the folder you cloned to: `~/transformers/`. You must keep the `transformers` folder if you want to keep using the library. Now you can easily update your clone to the latest version of 🤗 Transformers with the following command: ``` cd ~/transformers/ git pull``` Your Python environment will find the `main` version of 🤗 Transformers on the next run. ## [](#install-with-conda)Install with conda Install from the conda channel `huggingface`: ``` conda install -c huggingface transformers``` ## [](#cache-setup)Cache setup Pretrained models are downloaded and locally cached at: `~/.cache/huggingface/hub`. This is the default directory given by the shell environment variable `TRANSFORMERS_CACHE`. On Windows, the default directory is given by `C:\Users\username\.cache\huggingface\hub`. You can change the shell environment variables shown below - in order of priority - to specify a different cache directory: 1. Shell environment variable (default): `HUGGINGFACE_HUB_CACHE` or `TRANSFORMERS_CACHE`. 2. Shell environment variable: `HF_HOME`. 3. Shell environment variable: `XDG_CACHE_HOME` + `/huggingface`. 🤗 Transformers will use the shell environment variables `PYTORCH_TRANSFORMERS_CACHE` or `PYTORCH_PRETRAINED_BERT_CACHE` if you are coming from an earlier iteration of this library and have set those environment variables, unless you specify the shell environment variable `TRANSFORMERS_CACHE`. ## [](#offline-mode)Offline mode 🤗 Transformers is able to run in a firewalled or offline environment by only using local files. Set the environment variable `TRANSFORMERS_OFFLINE=1` to enable this behavior. Add [🤗 Datasets](https://huggingface.co/docs/datasets/) to your offline training workflow by setting the environment variable `HF_DATASETS_OFFLINE=1`. For example, you would typically run a program on a normal network firewalled to external instances with the following command: ``` python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en ...``` Run this same program in an offline instance with: ``` HF_DATASETS_OFFLINE=1 TRANSFORMERS_OFFLINE=1 \ python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en ...``` The script should now run without hanging or waiting to timeout because it knows it should only look for local files. ### [](#fetch-models-and-tokenizers-to-use-offline)Fetch models and tokenizers to use offline Another option for using 🤗 Transformers offline is to download the files ahead of time, and then point to their local path when you need to use them offline. There are three ways to do this: - Download a file through the user interface on the [Model Hub](https://huggingface.co/models) by clicking on the ↓ icon. ![download-icon](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/download-icon.png) - Use the [PreTrainedModel.from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) and [PreTrainedModel.save\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.save_pretrained) workflow: 1. Download your files ahead of time with [PreTrainedModel.from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained): ``` >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM >>> tokenizer = AutoTokenizer.from_pretrained("bigscience/T0_3B") >>> model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0_3B")``` 2. Save your files to a specified directory with [PreTrainedModel.save\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.save_pretrained): ``` >>> tokenizer.save_pretrained("./your/path/bigscience_t0") >>> model.save_pretrained("./your/path/bigscience_t0")``` 3. Now when you’re offline, reload your files with [PreTrainedModel.from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) from the specified directory: ``` >>> tokenizer = AutoTokenizer.from_pretrained("./your/path/bigscience_t0") >>> model = AutoModel.from_pretrained("./your/path/bigscience_t0")``` - Programmatically download files with the [huggingface\_hub](https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub) library: 1. Install the `huggingface_hub` library in your virtual environment: ``` python -m pip install huggingface_hub``` 2. Use the [`hf_hub_download`](https://huggingface.co/docs/hub/adding-a-library#download-files-from-the-hub) function to download a file to a specific path. For example, the following command downloads the `config.json` file from the [T0](https://huggingface.co/bigscience/T0_3B) model to your desired path: ``` >>> from huggingface_hub import hf_hub_download >>> hf_hub_download(repo_id="bigscience/T0_3B", filename="config.json", cache_dir="./your/path/bigscience_t0")``` Once your file is downloaded and locally cached, specify it’s local path to load and use it: ``` >>> from transformers import AutoConfig >>> config = AutoConfig.from_pretrained("./your/path/bigscience_t0/config.json")``` See the [How to download files from the Hub](https://huggingface.co/docs/hub/how-to-downstream) section for more details on downloading files stored on the Hub.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Installation"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/installation"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Installation</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;installation&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;install-with-pip&quot;,&quot;title&quot;:&quot;Install with pip&quot;},{&quot;local&quot;:&quot;install-from-source&quot;,&quot;title&quot;:&quot;Install from source&quot;},{&quot;local&quot;:&quot;editable-install&quot;,&quot;title&quot;:&quot;Editable install&quot;},{&quot;local&quot;:&quot;install-with-conda&quot;,&quot;title&quot;:&quot;Install with conda&quot;},{&quot;local&quot;:&quot;cache-setup&quot;,&quot;title&quot;:&quot;Cache setup&quot;},{&quot;local&quot;:&quot;offline-mode&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;fetch-models-and-tokenizers-to-use-offline&quot;,&quot;title&quot;:&quot;Fetch models and tokenizers to use offline&quot;}],&quot;title&quot;:&quot;Offline mode&quot;}],&quot;title&quot;:&quot;Installation&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;installation&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Installation&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Installation</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="installation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#installation"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Installation</span></h1> <p>Install 🤗 Transformers for whichever deep learning library you’re working with, setup your cache, and optionally configure 🤗 Transformers to run offline.</p> <p>🤗 Transformers is tested on Python 3.6+, PyTorch 1.1.0+, TensorFlow 2.0+, and Flax. Follow the installation instructions below for the deep learning library you are using:</p> <ul><li><a href="https://pytorch.org/get-started/locally/" rel="nofollow">PyTorch</a> installation instructions.</li> <li><a href="https://www.tensorflow.org/install/pip" rel="nofollow">TensorFlow 2.0</a> installation instructions.</li> <li><a href="https://flax.readthedocs.io/en/latest/" rel="nofollow">Flax</a> installation instructions.</li></ul> <h2 class="relative group"><a id="install-with-pip" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#install-with-pip"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Install with pip</span></h2> <p>You should install 🤗 Transformers in a <a href="https://docs.python.org/3/library/venv.html" rel="nofollow">virtual environment</a>. If you’re unfamiliar with Python virtual environments, take a look at this <a href="https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/" rel="nofollow">guide</a>. A virtual environment makes it easier to manage different projects, and avoid compatibility issues between dependencies.</p> <p>Start by creating a virtual environment in your project directory:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python -m venv .<span class="hljs-built_in">env</span></pre></div> <p>Activate the virtual environment. On Linux and MacOs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">source</span> .<span class="hljs-built_in">env</span>/bin/activate</pre></div> <p>Activate Virtual environment on Windows</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>.<span class="hljs-built_in">env</span>/Scripts/activate</pre></div> <p>Now you’re ready to install 🤗 Transformers with the following command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install transformers</pre></div> <p>For CPU-support only, you can conveniently install 🤗 Transformers and a deep learning library in one line. For example, install 🤗 Transformers and PyTorch with:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install <span class="hljs-string">'transformers[torch]'</span></pre></div> <p>🤗 Transformers and TensorFlow 2.0:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install <span class="hljs-string">'transformers[tf-cpu]'</span></pre></div> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>M1 / ARM Users</p> <p>You will need to install the following before installing TensorFLow 2.0</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">brew </span><span class="hljs-keyword">install </span>cmake <span class="hljs-keyword">brew </span><span class="hljs-keyword">install </span>pkg-<span class="hljs-built_in">config</span></pre></div></div> <p>🤗 Transformers and Flax:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install <span class="hljs-string">'transformers[flax]'</span></pre></div> <p>Finally, check if 🤗 Transformers has been properly installed by running the following command. It will download a pretrained model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python -c <span class="hljs-string">"from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))"</span></pre></div> <p>Then print out the label and score:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>[{<span class="hljs-string">'label'</span>: <span class="hljs-string">'POSITIVE'</span>, <span class="hljs-string">'score'</span>: 0.9998704791069031}]</pre></div> <h2 class="relative group"><a id="install-from-source" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#install-from-source"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Install from source</span></h2> <p>Install 🤗 Transformers from source with the following command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install git+https://github.com/huggingface/transformers</pre></div> <p>This command installs the bleeding edge <code>main</code> version rather than the latest <code>stable</code> version. The <code>main</code> version is useful for staying up-to-date with the latest developments. For instance, if a bug has been fixed since the last official release but a new release hasn’t been rolled out yet. However, this means the <code>main</code> version may not always be stable. We strive to keep the <code>main</code> version operational, and most issues are usually resolved within a few hours or a day. If you run into a problem, please open an <a href="https://github.com/huggingface/transformers/issues" rel="nofollow">Issue</a> so we can fix it even sooner!</p> <p>Check if 🤗 Transformers has been properly installed by running the following command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python -c <span class="hljs-string">"from transformers import pipeline; print(pipeline('sentiment-analysis')('I love you'))"</span></pre></div> <h2 class="relative group"><a id="editable-install" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#editable-install"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Editable install</span></h2> <p>You will need an editable install if you’d like to:</p> <ul><li>Use the <code>main</code> version of the source code.</li> <li>Contribute to 🤗 Transformers and need to test changes in the code.</li></ul> <p>Clone the repository and install 🤗 Transformers with the following commands:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>git <span class="hljs-built_in">clone</span> https://github.com/huggingface/transformers.git <span class="hljs-built_in">cd</span> transformers pip install -e .</pre></div> <p>These commands will link the folder you cloned the repository to and your Python library paths. Python will now look inside the folder you cloned to in addition to the normal library paths. For example, if your Python packages are typically installed in <code>~/anaconda3/envs/main/lib/python3.7/site-packages/</code>, Python will also search the folder you cloned to: <code>~/transformers/</code>.</p> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>You must keep the <code>transformers</code> folder if you want to keep using the library.</p></div> <p>Now you can easily update your clone to the latest version of 🤗 Transformers with the following command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-built_in">cd</span> ~/transformers/ git pull</pre></div> <p>Your Python environment will find the <code>main</code> version of 🤗 Transformers on the next run.</p> <h2 class="relative group"><a id="install-with-conda" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#install-with-conda"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Install with conda</span></h2> <p>Install from the conda channel <code>huggingface</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>conda install -c huggingface transformers</pre></div> <h2 class="relative group"><a id="cache-setup" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#cache-setup"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Cache setup</span></h2> <p>Pretrained models are downloaded and locally cached at: <code>~/.cache/huggingface/hub</code>. This is the default directory given by the shell environment variable <code>TRANSFORMERS_CACHE</code>. On Windows, the default directory is given by <code>C:\Users\username\.cache\huggingface\hub</code>. You can change the shell environment variables shown below - in order of priority - to specify a different cache directory:</p> <ol><li>Shell environment variable (default): <code>HUGGINGFACE_HUB_CACHE</code> or <code>TRANSFORMERS_CACHE</code>.</li> <li>Shell environment variable: <code>HF_HOME</code>.</li> <li>Shell environment variable: <code>XDG_CACHE_HOME</code> + <code>/huggingface</code>.</li></ol> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>🤗 Transformers will use the shell environment variables <code>PYTORCH_TRANSFORMERS_CACHE</code> or <code>PYTORCH_PRETRAINED_BERT_CACHE</code> if you are coming from an earlier iteration of this library and have set those environment variables, unless you specify the shell environment variable <code>TRANSFORMERS_CACHE</code>.</p></div> <h2 class="relative group"><a id="offline-mode" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#offline-mode"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Offline mode</span></h2> <p>🤗 Transformers is able to run in a firewalled or offline environment by only using local files. Set the environment variable <code>TRANSFORMERS_OFFLINE=1</code> to enable this behavior.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>Add <a href="https://huggingface.co/docs/datasets/" rel="nofollow">🤗 Datasets</a> to your offline training workflow by setting the environment variable <code>HF_DATASETS_OFFLINE=1</code>.</p></div> <p>For example, you would typically run a program on a normal network firewalled to external instances with the following command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en ...</pre></div> <p>Run this same program in an offline instance with:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>HF_DATASETS_OFFLINE=1 TRANSFORMERS_OFFLINE=1 \ python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en ...</pre></div> <p>The script should now run without hanging or waiting to timeout because it knows it should only look for local files.</p> <h3 class="relative group"><a id="fetch-models-and-tokenizers-to-use-offline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fetch-models-and-tokenizers-to-use-offline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fetch models and tokenizers to use offline</span></h3> <p>Another option for using 🤗 Transformers offline is to download the files ahead of time, and then point to their local path when you need to use them offline. There are three ways to do this:</p> <ul><li><p>Download a file through the user interface on the <a href="https://huggingface.co/models" rel="nofollow">Model Hub</a> by clicking on the ↓ icon.</p> <p><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/download-icon.png" alt="download-icon"></p></li> <li><p>Use the <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained">PreTrainedModel.from_pretrained()</a> and <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.save_pretrained">PreTrainedModel.save_pretrained()</a> workflow:</p> <ol><li><p>Download your files ahead of time with <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained">PreTrainedModel.from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForSeq2SeqLM <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bigscience/T0_3B"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSeq2SeqLM.from_pretrained(<span class="hljs-string">"bigscience/T0_3B"</span>)</pre></div></li> <li><p>Save your files to a specified directory with <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.save_pretrained">PreTrainedModel.save_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.save_pretrained(<span class="hljs-string">"./your/path/bigscience_t0"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model.save_pretrained(<span class="hljs-string">"./your/path/bigscience_t0"</span>)</pre></div></li> <li><p>Now when you’re offline, reload your files with <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained">PreTrainedModel.from_pretrained()</a> from the specified directory:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"./your/path/bigscience_t0"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModel.from_pretrained(<span class="hljs-string">"./your/path/bigscience_t0"</span>)</pre></div></li></ol></li> <li><p>Programmatically download files with the <a href="https://github.com/huggingface/huggingface_hub/tree/main/src/huggingface_hub" rel="nofollow">huggingface_hub</a> library:</p> <ol><li><p>Install the <code>huggingface_hub</code> library in your virtual environment:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python -m pip install huggingface_hub</pre></div></li> <li><p>Use the <a href="https://huggingface.co/docs/hub/adding-a-library#download-files-from-the-hub" rel="nofollow"><code>hf_hub_download</code></a> function to download a file to a specific path. For example, the following command downloads the <code>config.json</code> file from the <a href="https://huggingface.co/bigscience/T0_3B" rel="nofollow">T0</a> model to your desired path:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> hf_hub_download <span class="hljs-meta">&gt;&gt;&gt; </span>hf_hub_download(repo_id=<span class="hljs-string">"bigscience/T0_3B"</span>, filename=<span class="hljs-string">"config.json"</span>, cache_dir=<span class="hljs-string">"./your/path/bigscience_t0"</span>)</pre></div></li></ol></li></ul> <p>Once your file is downloaded and locally cached, specify it’s local path to load and use it:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoConfig <span class="hljs-meta">&gt;&gt;&gt; </span>config = AutoConfig.from_pretrained(<span class="hljs-string">"./your/path/bigscience_t0/config.json"</span>)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>See the <a href="https://huggingface.co/docs/hub/how-to-downstream" rel="nofollow">How to download files from the Hub</a> section for more details on downloading files stored on the Hub.</p></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/quicktour" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Quick tour</a> <a href="/docs/transformers/pipeline_tutorial" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Run inference with pipelines<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;#installation&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Install with pip&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;install-with-pip&quot;,&quot;url&quot;:&quot;#install-with-pip&quot;},{&quot;title&quot;:&quot;Install from source&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;install-from-source&quot;,&quot;url&quot;:&quot;#install-from-source&quot;},{&quot;title&quot;:&quot;Editable install&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;editable-install&quot;,&quot;url&quot;:&quot;#editable-install&quot;},{&quot;title&quot;:&quot;Install with conda&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;install-with-conda&quot;,&quot;url&quot;:&quot;#install-with-conda&quot;},{&quot;title&quot;:&quot;Cache setup&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;cache-setup&quot;,&quot;url&quot;:&quot;#cache-setup&quot;},{&quot;title&quot;:&quot;Offline mode&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;offline-mode&quot;,&quot;url&quot;:&quot;#offline-mode&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Fetch models and tokenizers to use offline&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fetch-models-and-tokenizers-to-use-offline&quot;,&quot;url&quot;:&quot;#fetch-models-and-tokenizers-to-use-offline&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#installation" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-installation"><wbr>Installation</a> <a href="#install-with-pip" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-install-with-pip"><wbr>Install with pip</a> <a href="#install-from-source" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-install-from-source"><wbr>Install from source</a> <a href="#editable-install" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-editable-install"><wbr>Editable install</a> <a href="#install-with-conda" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-install-with-conda"><wbr>Install with conda</a> <a href="#cache-setup" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-cache-setup"><wbr>Cache setup</a> <a href="#offline-mode" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-offline-mode"><wbr>Offline mode</a> <a href="#fetch-models-and-tokenizers-to-use-offline" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-fetch-models-and-tokenizers-to-use-offline"><wbr>Fetch models and tokenizers to use offline</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/installation" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/installation"); } </script> <iframe name="__privateStripeMetricsController0500" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Finstallation&amp;title=Installation&amp;referrer=&amp;muid=NA&amp;sid=NA&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:04.865Z
Load pretrained instances with an AutoClass
https://huggingface.co/docs/transformers/autoclass_tutorial
With so many different Transformer architectures, it can be challenging to create one for your checkpoint. As a part of 🤗 Transformers core philosophy to make the library easy, simple and flexible to use, an `AutoClass` automatically infer and load the correct architecture from a given checkpoint. The `from_pretrained()` method lets you quickly load a pretrained model for any architecture so you don’t have to devote time and resources to train a model from scratch. Producing this type of checkpoint-agnostic code means if your code works for one checkpoint, it will work with another checkpoint - as long as it was trained for a similar task - even if the architecture is different. Remember, architecture refers to the skeleton of the model and checkpoints are the weights for a given architecture. For example, [BERT](https://huggingface.co/bert-base-uncased) is an architecture, while `bert-base-uncased` is a checkpoint. Model is a general term that can mean either architecture or checkpoint. In this tutorial, learn to: - Load a pretrained tokenizer. - Load a pretrained image processor - Load a pretrained feature extractor. - Load a pretrained processor. - Load a pretrained model. ## [](#autotokenizer)AutoTokenizer Nearly every NLP task begins with a tokenizer. A tokenizer converts your input into a format that can be processed by the model. Load a tokenizer with [AutoTokenizer.from\_pretrained()](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer.from_pretrained): ``` HTML_TAG_START >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") HTML_TAG_END ``` Then tokenize your input as shown below: ``` HTML_TAG_START >>> sequence = "In a hole in the ground there lived a hobbit." >>> print(tokenizer(sequence)) {'input_ids': [101, 1999, 1037, 4920, 1999, 1996, 2598, 2045, 2973, 1037, 7570, 10322, 4183, 1012, 102], 'token_type_ids': [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]} HTML_TAG_END ``` ## [](#autoimageprocessor)AutoImageProcessor For vision tasks, an image processor processes the image into the correct input format. ``` HTML_TAG_START >>> from transformers import AutoImageProcessor >>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224") HTML_TAG_END ``` ## [](#autofeatureextractor)AutoFeatureExtractor For audio tasks, a feature extractor processes the audio signal the correct input format. Load a feature extractor with [AutoFeatureExtractor.from\_pretrained()](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoFeatureExtractor.from_pretrained): ``` HTML_TAG_START >>> from transformers import AutoFeatureExtractor >>> feature_extractor = AutoFeatureExtractor.from_pretrained( ... "ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition" ... ) HTML_TAG_END ``` ## [](#autoprocessor)AutoProcessor Multimodal tasks require a processor that combines two types of preprocessing tools. For example, the [LayoutLMV2](model_doc/layoutlmv2) model requires an image processor to handle images and a tokenizer to handle text; a processor combines both of them. Load a processor with [AutoProcessor.from\_pretrained()](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoProcessor.from_pretrained): ``` HTML_TAG_START >>> from transformers import AutoProcessor >>> processor = AutoProcessor.from_pretrained("microsoft/layoutlmv2-base-uncased") HTML_TAG_END ``` ## [](#automodel)AutoModel Finally, the `AutoModelFor` classes let you load a pretrained model for a given task (see [here](model_doc/auto) for a complete list of available tasks). For example, load a model for sequence classification with [AutoModelForSequenceClassification.from\_pretrained()](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.FlaxAutoModelForVision2Seq.from_pretrained): ``` HTML_TAG_START >>> from transformers import AutoModelForSequenceClassification >>> model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased") HTML_TAG_END ``` Easily reuse the same checkpoint to load an architecture for a different task: ``` HTML_TAG_START >>> from transformers import AutoModelForTokenClassification >>> model = AutoModelForTokenClassification.from_pretrained("distilbert-base-uncased") HTML_TAG_END ``` For PyTorch models, the `from_pretrained()` method uses `torch.load()` which internally uses `pickle` and is known to be insecure. In general, never load a model that could have come from an untrusted source, or that could have been tampered with. This security risk is partially mitigated for public models hosted on the Hugging Face Hub, which are [scanned for malware](https://huggingface.co/docs/hub/security-malware) at each commit. See the [Hub documentation](https://huggingface.co/docs/hub/security) for best practices like [signed commit verification](https://huggingface.co/docs/hub/security-gpg#signing-commits-with-gpg) with GPG. TensorFlow and Flax checkpoints are not affected, and can be loaded within PyTorch architectures using the `from_tf` and `from_flax` kwargs for the `from_pretrained` method to circumvent this issue. Generally, we recommend using the `AutoTokenizer` class and the `AutoModelFor` class to load pretrained instances of models. This will ensure you load the correct architecture every time. In the next [tutorial](preprocessing), learn how to use your newly loaded tokenizer, image processor, feature extractor and processor to preprocess a dataset for fine-tuning. Finally, the `TFAutoModelFor` classes let you load a pretrained model for a given task (see [here](model_doc/auto) for a complete list of available tasks). For example, load a model for sequence classification with [TFAutoModelForSequenceClassification.from\_pretrained()](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.FlaxAutoModelForVision2Seq.from_pretrained): ``` HTML_TAG_START >>> from transformers import TFAutoModelForSequenceClassification >>> model = TFAutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased") HTML_TAG_END ``` Easily reuse the same checkpoint to load an architecture for a different task: ``` HTML_TAG_START >>> from transformers import TFAutoModelForTokenClassification >>> model = TFAutoModelForTokenClassification.from_pretrained("distilbert-base-uncased") HTML_TAG_END ``` Generally, we recommend using the `AutoTokenizer` class and the `TFAutoModelFor` class to load pretrained instances of models. This will ensure you load the correct architecture every time. In the next [tutorial](preprocessing), learn how to use your newly loaded tokenizer, image processor, feature extractor and processor to preprocess a dataset for fine-tuning.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Load pretrained instances with an AutoClass"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/autoclass_tutorial"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Load pretrained instances with an AutoClass</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;autoclass_tutorial&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Load pretrained instances with an AutoClass&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Load pretrained instances with an AutoClass</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"><!-- HTML_TAG_START --> <link rel="modulepreload" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"> <link rel="modulepreload" href="/docs/transformers/v4.30.0/en/_app/start-hf-doc-builder.js"> <link rel="modulepreload" href="/docs/transformers/v4.30.0/en/_app/chunks/vendor-hf-doc-builder.js"> <link rel="modulepreload" href="/docs/transformers/v4.30.0/en/_app/chunks/paths-hf-doc-builder.js"> <link rel="modulepreload" href="/docs/transformers/v4.30.0/en/_app/pages/__layout.svelte-hf-doc-builder.js"> <link rel="modulepreload" href="/docs/transformers/v4.30.0/en/_app/pages/autoclass_tutorial.mdx-hf-doc-builder.js"> <link rel="modulepreload" href="/docs/transformers/v4.30.0/en/_app/chunks/Tip-hf-doc-builder.js"> <link rel="modulepreload" href="/docs/transformers/v4.30.0/en/_app/chunks/IconCopyLink-hf-doc-builder.js"> <link rel="modulepreload" href="/docs/transformers/v4.30.0/en/_app/chunks/CodeBlock-hf-doc-builder.js"> <link rel="modulepreload" href="/docs/transformers/v4.30.0/en/_app/chunks/Markdown-hf-doc-builder.js"> <h1 class="relative group"><a id="load-pretrained-instances-with-an-autoclass" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#load-pretrained-instances-with-an-autoclass"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Load pretrained instances with an AutoClass </span></h1> <p>With so many different Transformer architectures, it can be challenging to create one for your checkpoint. As a part of 🤗 Transformers core philosophy to make the library easy, simple and flexible to use, an <code>AutoClass</code> automatically infer and load the correct architecture from a given checkpoint. The <code>from_pretrained()</code> method lets you quickly load a pretrained model for any architecture so you don’t have to devote time and resources to train a model from scratch. Producing this type of checkpoint-agnostic code means if your code works for one checkpoint, it will work with another checkpoint - as long as it was trained for a similar task - even if the architecture is different.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>Remember, architecture refers to the skeleton of the model and checkpoints are the weights for a given architecture. For example, <a href="https://huggingface.co/bert-base-uncased" rel="nofollow">BERT</a> is an architecture, while <code>bert-base-uncased</code> is a checkpoint. Model is a general term that can mean either architecture or checkpoint.</p></div> <p>In this tutorial, learn to:</p> <ul><li>Load a pretrained tokenizer.</li> <li>Load a pretrained image processor</li> <li>Load a pretrained feature extractor.</li> <li>Load a pretrained processor.</li> <li>Load a pretrained model.</li></ul> <h2 class="relative group"><a id="autotokenizer" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#autotokenizer"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>AutoTokenizer </span></h2> <p>Nearly every NLP task begins with a tokenizer. A tokenizer converts your input into a format that can be processed by the model.</p> <p>Load a tokenizer with <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer.from_pretrained">AutoTokenizer.from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-uncased"</span>)<!-- HTML_TAG_END --></pre></div> <p>Then tokenize your input as shown below:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span>sequence = <span class="hljs-string">"In a hole in the ground there lived a hobbit."</span> <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(tokenizer(sequence)) {<span class="hljs-string">'input_ids'</span>: [<span class="hljs-number">101</span>, <span class="hljs-number">1999</span>, <span class="hljs-number">1037</span>, <span class="hljs-number">4920</span>, <span class="hljs-number">1999</span>, <span class="hljs-number">1996</span>, <span class="hljs-number">2598</span>, <span class="hljs-number">2045</span>, <span class="hljs-number">2973</span>, <span class="hljs-number">1037</span>, <span class="hljs-number">7570</span>, <span class="hljs-number">10322</span>, <span class="hljs-number">4183</span>, <span class="hljs-number">1012</span>, <span class="hljs-number">102</span>], <span class="hljs-string">'token_type_ids'</span>: [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], <span class="hljs-string">'attention_mask'</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]}<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="autoimageprocessor" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#autoimageprocessor"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>AutoImageProcessor </span></h2> <p>For vision tasks, an image processor processes the image into the correct input format.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoImageProcessor <span class="hljs-meta">&gt;&gt;&gt; </span>image_processor = AutoImageProcessor.from_pretrained(<span class="hljs-string">"google/vit-base-patch16-224"</span>)<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="autofeatureextractor" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#autofeatureextractor"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>AutoFeatureExtractor </span></h2> <p>For audio tasks, a feature extractor processes the audio signal the correct input format.</p> <p>Load a feature extractor with <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoFeatureExtractor.from_pretrained">AutoFeatureExtractor.from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoFeatureExtractor <span class="hljs-meta">&gt;&gt;&gt; </span>feature_extractor = AutoFeatureExtractor.from_pretrained( <span class="hljs-meta">... </span> <span class="hljs-string">"ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"</span> <span class="hljs-meta">... </span>)<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="autoprocessor" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#autoprocessor"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>AutoProcessor </span></h2> <p>Multimodal tasks require a processor that combines two types of preprocessing tools. For example, the <a href="model_doc/layoutlmv2">LayoutLMV2</a> model requires an image processor to handle images and a tokenizer to handle text; a processor combines both of them.</p> <p>Load a processor with <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoProcessor.from_pretrained">AutoProcessor.from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoProcessor <span class="hljs-meta">&gt;&gt;&gt; </span>processor = AutoProcessor.from_pretrained(<span class="hljs-string">"microsoft/layoutlmv2-base-uncased"</span>)<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="automodel" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#automodel"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>AutoModel </span></h2> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"> <p>Finally, the <code>AutoModelFor</code> classes let you load a pretrained model for a given task (see <a href="model_doc/auto">here</a> for a complete list of available tasks). For example, load a model for sequence classification with <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.FlaxAutoModelForVision2Seq.from_pretrained">AutoModelForSequenceClassification.from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)<!-- HTML_TAG_END --></pre></div> <p>Easily reuse the same checkpoint to load an architecture for a different task:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForTokenClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForTokenClassification.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)<!-- HTML_TAG_END --></pre></div> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>For PyTorch models, the <code>from_pretrained()</code> method uses <code>torch.load()</code> which internally uses <code>pickle</code> and is known to be insecure. In general, never load a model that could have come from an untrusted source, or that could have been tampered with. This security risk is partially mitigated for public models hosted on the Hugging Face Hub, which are <a href="https://huggingface.co/docs/hub/security-malware" rel="nofollow">scanned for malware</a> at each commit. See the <a href="https://huggingface.co/docs/hub/security" rel="nofollow">Hub documentation</a> for best practices like <a href="https://huggingface.co/docs/hub/security-gpg#signing-commits-with-gpg" rel="nofollow">signed commit verification</a> with GPG.</p> <p>TensorFlow and Flax checkpoints are not affected, and can be loaded within PyTorch architectures using the <code>from_tf</code> and <code>from_flax</code> kwargs for the <code>from_pretrained</code> method to circumvent this issue.</p></div> <p>Generally, we recommend using the <code>AutoTokenizer</code> class and the <code>AutoModelFor</code> class to load pretrained instances of models. This will ensure you load the correct architecture every time. In the next <a href="preprocessing">tutorial</a>, learn how to use your newly loaded tokenizer, image processor, feature extractor and processor to preprocess a dataset for fine-tuning.</p></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"> <p>Finally, the <code>TFAutoModelFor</code> classes let you load a pretrained model for a given task (see <a href="model_doc/auto">here</a> for a complete list of available tasks). For example, load a model for sequence classification with <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.FlaxAutoModelForVision2Seq.from_pretrained">TFAutoModelForSequenceClassification.from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model = TFAutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)<!-- HTML_TAG_END --></pre></div> <p>Easily reuse the same checkpoint to load an architecture for a different task:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><!-- HTML_TAG_START --><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForTokenClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model = TFAutoModelForTokenClassification.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)<!-- HTML_TAG_END --></pre></div> <p>Generally, we recommend using the <code>AutoTokenizer</code> class and the <code>TFAutoModelFor</code> class to load pretrained instances of models. This will ensure you load the correct architecture every time. In the next <a href="preprocessing">tutorial</a>, learn how to use your newly loaded tokenizer, image processor, feature extractor and processor to preprocess a dataset for fine-tuning.</p> </div></div> </div> <script type="module" data-hydrate="10cri8c"> import { start } from "/docs/transformers/v4.30.0/en/_app/start-hf-doc-builder.js"; start({ target: document.querySelector('[data-hydrate="10cri8c"]').parentNode, paths: {"base":"/docs/transformers/v4.30.0/en","assets":"/docs/transformers/v4.30.0/en"}, session: {}, route: false, spa: false, trailing_slash: "never", hydrate: { status: 200, error: null, nodes: [ import("/docs/transformers/v4.30.0/en/_app/pages/__layout.svelte-hf-doc-builder.js"), import("/docs/transformers/v4.30.0/en/_app/pages/autoclass_tutorial.mdx-hf-doc-builder.js") ], params: {} } }); </script> <!-- HTML_TAG_END --></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/pipeline_tutorial" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Run inference with pipelines</a> <a href="/docs/transformers/preprocessing" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Preprocess data<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Load pretrained instances with an AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;load-pretrained-instances-with-an-autoclass&quot;,&quot;url&quot;:&quot;#load-pretrained-instances-with-an-autoclass&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;AutoTokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autotokenizer&quot;,&quot;url&quot;:&quot;#autotokenizer&quot;},{&quot;title&quot;:&quot;AutoImageProcessor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoimageprocessor&quot;,&quot;url&quot;:&quot;#autoimageprocessor&quot;},{&quot;title&quot;:&quot;AutoFeatureExtractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autofeatureextractor&quot;,&quot;url&quot;:&quot;#autofeatureextractor&quot;},{&quot;title&quot;:&quot;AutoProcessor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoprocessor&quot;,&quot;url&quot;:&quot;#autoprocessor&quot;},{&quot;title&quot;:&quot;AutoModel&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;automodel&quot;,&quot;url&quot;:&quot;#automodel&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#load-pretrained-instances-with-an-autoclass" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-load-pretrained-instances-with-an-autoclass"><wbr>Load pretrained instances with an <wbr>Auto<wbr>Class</a> <a href="#autotokenizer" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-autotokenizer"><wbr>Auto<wbr>Tokenizer</a> <a href="#autoimageprocessor" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-autoimageprocessor"><wbr>Auto<wbr>Image<wbr>Processor</a> <a href="#autofeatureextractor" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-autofeatureextractor"><wbr>Auto<wbr>Feature<wbr>Extractor</a> <a href="#autoprocessor" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-autoprocessor"><wbr>Auto<wbr>Processor</a> <a href="#automodel" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-automodel"><wbr>Auto<wbr>Model</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/autoclass_tutorial" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/autoclass_tutorial"); } </script> <iframe name="__privateStripeMetricsController9760" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fautoclass_tutorial&amp;title=Load%20pretrained%20instances%20with%20an%20AutoClass&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:05.500Z
Pipelines for inference
https://huggingface.co/docs/transformers/pipeline_tutorial
The [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) makes it simple to use any model from the [Hub](https://huggingface.co/models) for inference on any language, computer vision, speech, and multimodal tasks. Even if you don’t have experience with a specific modality or aren’t familiar with the underlying code behind the models, you can still use them for inference with the [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline)! This tutorial will teach you to: - Use a [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) for inference. - Use a specific tokenizer or model. - Use a [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) for audio, vision, and multimodal tasks. Take a look at the [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) documentation for a complete list of supported tasks and available parameters. ## [](#pipeline-usage)Pipeline usage While each task has an associated [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline), it is simpler to use the general [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) abstraction which contains all the task-specific pipelines. The [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) automatically loads a default model and a preprocessing class capable of inference for your task. 1. Start by creating a [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) and specify an inference task: ``` >>> from transformers import pipeline >>> generator = pipeline(task="automatic-speech-recognition")``` 2. Pass your input text to the [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline): ``` >>> generator("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") {'text': 'I HAVE A DREAM BUT ONE DAY THIS NATION WILL RISE UP LIVE UP THE TRUE MEANING OF ITS TREES'}``` Not the result you had in mind? Check out some of the [most downloaded automatic speech recognition models](https://huggingface.co/models?pipeline_tag=automatic-speech-recognition&sort=downloads) on the Hub to see if you can get a better transcription. Let’s try [openai/whisper-large](https://huggingface.co/openai/whisper-large): ``` >>> generator = pipeline(model="openai/whisper-large") >>> generator("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") {'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'}``` Now this result looks more accurate! We really encourage you to check out the Hub for models in different languages, models specialized in your field, and more. You can check out and compare model results directly from your browser on the Hub to see if it fits or handles corner cases better than other ones. And if you don’t find a model for your use case, you can always start [training](training) your own! If you have several inputs, you can pass your input as a list: ``` generator( [ "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac", "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/1.flac", ] )``` If you want to iterate over a whole dataset, or want to use it for inference in a webserver, check out dedicated parts [Using pipelines on a dataset](#using-pipelines-on-a-dataset) [Using pipelines for a webserver](./pipeline_webserver) ## [](#parameters)Parameters [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) supports many parameters; some are task specific, and some are general to all pipelines. In general you can specify parameters anywhere you want: ``` generator = pipeline(model="openai/whisper-large", my_parameter=1) out = generator(...) out = generator(..., my_parameter=2) out = generator(...) ``` Let’s check out 3 important ones: ### [](#device)Device If you use `device=n`, the pipeline automatically puts the model on the specified device. This will work regardless of whether you are using PyTorch or Tensorflow. ``` generator = pipeline(model="openai/whisper-large", device=0)``` If the model is too large for a single GPU, you can set `device_map="auto"` to allow 🤗 [Accelerate](https://huggingface.co/docs/accelerate) to automatically determine how to load and store the model weights. ``` generator = pipeline(model="openai/whisper-large", device_map="auto")``` Note that if `device_map="auto"` is passed, there is no need to add the argument `device=device` when instantiating your `pipeline` as you may encounter some unexpected behavior! ### [](#batch-size)Batch size By default, pipelines will not batch inference for reasons explained in detail [here](https://huggingface.co/docs/transformers/main_classes/pipelines#pipeline-batching). The reason is that batching is not necessarily faster, and can actually be quite slower in some cases. But if it works in your use case, you can use: ``` generator = pipeline(model="openai/whisper-large", device=0, batch_size=2) audio_filenames = [f"audio_{i}.flac" for i in range(10)] texts = generator(audio_filenames)``` This runs the pipeline on the 10 provided audio files, but it will pass them in batches of 2 to the model (which is on a GPU, where batching is more likely to help) without requiring any further code from you. The output should always match what you would have received without batching. It is only meant as a way to help you get more speed out of a pipeline. Pipelines can also alleviate some of the complexities of batching because, for some pipelines, a single item (like a long audio file) needs to be chunked into multiple parts to be processed by a model. The pipeline performs this [_chunk batching_](./main_classes/pipelines#pipeline-chunk-batching) for you. ### [](#task-specific-parameters)Task specific parameters All tasks provide task specific parameters which allow for additional flexibility and options to help you get your job done. For instance, the [transformers.AutomaticSpeechRecognitionPipeline.**call**()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.AutomaticSpeechRecognitionPipeline.__call__) method has a `return_timestamps` parameter which sounds promising for subtitling videos: ``` >>> >>> generator = pipeline(model="facebook/wav2vec2-large-960h-lv60-self", return_timestamps="word") >>> generator("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac") {'text': 'I HAVE A DREAM BUT ONE DAY THIS NATION WILL RISE UP AND LIVE OUT THE TRUE MEANING OF ITS CREED', 'chunks': [{'text': 'I', 'timestamp': (1.22, 1.24)}, {'text': 'HAVE', 'timestamp': (1.42, 1.58)}, {'text': 'A', 'timestamp': (1.66, 1.68)}, {'text': 'DREAM', 'timestamp': (1.76, 2.14)}, {'text': 'BUT', 'timestamp': (3.68, 3.8)}, {'text': 'ONE', 'timestamp': (3.94, 4.06)}, {'text': 'DAY', 'timestamp': (4.16, 4.3)}, {'text': 'THIS', 'timestamp': (6.36, 6.54)}, {'text': 'NATION', 'timestamp': (6.68, 7.1)}, {'text': 'WILL', 'timestamp': (7.32, 7.56)}, {'text': 'RISE', 'timestamp': (7.8, 8.26)}, {'text': 'UP', 'timestamp': (8.38, 8.48)}, {'text': 'AND', 'timestamp': (10.08, 10.18)}, {'text': 'LIVE', 'timestamp': (10.26, 10.48)}, {'text': 'OUT', 'timestamp': (10.58, 10.7)}, {'text': 'THE', 'timestamp': (10.82, 10.9)}, {'text': 'TRUE', 'timestamp': (10.98, 11.18)}, {'text': 'MEANING', 'timestamp': (11.26, 11.58)}, {'text': 'OF', 'timestamp': (11.66, 11.7)}, {'text': 'ITS', 'timestamp': (11.76, 11.88)}, {'text': 'CREED', 'timestamp': (12.0, 12.38)}]}``` As you can see, the model inferred the text and also outputted **when** the various words were pronounced in the sentence. There are many parameters available for each task, so check out each task’s API reference to see what you can tinker with! For instance, the [AutomaticSpeechRecognitionPipeline](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.AutomaticSpeechRecognitionPipeline) has a `chunk_length_s` parameter which is helpful for working on really long audio files (for example, subtitling entire movies or hour-long videos) that a model typically cannot handle on its own. If you can’t find a parameter that would really help you out, feel free to [request it](https://github.com/huggingface/transformers/issues/new?assignees=&labels=feature&template=feature-request.yml)! ## [](#using-pipelines-on-a-dataset)Using pipelines on a dataset The pipeline can also run inference on a large dataset. The easiest way we recommend doing this is by using an iterator: ``` def data(): for i in range(1000): yield f"My example {i}" pipe = pipeline(model="gpt2", device=0) generated_characters = 0 for out in pipe(data()): generated_characters += len(out[0]["generated_text"])``` The iterator `data()` yields each result, and the pipeline automatically recognizes the input is iterable and will start fetching the data while it continues to process it on the GPU (this uses [DataLoader](https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader) under the hood). This is important because you don’t have to allocate memory for the whole dataset and you can feed the GPU as fast as possible. Since batching could speed things up, it may be useful to try tuning the `batch_size` parameter here. The simplest way to iterate over a dataset is to just load one from 🤗 [Datasets](https://github.com/huggingface/datasets/): ``` from transformers.pipelines.pt_utils import KeyDataset from datasets import load_dataset pipe = pipeline(model="hf-internal-testing/tiny-random-wav2vec2", device=0) dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation[:10]") for out in pipe(KeyDataset(dataset, "audio")): print(out)``` ## [](#using-pipelines-for-a-webserver)Using pipelines for a webserver Creating an inference engine is a complex topic which deserves it's own page. [Link](./pipeline_webserver) ## [](#vision-pipeline)Vision pipeline Using a [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) for vision tasks is practically identical. Specify your task and pass your image to the classifier. The image can be a link or a local path to the image. For example, what species of cat is shown below? ![pipeline-cat-chonk](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg) ``` >>> from transformers import pipeline >>> vision_classifier = pipeline(model="google/vit-base-patch16-224") >>> preds = vision_classifier( ... images="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" ... ) >>> preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds] >>> preds [{'score': 0.4335, 'label': 'lynx, catamount'}, {'score': 0.0348, 'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'}, {'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'}, {'score': 0.0239, 'label': 'Egyptian cat'}, {'score': 0.0229, 'label': 'tiger cat'}]``` ## [](#text-pipeline)Text pipeline Using a [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) for NLP tasks is practically identical. ``` >>> from transformers import pipeline >>> >>> >>> classifier = pipeline(model="facebook/bart-large-mnli") >>> classifier( ... "I have a problem with my iphone that needs to be resolved asap!!", ... candidate_labels=["urgent", "not urgent", "phone", "tablet", "computer"], ... ) {'sequence': 'I have a problem with my iphone that needs to be resolved asap!!', 'labels': ['urgent', 'phone', 'computer', 'not urgent', 'tablet'], 'scores': [0.504, 0.479, 0.013, 0.003, 0.002]}``` ## [](#multimodal-pipeline)Multimodal pipeline The [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) supports more than one modality. For example, a visual question answering (VQA) task combines text and image. Feel free to use any image link you like and a question you want to ask about the image. The image can be a URL or a local path to the image. For example, if you use this [invoice image](https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png): ``` >>> from transformers import pipeline >>> vqa = pipeline(model="impira/layoutlm-document-qa") >>> vqa( ... image="https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png", ... question="What is the invoice number?", ... ) [{'score': 0.42515, 'answer': 'us-001', 'start': 16, 'end': 16}]``` To run the example above you need to have [`pytesseract`](https://pypi.org/project/pytesseract/) installed in addition to 🤗 Transformers: ``` sudo apt install -y tesseract-ocr pip install pytesseract``` ## [](#using-pipeline-on-large-models-with-accelerate)Using `pipeline` on large models with 🤗 `accelerate`: You can easily run `pipeline` on large models using 🤗 `accelerate`! First make sure you have installed `accelerate` with `pip install accelerate`. First load your model using `device_map="auto"`! We will use `facebook/opt-1.3b` for our example. ``` import torch from transformers import pipeline pipe = pipeline(model="facebook/opt-1.3b", torch_dtype=torch.bfloat16, device_map="auto") output = pipe("This is a cool example!", do_sample=True, top_p=0.95)``` You can also pass 8-bit loaded models if you install `bitsandbytes` and add the argument `load_in_8bit=True` ``` import torch from transformers import pipeline pipe = pipeline(model="facebook/opt-1.3b", device_map="auto", model_kwargs={"load_in_8bit": True}) output = pipe("This is a cool example!", do_sample=True, top_p=0.95)``` Note that you can replace the checkpoint with any of the Hugging Face model that supports large model loading such as BLOOM!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Pipelines for inference"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/pipeline_tutorial"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Pipelines for inference</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;pipelines-for-inference&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;pipeline-usage&quot;,&quot;title&quot;:&quot;Pipeline usage&quot;},{&quot;local&quot;:&quot;parameters&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;device&quot;,&quot;title&quot;:&quot;Device&quot;},{&quot;local&quot;:&quot;batch-size&quot;,&quot;title&quot;:&quot;Batch size&quot;},{&quot;local&quot;:&quot;task-specific-parameters&quot;,&quot;title&quot;:&quot;Task specific parameters&quot;}],&quot;title&quot;:&quot;Parameters&quot;},{&quot;local&quot;:&quot;using-pipelines-on-a-dataset&quot;,&quot;title&quot;:&quot;Using pipelines on a dataset&quot;},{&quot;local&quot;:&quot;using-pipelines-for-a-webserver&quot;,&quot;title&quot;:&quot;Using pipelines for a webserver&quot;},{&quot;local&quot;:&quot;vision-pipeline&quot;,&quot;title&quot;:&quot;Vision pipeline&quot;},{&quot;local&quot;:&quot;text-pipeline&quot;,&quot;title&quot;:&quot;Text pipeline&quot;},{&quot;local&quot;:&quot;multimodal-pipeline&quot;,&quot;title&quot;:&quot;Multimodal pipeline&quot;},{&quot;local&quot;:&quot;using-pipeline-on-large-models-with-accelerate&quot;,&quot;title&quot;:&quot;Using `pipeline` on large models with 🤗 `accelerate`:&quot;}],&quot;title&quot;:&quot;Pipelines for inference&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;pipeline_tutorial&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Pipelines for inference&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Pipelines for inference</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="pipelines-for-inference" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pipelines-for-inference"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Pipelines for inference</span></h1> <p>The <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> makes it simple to use any model from the <a href="https://huggingface.co/models" rel="nofollow">Hub</a> for inference on any language, computer vision, speech, and multimodal tasks. Even if you don’t have experience with a specific modality or aren’t familiar with the underlying code behind the models, you can still use them for inference with the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a>! This tutorial will teach you to:</p> <ul><li>Use a <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> for inference.</li> <li>Use a specific tokenizer or model.</li> <li>Use a <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> for audio, vision, and multimodal tasks.</li></ul> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>Take a look at the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> documentation for a complete list of supported tasks and available parameters.</p></div> <h2 class="relative group"><a id="pipeline-usage" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pipeline-usage"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Pipeline usage</span></h2> <p>While each task has an associated <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a>, it is simpler to use the general <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> abstraction which contains all the task-specific pipelines. The <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> automatically loads a default model and a preprocessing class capable of inference for your task.</p> <ol><li>Start by creating a <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> and specify an inference task:</li></ol> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline <span class="hljs-meta">&gt;&gt;&gt; </span>generator = pipeline(task=<span class="hljs-string">"automatic-speech-recognition"</span>)</pre></div> <ol start="2"><li>Pass your input text to the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a>:</li></ol> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>generator(<span class="hljs-string">"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac"</span>) {<span class="hljs-string">'text'</span>: <span class="hljs-string">'I HAVE A DREAM BUT ONE DAY THIS NATION WILL RISE UP LIVE UP THE TRUE MEANING OF ITS TREES'</span>}</pre></div> <p>Not the result you had in mind? Check out some of the <a href="https://huggingface.co/models?pipeline_tag=automatic-speech-recognition&amp;sort=downloads" rel="nofollow">most downloaded automatic speech recognition models</a> on the Hub to see if you can get a better transcription. Let’s try <a href="https://huggingface.co/openai/whisper-large" rel="nofollow">openai/whisper-large</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>generator = pipeline(model=<span class="hljs-string">"openai/whisper-large"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>generator(<span class="hljs-string">"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac"</span>) {<span class="hljs-string">'text'</span>: <span class="hljs-string">' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'</span>}</pre></div> <p>Now this result looks more accurate! We really encourage you to check out the Hub for models in different languages, models specialized in your field, and more. You can check out and compare model results directly from your browser on the Hub to see if it fits or handles corner cases better than other ones. And if you don’t find a model for your use case, you can always start <a href="training">training</a> your own!</p> <p>If you have several inputs, you can pass your input as a list:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>generator( [ <span class="hljs-string">"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac"</span>, <span class="hljs-string">"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/1.flac"</span>, ] )</pre></div> <p>If you want to iterate over a whole dataset, or want to use it for inference in a webserver, check out dedicated parts</p> <p><a href="#using-pipelines-on-a-dataset">Using pipelines on a dataset</a></p> <p><a href="./pipeline_webserver">Using pipelines for a webserver</a></p> <h2 class="relative group"><a id="parameters" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#parameters"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Parameters</span></h2> <p><a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> supports many parameters; some are task specific, and some are general to all pipelines. In general you can specify parameters anywhere you want:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>generator = pipeline(model=<span class="hljs-string">"openai/whisper-large"</span>, my_parameter=<span class="hljs-number">1</span>) out = generator(...) <span class="hljs-comment"># This will use `my_parameter=1`.</span> out = generator(..., my_parameter=<span class="hljs-number">2</span>) <span class="hljs-comment"># This will override and use `my_parameter=2`.</span> out = generator(...) <span class="hljs-comment"># This will go back to using `my_parameter=1`.</span></pre></div> <p>Let’s check out 3 important ones:</p> <h3 class="relative group"><a id="device" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#device"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Device</span></h3> <p>If you use <code>device=n</code>, the pipeline automatically puts the model on the specified device. This will work regardless of whether you are using PyTorch or Tensorflow.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>generator = pipeline(model=<span class="hljs-string">"openai/whisper-large"</span>, device=<span class="hljs-number">0</span>)</pre></div> <p>If the model is too large for a single GPU, you can set <code>device_map="auto"</code> to allow 🤗 <a href="https://huggingface.co/docs/accelerate" rel="nofollow">Accelerate</a> to automatically determine how to load and store the model weights.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment">#!pip install accelerate</span> generator = pipeline(model=<span class="hljs-string">"openai/whisper-large"</span>, device_map=<span class="hljs-string">"auto"</span>)</pre></div> <p>Note that if <code>device_map="auto"</code> is passed, there is no need to add the argument <code>device=device</code> when instantiating your <code>pipeline</code> as you may encounter some unexpected behavior!</p> <h3 class="relative group"><a id="batch-size" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#batch-size"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Batch size</span></h3> <p>By default, pipelines will not batch inference for reasons explained in detail <a href="https://huggingface.co/docs/transformers/main_classes/pipelines#pipeline-batching" rel="nofollow">here</a>. The reason is that batching is not necessarily faster, and can actually be quite slower in some cases.</p> <p>But if it works in your use case, you can use:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>generator = pipeline(model=<span class="hljs-string">"openai/whisper-large"</span>, device=<span class="hljs-number">0</span>, batch_size=<span class="hljs-number">2</span>) audio_filenames = [<span class="hljs-string">f"audio_<span class="hljs-subst">{i}</span>.flac"</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">10</span>)] texts = generator(audio_filenames)</pre></div> <p>This runs the pipeline on the 10 provided audio files, but it will pass them in batches of 2 to the model (which is on a GPU, where batching is more likely to help) without requiring any further code from you. The output should always match what you would have received without batching. It is only meant as a way to help you get more speed out of a pipeline.</p> <p>Pipelines can also alleviate some of the complexities of batching because, for some pipelines, a single item (like a long audio file) needs to be chunked into multiple parts to be processed by a model. The pipeline performs this <a href="./main_classes/pipelines#pipeline-chunk-batching"><em>chunk batching</em></a> for you.</p> <h3 class="relative group"><a id="task-specific-parameters" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#task-specific-parameters"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Task specific parameters</span></h3> <p>All tasks provide task specific parameters which allow for additional flexibility and options to help you get your job done. For instance, the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.AutomaticSpeechRecognitionPipeline.__call__">transformers.AutomaticSpeechRecognitionPipeline.<strong>call</strong>()</a> method has a <code>return_timestamps</code> parameter which sounds promising for subtitling videos:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Not using whisper, as it cannot provide timestamps.</span> <span class="hljs-meta">&gt;&gt;&gt; </span>generator = pipeline(model=<span class="hljs-string">"facebook/wav2vec2-large-960h-lv60-self"</span>, return_timestamps=<span class="hljs-string">"word"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>generator(<span class="hljs-string">"https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac"</span>) {<span class="hljs-string">'text'</span>: <span class="hljs-string">'I HAVE A DREAM BUT ONE DAY THIS NATION WILL RISE UP AND LIVE OUT THE TRUE MEANING OF ITS CREED'</span>, <span class="hljs-string">'chunks'</span>: [{<span class="hljs-string">'text'</span>: <span class="hljs-string">'I'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">1.22</span>, <span class="hljs-number">1.24</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'HAVE'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">1.42</span>, <span class="hljs-number">1.58</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'A'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">1.66</span>, <span class="hljs-number">1.68</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'DREAM'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">1.76</span>, <span class="hljs-number">2.14</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'BUT'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">3.68</span>, <span class="hljs-number">3.8</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'ONE'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">3.94</span>, <span class="hljs-number">4.06</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'DAY'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">4.16</span>, <span class="hljs-number">4.3</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'THIS'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">6.36</span>, <span class="hljs-number">6.54</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'NATION'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">6.68</span>, <span class="hljs-number">7.1</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'WILL'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">7.32</span>, <span class="hljs-number">7.56</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'RISE'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">7.8</span>, <span class="hljs-number">8.26</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'UP'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">8.38</span>, <span class="hljs-number">8.48</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'AND'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">10.08</span>, <span class="hljs-number">10.18</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'LIVE'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">10.26</span>, <span class="hljs-number">10.48</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'OUT'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">10.58</span>, <span class="hljs-number">10.7</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'THE'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">10.82</span>, <span class="hljs-number">10.9</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'TRUE'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">10.98</span>, <span class="hljs-number">11.18</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'MEANING'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">11.26</span>, <span class="hljs-number">11.58</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'OF'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">11.66</span>, <span class="hljs-number">11.7</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'ITS'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">11.76</span>, <span class="hljs-number">11.88</span>)}, {<span class="hljs-string">'text'</span>: <span class="hljs-string">'CREED'</span>, <span class="hljs-string">'timestamp'</span>: (<span class="hljs-number">12.0</span>, <span class="hljs-number">12.38</span>)}]}</pre></div> <p>As you can see, the model inferred the text and also outputted <strong>when</strong> the various words were pronounced in the sentence.</p> <p>There are many parameters available for each task, so check out each task’s API reference to see what you can tinker with! For instance, the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.AutomaticSpeechRecognitionPipeline">AutomaticSpeechRecognitionPipeline</a> has a <code>chunk_length_s</code> parameter which is helpful for working on really long audio files (for example, subtitling entire movies or hour-long videos) that a model typically cannot handle on its own.</p> <p>If you can’t find a parameter that would really help you out, feel free to <a href="https://github.com/huggingface/transformers/issues/new?assignees=&amp;labels=feature&amp;template=feature-request.yml" rel="nofollow">request it</a>!</p> <h2 class="relative group"><a id="using-pipelines-on-a-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-pipelines-on-a-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using pipelines on a dataset</span></h2> <p>The pipeline can also run inference on a large dataset. The easiest way we recommend doing this is by using an iterator:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">data</span>(): <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>): <span class="hljs-keyword">yield</span> <span class="hljs-string">f"My example <span class="hljs-subst">{i}</span>"</span> pipe = pipeline(model=<span class="hljs-string">"gpt2"</span>, device=<span class="hljs-number">0</span>) generated_characters = <span class="hljs-number">0</span> <span class="hljs-keyword">for</span> out <span class="hljs-keyword">in</span> pipe(data()): generated_characters += <span class="hljs-built_in">len</span>(out[<span class="hljs-number">0</span>][<span class="hljs-string">"generated_text"</span>])</pre></div> <p>The iterator <code>data()</code> yields each result, and the pipeline automatically recognizes the input is iterable and will start fetching the data while it continues to process it on the GPU (this uses <a href="https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader" rel="nofollow">DataLoader</a> under the hood). This is important because you don’t have to allocate memory for the whole dataset and you can feed the GPU as fast as possible.</p> <p>Since batching could speed things up, it may be useful to try tuning the <code>batch_size</code> parameter here.</p> <p>The simplest way to iterate over a dataset is to just load one from 🤗 <a href="https://github.com/huggingface/datasets/" rel="nofollow">Datasets</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># KeyDataset is a util that will just output the item we're interested in.</span> <span class="hljs-keyword">from</span> transformers.pipelines.pt_utils <span class="hljs-keyword">import</span> KeyDataset <span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset pipe = pipeline(model=<span class="hljs-string">"hf-internal-testing/tiny-random-wav2vec2"</span>, device=<span class="hljs-number">0</span>) dataset = load_dataset(<span class="hljs-string">"hf-internal-testing/librispeech_asr_dummy"</span>, <span class="hljs-string">"clean"</span>, split=<span class="hljs-string">"validation[:10]"</span>) <span class="hljs-keyword">for</span> out <span class="hljs-keyword">in</span> pipe(KeyDataset(dataset, <span class="hljs-string">"audio"</span>)): <span class="hljs-built_in">print</span>(out)</pre></div> <h2 class="relative group"><a id="using-pipelines-for-a-webserver" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-pipelines-for-a-webserver"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using pipelines for a webserver</span></h2> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400">Creating an inference engine is a complex topic which deserves it's own page.</div> <p><a href="./pipeline_webserver">Link</a></p> <h2 class="relative group"><a id="vision-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#vision-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Vision pipeline</span></h2> <p>Using a <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> for vision tasks is practically identical.</p> <p>Specify your task and pass your image to the classifier. The image can be a link or a local path to the image. For example, what species of cat is shown below?</p> <p><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" alt="pipeline-cat-chonk"></p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline <span class="hljs-meta">&gt;&gt;&gt; </span>vision_classifier = pipeline(model=<span class="hljs-string">"google/vit-base-patch16-224"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>preds = vision_classifier( <span class="hljs-meta">... </span> images=<span class="hljs-string">"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"</span> <span class="hljs-meta">... </span>) <span class="hljs-meta">&gt;&gt;&gt; </span>preds = [{<span class="hljs-string">"score"</span>: <span class="hljs-built_in">round</span>(pred[<span class="hljs-string">"score"</span>], <span class="hljs-number">4</span>), <span class="hljs-string">"label"</span>: pred[<span class="hljs-string">"label"</span>]} <span class="hljs-keyword">for</span> pred <span class="hljs-keyword">in</span> preds] <span class="hljs-meta">&gt;&gt;&gt; </span>preds [{<span class="hljs-string">'score'</span>: <span class="hljs-number">0.4335</span>, <span class="hljs-string">'label'</span>: <span class="hljs-string">'lynx, catamount'</span>}, {<span class="hljs-string">'score'</span>: <span class="hljs-number">0.0348</span>, <span class="hljs-string">'label'</span>: <span class="hljs-string">'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'</span>}, {<span class="hljs-string">'score'</span>: <span class="hljs-number">0.0324</span>, <span class="hljs-string">'label'</span>: <span class="hljs-string">'snow leopard, ounce, Panthera uncia'</span>}, {<span class="hljs-string">'score'</span>: <span class="hljs-number">0.0239</span>, <span class="hljs-string">'label'</span>: <span class="hljs-string">'Egyptian cat'</span>}, {<span class="hljs-string">'score'</span>: <span class="hljs-number">0.0229</span>, <span class="hljs-string">'label'</span>: <span class="hljs-string">'tiger cat'</span>}]</pre></div> <h2 class="relative group"><a id="text-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#text-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Text pipeline</span></h2> <p>Using a <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> for NLP tasks is practically identical.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># This model is a `zero-shot-classification` model.</span> <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># It will classify text, except you are free to choose any label you might imagine</span> <span class="hljs-meta">&gt;&gt;&gt; </span>classifier = pipeline(model=<span class="hljs-string">"facebook/bart-large-mnli"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>classifier( <span class="hljs-meta">... </span> <span class="hljs-string">"I have a problem with my iphone that needs to be resolved asap!!"</span>, <span class="hljs-meta">... </span> candidate_labels=[<span class="hljs-string">"urgent"</span>, <span class="hljs-string">"not urgent"</span>, <span class="hljs-string">"phone"</span>, <span class="hljs-string">"tablet"</span>, <span class="hljs-string">"computer"</span>], <span class="hljs-meta">... </span>) {<span class="hljs-string">'sequence'</span>: <span class="hljs-string">'I have a problem with my iphone that needs to be resolved asap!!'</span>, <span class="hljs-string">'labels'</span>: [<span class="hljs-string">'urgent'</span>, <span class="hljs-string">'phone'</span>, <span class="hljs-string">'computer'</span>, <span class="hljs-string">'not urgent'</span>, <span class="hljs-string">'tablet'</span>], <span class="hljs-string">'scores'</span>: [<span class="hljs-number">0.504</span>, <span class="hljs-number">0.479</span>, <span class="hljs-number">0.013</span>, <span class="hljs-number">0.003</span>, <span class="hljs-number">0.002</span>]}</pre></div> <h2 class="relative group"><a id="multimodal-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#multimodal-pipeline"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Multimodal pipeline</span></h2> <p>The <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> supports more than one modality. For example, a visual question answering (VQA) task combines text and image. Feel free to use any image link you like and a question you want to ask about the image. The image can be a URL or a local path to the image.</p> <p>For example, if you use this <a href="https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png" rel="nofollow">invoice image</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline <span class="hljs-meta">&gt;&gt;&gt; </span>vqa = pipeline(model=<span class="hljs-string">"impira/layoutlm-document-qa"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>vqa( <span class="hljs-meta">... </span> image=<span class="hljs-string">"https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png"</span>, <span class="hljs-meta">... </span> question=<span class="hljs-string">"What is the invoice number?"</span>, <span class="hljs-meta">... </span>) [{<span class="hljs-string">'score'</span>: <span class="hljs-number">0.42515</span>, <span class="hljs-string">'answer'</span>: <span class="hljs-string">'us-001'</span>, <span class="hljs-string">'start'</span>: <span class="hljs-number">16</span>, <span class="hljs-string">'end'</span>: <span class="hljs-number">16</span>}]</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>To run the example above you need to have <a href="https://pypi.org/project/pytesseract/" rel="nofollow"><code>pytesseract</code></a> installed in addition to 🤗 Transformers:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>sudo apt install -y tesseract-ocr pip install pytesseract</pre></div></div> <h2 class="relative group"><a id="using-pipeline-on-large-models-with-accelerate" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-pipeline-on-large-models-with-accelerate"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using <code>pipeline</code> on large models with 🤗 <code>accelerate</code>:</span></h2> <p>You can easily run <code>pipeline</code> on large models using 🤗 <code>accelerate</code>! First make sure you have installed <code>accelerate</code> with <code>pip install accelerate</code>.</p> <p>First load your model using <code>device_map="auto"</code>! We will use <code>facebook/opt-1.3b</code> for our example.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># pip install accelerate</span> <span class="hljs-keyword">import</span> torch <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline pipe = pipeline(model=<span class="hljs-string">"facebook/opt-1.3b"</span>, torch_dtype=torch.bfloat16, device_map=<span class="hljs-string">"auto"</span>) output = pipe(<span class="hljs-string">"This is a cool example!"</span>, do_sample=<span class="hljs-literal">True</span>, top_p=<span class="hljs-number">0.95</span>)</pre></div> <p>You can also pass 8-bit loaded models if you install <code>bitsandbytes</code> and add the argument <code>load_in_8bit=True</code></p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-comment"># pip install accelerate bitsandbytes</span> <span class="hljs-keyword">import</span> torch <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline pipe = pipeline(model=<span class="hljs-string">"facebook/opt-1.3b"</span>, device_map=<span class="hljs-string">"auto"</span>, model_kwargs={<span class="hljs-string">"load_in_8bit"</span>: <span class="hljs-literal">True</span>}) output = pipe(<span class="hljs-string">"This is a cool example!"</span>, do_sample=<span class="hljs-literal">True</span>, top_p=<span class="hljs-number">0.95</span>)</pre></div> <p>Note that you can replace the checkpoint with any of the Hugging Face model that supports large model loading such as BLOOM!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/installation" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Installation</a> <a href="/docs/transformers/autoclass_tutorial" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Write portable code with AutoClass<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Pipelines for inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipelines-for-inference&quot;,&quot;url&quot;:&quot;#pipelines-for-inference&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Pipeline usage&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline-usage&quot;,&quot;url&quot;:&quot;#pipeline-usage&quot;},{&quot;title&quot;:&quot;Parameters&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;parameters&quot;,&quot;url&quot;:&quot;#parameters&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Device&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;device&quot;,&quot;url&quot;:&quot;#device&quot;},{&quot;title&quot;:&quot;Batch size&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;batch-size&quot;,&quot;url&quot;:&quot;#batch-size&quot;},{&quot;title&quot;:&quot;Task specific parameters&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task-specific-parameters&quot;,&quot;url&quot;:&quot;#task-specific-parameters&quot;}]},{&quot;title&quot;:&quot;Using pipelines on a dataset&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;using-pipelines-on-a-dataset&quot;,&quot;url&quot;:&quot;#using-pipelines-on-a-dataset&quot;},{&quot;title&quot;:&quot;Using pipelines for a webserver&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;using-pipelines-for-a-webserver&quot;,&quot;url&quot;:&quot;#using-pipelines-for-a-webserver&quot;},{&quot;title&quot;:&quot;Vision pipeline&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;vision-pipeline&quot;,&quot;url&quot;:&quot;#vision-pipeline&quot;},{&quot;title&quot;:&quot;Text pipeline&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;text-pipeline&quot;,&quot;url&quot;:&quot;#text-pipeline&quot;},{&quot;title&quot;:&quot;Multimodal pipeline&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multimodal-pipeline&quot;,&quot;url&quot;:&quot;#multimodal-pipeline&quot;},{&quot;title&quot;:&quot;Using `pipeline` on large models with 🤗 `accelerate`:&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;using-pipeline-on-large-models-with-accelerate&quot;,&quot;url&quot;:&quot;#using-pipeline-on-large-models-with-accelerate&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#pipelines-for-inference" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-pipelines-for-inference"><wbr>Pipelines for inference</a> <a href="#pipeline-usage" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-pipeline-usage"><wbr>Pipeline usage</a> <a href="#parameters" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-parameters"><wbr>Parameters</a> <a href="#device" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-device"><wbr>Device</a> <a href="#batch-size" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-batch-size"><wbr>Batch size</a> <a href="#task-specific-parameters" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-task-specific-parameters"><wbr>Task specific parameters</a> <a href="#using-pipelines-on-a-dataset" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-pipelines-on-a-dataset"><wbr>Using pipelines on a dataset</a> <a href="#using-pipelines-for-a-webserver" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-pipelines-for-a-webserver"><wbr>Using pipelines for a webserver</a> <a href="#vision-pipeline" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-vision-pipeline"><wbr>Vision pipeline</a> <a href="#text-pipeline" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-text-pipeline"><wbr>Text pipeline</a> <a href="#multimodal-pipeline" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-multimodal-pipeline"><wbr>Multimodal pipeline</a> <a href="#using-pipeline-on-large-models-with-accelerate" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-pipeline-on-large-models-with-accelerate"><wbr>Using `pipeline` on large models with 🤗 `accelerate`:</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/pipeline_tutorial" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/pipeline_tutorial"); } </script> <iframe name="__privateStripeMetricsController3190" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fpipeline_tutorial&amp;title=Pipelines%20for%20inference&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:05.853Z
Preprocess
https://huggingface.co/docs/transformers/preprocessing
Before you can train a model on a dataset, it needs to be preprocessed into the expected model input format. Whether your data is text, images, or audio, they need to be converted and assembled into batches of tensors. 🤗 Transformers provides a set of preprocessing classes to help prepare your data for the model. In this tutorial, you’ll learn that for: - Text, use a [Tokenizer](./main_classes/tokenizer) to convert text into a sequence of tokens, create a numerical representation of the tokens, and assemble them into tensors. - Speech and audio, use a [Feature extractor](./main_classes/feature_extractor) to extract sequential features from audio waveforms and convert them into tensors. - Image inputs use a [ImageProcessor](./main_classes/image) to convert images into tensors. - Multimodal inputs, use a [Processor](./main_classes/processors) to combine a tokenizer and a feature extractor or image processor. `AutoProcessor` **always** works and automatically chooses the correct class for the model you’re using, whether you’re using a tokenizer, image processor, feature extractor or processor. Before you begin, install 🤗 Datasets so you can load some datasets to experiment with: ## [](#natural-language-processing)Natural Language Processing The main tool for preprocessing textual data is a [tokenizer](main_classes/tokenizer). A tokenizer splits text into _tokens_ according to a set of rules. The tokens are converted into numbers and then tensors, which become the model inputs. Any additional inputs required by the model are added by the tokenizer. If you plan on using a pretrained model, it’s important to use the associated pretrained tokenizer. This ensures the text is split the same way as the pretraining corpus, and uses the same corresponding tokens-to-index (usually referred to as the _vocab_) during pretraining. Get started by loading a pretrained tokenizer with the [AutoTokenizer.from\_pretrained()](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer.from_pretrained) method. This downloads the _vocab_ a model was pretrained with: ``` >>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")``` Then pass your text to the tokenizer: ``` >>> encoded_input = tokenizer("Do not meddle in the affairs of wizards, for they are subtle and quick to anger.") >>> print(encoded_input) {'input_ids': [101, 2079, 2025, 19960, 10362, 1999, 1996, 3821, 1997, 16657, 1010, 2005, 2027, 2024, 11259, 1998, 4248, 2000, 4963, 1012, 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], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}``` The tokenizer returns a dictionary with three important items: - [input\_ids](glossary#input-ids) are the indices corresponding to each token in the sentence. - [attention\_mask](glossary#attention-mask) indicates whether a token should be attended to or not. - [token\_type\_ids](glossary#token-type-ids) identifies which sequence a token belongs to when there is more than one sequence. Return your input by decoding the `input_ids`: ``` >>> tokenizer.decode(encoded_input["input_ids"]) '[CLS] Do not meddle in the affairs of wizards, for they are subtle and quick to anger. [SEP]'``` As you can see, the tokenizer added two special tokens - `CLS` and `SEP` (classifier and separator) - to the sentence. Not all models need special tokens, but if they do, the tokenizer automatically adds them for you. If there are several sentences you want to preprocess, pass them as a list to the tokenizer: ``` >>> batch_sentences = [ ... "But what about second breakfast?", ... "Don't think he knows about second breakfast, Pip.", ... "What about elevensies?", ... ] >>> encoded_inputs = tokenizer(batch_sentences) >>> print(encoded_inputs) {'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102], [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], [101, 1327, 1164, 5450, 23434, 136, 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, 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, 1], [1, 1, 1, 1, 1, 1, 1]]}``` ### [](#pad)Pad Sentences aren’t always the same length which can be an issue because tensors, the model inputs, need to have a uniform shape. Padding is a strategy for ensuring tensors are rectangular by adding a special _padding token_ to shorter sentences. Set the `padding` parameter to `True` to pad the shorter sequences in the batch to match the longest sequence: ``` >>> batch_sentences = [ ... "But what about second breakfast?", ... "Don't think he knows about second breakfast, Pip.", ... "What about elevensies?", ... ] >>> encoded_input = tokenizer(batch_sentences, padding=True) >>> print(encoded_input) {'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], '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, 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': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]}``` The first and third sentences are now padded with `0`’s because they are shorter. ### [](#truncation)Truncation On the other end of the spectrum, sometimes a sequence may be too long for a model to handle. In this case, you’ll need to truncate the sequence to a shorter length. Set the `truncation` parameter to `True` to truncate a sequence to the maximum length accepted by the model: ``` >>> batch_sentences = [ ... "But what about second breakfast?", ... "Don't think he knows about second breakfast, Pip.", ... "What about elevensies?", ... ] >>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True) >>> print(encoded_input) {'input_ids': [[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], '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, 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': [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]}``` Check out the [Padding and truncation](./pad_truncation) concept guide to learn more different padding and truncation arguments. ### [](#build-tensors)Build tensors Finally, you want the tokenizer to return the actual tensors that get fed to the model. Set the `return_tensors` parameter to either `pt` for PyTorch, or `tf` for TensorFlow: ``` >>> batch_sentences = [ ... "But what about second breakfast?", ... "Don't think he knows about second breakfast, Pip.", ... "What about elevensies?", ... ] >>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True, return_tensors="pt") >>> print(encoded_input) {'input_ids': tensor([[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 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, 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, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]])}``` ``` >>> batch_sentences = [ ... "But what about second breakfast?", ... "Don't think he knows about second breakfast, Pip.", ... "What about elevensies?", ... ] >>> encoded_input = tokenizer(batch_sentences, padding=True, truncation=True, return_tensors="tf") >>> print(encoded_input) {'input_ids': <tf.Tensor: shape=(2, 9), dtype=int32, numpy= array([[101, 1252, 1184, 1164, 1248, 6462, 136, 102, 0, 0, 0, 0, 0, 0, 0], [101, 1790, 112, 189, 1341, 1119, 3520, 1164, 1248, 6462, 117, 21902, 1643, 119, 102], [101, 1327, 1164, 5450, 23434, 136, 102, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)>, 'token_type_ids': <tf.Tensor: shape=(2, 9), dtype=int32, numpy= array([[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, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)>, 'attention_mask': <tf.Tensor: shape=(2, 9), dtype=int32, numpy= array([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)>}``` ## [](#audio)Audio For audio tasks, you’ll need a [feature extractor](main_classes/feature_extractor) to prepare your dataset for the model. The feature extractor is designed to extract features from raw audio data, and convert them into tensors. Load the [MInDS-14](https://huggingface.co/datasets/PolyAI/minds14) dataset (see the 🤗 [Datasets tutorial](https://huggingface.co/docs/datasets/load_hub.html) for more details on how to load a dataset) to see how you can use a feature extractor with audio datasets: ``` >>> from datasets import load_dataset, Audio >>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train")``` Access the first element of the `audio` column to take a look at the input. Calling the `audio` column automatically loads and resamples the audio file: ``` >>> dataset[0]["audio"] {'array': array([ 0. , 0.00024414, -0.00024414, ..., -0.00024414, 0. , 0. ], dtype=float32), 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav', 'sampling_rate': 8000}``` This returns three items: - `array` is the speech signal loaded - and potentially resampled - as a 1D array. - `path` points to the location of the audio file. - `sampling_rate` refers to how many data points in the speech signal are measured per second. For this tutorial, you’ll use the [Wav2Vec2](https://huggingface.co/facebook/wav2vec2-base) model. Take a look at the model card, and you’ll learn Wav2Vec2 is pretrained on 16kHz sampled speech audio. It is important your audio data’s sampling rate matches the sampling rate of the dataset used to pretrain the model. If your data’s sampling rate isn’t the same, then you need to resample your data. 1. Use 🤗 Datasets’ [cast\_column](https://huggingface.co/docs/datasets/v2.12.0/en/package_reference/main_classes#datasets.Dataset.cast_column) method to upsample the sampling rate to 16kHz: ``` >>> dataset = dataset.cast_column("audio", Audio(sampling_rate=16_000))``` 2. Call the `audio` column again to resample the audio file: ``` >>> dataset[0]["audio"] {'array': array([ 2.3443763e-05, 2.1729663e-04, 2.2145823e-04, ..., 3.8356509e-05, -7.3497440e-06, -2.1754686e-05], dtype=float32), 'path': '/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav', 'sampling_rate': 16000}``` Next, load a feature extractor to normalize and pad the input. When padding textual data, a `0` is added for shorter sequences. The same idea applies to audio data. The feature extractor adds a `0` - interpreted as silence - to `array`. Load the feature extractor with [AutoFeatureExtractor.from\_pretrained()](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoFeatureExtractor.from_pretrained): ``` >>> from transformers import AutoFeatureExtractor >>> feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base")``` Pass the audio `array` to the feature extractor. We also recommend adding the `sampling_rate` argument in the feature extractor in order to better debug any silent errors that may occur. ``` >>> audio_input = [dataset[0]["audio"]["array"]] >>> feature_extractor(audio_input, sampling_rate=16000) {'input_values': [array([ 3.8106556e-04, 2.7506407e-03, 2.8015103e-03, ..., 5.6335266e-04, 4.6588284e-06, -1.7142107e-04], dtype=float32)]}``` Just like the tokenizer, you can apply padding or truncation to handle variable sequences in a batch. Take a look at the sequence length of these two audio samples: ``` >>> dataset[0]["audio"]["array"].shape (173398,) >>> dataset[1]["audio"]["array"].shape (106496,)``` Create a function to preprocess the dataset so the audio samples are the same lengths. Specify a maximum sample length, and the feature extractor will either pad or truncate the sequences to match it: ``` >>> def preprocess_function(examples): ... audio_arrays = [x["array"] for x in examples["audio"]] ... inputs = feature_extractor( ... audio_arrays, ... sampling_rate=16000, ... padding=True, ... max_length=100000, ... truncation=True, ... ) ... return inputs``` Apply the `preprocess_function` to the the first few examples in the dataset: ``` >>> processed_dataset = preprocess_function(dataset[:5])``` The sample lengths are now the same and match the specified maximum length. You can pass your processed dataset to the model now! ``` >>> processed_dataset["input_values"][0].shape (100000,) >>> processed_dataset["input_values"][1].shape (100000,)``` ## [](#computer-vision)Computer vision For computer vision tasks, you’ll need an [image processor](main_classes/image_processor) to prepare your dataset for the model. Image preprocessing consists of several steps that convert images into the input expected by the model. These steps include but are not limited to resizing, normalizing, color channel correction, and converting images to tensors. Image preprocessing often follows some form of image augmentation. Both image preprocessing and image augmentation transform image data, but they serve different purposes: - Image augmentation alters images in a way that can help prevent overfitting and increase the robustness of the model. You can get creative in how you augment your data - adjust brightness and colors, crop, rotate, resize, zoom, etc. However, be mindful not to change the meaning of the images with your augmentations. - Image preprocessing guarantees that the images match the model’s expected input format. When fine-tuning a computer vision model, images must be preprocessed exactly as when the model was initially trained. You can use any library you like for image augmentation. For image preprocessing, use the `ImageProcessor` associated with the model. Load the [food101](https://huggingface.co/datasets/food101) dataset (see the 🤗 [Datasets tutorial](https://huggingface.co/docs/datasets/load_hub.html) for more details on how to load a dataset) to see how you can use an image processor with computer vision datasets: Use 🤗 Datasets `split` parameter to only load a small sample from the training split since the dataset is quite large! ``` >>> from datasets import load_dataset >>> dataset = load_dataset("food101", split="train[:100]")``` Next, take a look at the image with 🤗 Datasets [`Image`](https://huggingface.co/docs/datasets/package_reference/main_classes.html?highlight=image#datasets.Image) feature: ![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vision-preprocess-tutorial.png) Load the image processor with [AutoImageProcessor.from\_pretrained()](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoImageProcessor.from_pretrained): ``` >>> from transformers import AutoImageProcessor >>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")``` First, let’s add some image augmentation. You can use any library you prefer, but in this tutorial, we’ll use torchvision’s [`transforms`](https://pytorch.org/vision/stable/transforms.html) module. If you’re interested in using another data augmentation library, learn how in the [Albumentations](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_albumentations.ipynb) or [Kornia notebooks](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_kornia.ipynb). 1. Here we use [`Compose`](https://pytorch.org/vision/master/generated/torchvision.transforms.Compose.html) to chain together a couple of transforms - [`RandomResizedCrop`](https://pytorch.org/vision/main/generated/torchvision.transforms.RandomResizedCrop.html) and [`ColorJitter`](https://pytorch.org/vision/main/generated/torchvision.transforms.ColorJitter.html). Note that for resizing, we can get the image size requirements from the `image_processor`. For some models, an exact height and width are expected, for others only the `shortest_edge` is defined. ``` >>> from torchvision.transforms import RandomResizedCrop, ColorJitter, Compose >>> size = ( ... image_processor.size["shortest_edge"] ... if "shortest_edge" in image_processor.size ... else (image_processor.size["height"], image_processor.size["width"]) ... ) >>> _transforms = Compose([RandomResizedCrop(size), ColorJitter(brightness=0.5, hue=0.5)])``` 2. The model accepts [`pixel_values`](model_doc/visionencoderdecoder#transformers.VisionEncoderDecoderModel.forward.pixel_values) as its input. `ImageProcessor` can take care of normalizing the images, and generating appropriate tensors. Create a function that combines image augmentation and image preprocessing for a batch of images and generates `pixel_values`: ``` >>> def transforms(examples): ... images = [_transforms(img.convert("RGB")) for img in examples["image"]] ... examples["pixel_values"] = image_processor(images, do_resize=False, return_tensors="pt")["pixel_values"] ... return examples``` In the example above we set `do_resize=False` because we have already resized the images in the image augmentation transformation, and leveraged the `size` attribute from the appropriate `image_processor`. If you do not resize images during image augmentation, leave this parameter out. By default, `ImageProcessor` will handle the resizing. If you wish to normalize images as a part of the augmentation transformation, use the `image_processor.image_mean`, and `image_processor.image_std` values. 3. Then use 🤗 Datasets [`set_transform`](https://huggingface.co/docs/datasets/process.html#format-transform) to apply the transforms on the fly: ``` >>> dataset.set_transform(transforms)``` 4. Now when you access the image, you’ll notice the image processor has added `pixel_values`. You can pass your processed dataset to the model now! Here is what the image looks like after the transforms are applied. The image has been randomly cropped and it’s color properties are different. ``` >>> import numpy as np >>> import matplotlib.pyplot as plt >>> img = dataset[0]["pixel_values"] >>> plt.imshow(img.permute(1, 2, 0))``` ![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/preprocessed_image.png) For tasks like object detection, semantic segmentation, instance segmentation, and panoptic segmentation, `ImageProcessor` offers post processing methods. These methods convert model’s raw outputs into meaningful predictions such as bounding boxes, or segmentation maps. ### [](#pad)Pad In some cases, for instance, when fine-tuning [DETR](./model_doc/detr), the model applies scale augmentation at training time. This may cause images to be different sizes in a batch. You can use [DetrImageProcessor.pad\_and\_create\_pixel\_mask()](/docs/transformers/v4.30.0/en/model_doc/detr#transformers.DetrFeatureExtractor.pad_and_create_pixel_mask) from [DetrImageProcessor](/docs/transformers/v4.30.0/en/model_doc/detr#transformers.DetrImageProcessor) and define a custom `collate_fn` to batch images together. ``` >>> def collate_fn(batch): ... pixel_values = [item["pixel_values"] for item in batch] ... encoding = image_processor.pad_and_create_pixel_mask(pixel_values, return_tensors="pt") ... labels = [item["labels"] for item in batch] ... batch = {} ... batch["pixel_values"] = encoding["pixel_values"] ... batch["pixel_mask"] = encoding["pixel_mask"] ... batch["labels"] = labels ... return batch``` ## [](#multimodal)Multimodal For tasks involving multimodal inputs, you’ll need a [processor](main_classes/processors) to prepare your dataset for the model. A processor couples together two processing objects such as as tokenizer and feature extractor. Load the [LJ Speech](https://huggingface.co/datasets/lj_speech) dataset (see the 🤗 [Datasets tutorial](https://huggingface.co/docs/datasets/load_hub.html) for more details on how to load a dataset) to see how you can use a processor for automatic speech recognition (ASR): ``` >>> from datasets import load_dataset >>> lj_speech = load_dataset("lj_speech", split="train")``` For ASR, you’re mainly focused on `audio` and `text` so you can remove the other columns: ``` >>> lj_speech = lj_speech.map(remove_columns=["file", "id", "normalized_text"])``` Now take a look at the `audio` and `text` columns: ``` >>> lj_speech[0]["audio"] {'array': array([-7.3242188e-04, -7.6293945e-04, -6.4086914e-04, ..., 7.3242188e-04, 2.1362305e-04, 6.1035156e-05], dtype=float32), 'path': '/root/.cache/huggingface/datasets/downloads/extracted/917ece08c95cf0c4115e45294e3cd0dee724a1165b7fc11798369308a465bd26/LJSpeech-1.1/wavs/LJ001-0001.wav', 'sampling_rate': 22050} >>> lj_speech[0]["text"] 'Printing, in the only sense with which we are at present concerned, differs from most if not from all the arts and crafts represented in the Exhibition'``` Remember you should always [resample](preprocessing#audio) your audio dataset’s sampling rate to match the sampling rate of the dataset used to pretrain a model! ``` >>> lj_speech = lj_speech.cast_column("audio", Audio(sampling_rate=16_000))``` Load a processor with [AutoProcessor.from\_pretrained()](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoProcessor.from_pretrained): ``` >>> from transformers import AutoProcessor >>> processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base-960h")``` 1. Create a function to process the audio data contained in `array` to `input_values`, and tokenize `text` to `labels`. These are the inputs to the model: ``` >>> def prepare_dataset(example): ... audio = example["audio"] ... example.update(processor(audio=audio["array"], text=example["text"], sampling_rate=16000)) ... return example``` 2. Apply the `prepare_dataset` function to a sample: ``` >>> prepare_dataset(lj_speech[0])``` The processor has now added `input_values` and `labels`, and the sampling rate has also been correctly downsampled to 16kHz. You can pass your processed dataset to the model now!
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Preprocess"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/preprocessing"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Preprocess</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;preprocess&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;natural-language-processing&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;pad&quot;,&quot;title&quot;:&quot;Pad&quot;},{&quot;local&quot;:&quot;truncation&quot;,&quot;title&quot;:&quot;Truncation&quot;},{&quot;local&quot;:&quot;build-tensors&quot;,&quot;title&quot;:&quot;Build tensors&quot;}],&quot;title&quot;:&quot;Natural Language Processing&quot;},{&quot;local&quot;:&quot;audio&quot;,&quot;title&quot;:&quot;Audio&quot;},{&quot;local&quot;:&quot;computer-vision&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;pad&quot;,&quot;title&quot;:&quot;Pad&quot;}],&quot;title&quot;:&quot;Computer vision&quot;},{&quot;local&quot;:&quot;multimodal&quot;,&quot;title&quot;:&quot;Multimodal&quot;}],&quot;title&quot;:&quot;Preprocess&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;preprocessing&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Preprocess&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Preprocess</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="preprocess" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#preprocess"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Preprocess</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"> <div class="relative colab-dropdown "><button class=" " type="button"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></button> </div> <div class="relative colab-dropdown "><button class=" " type="button"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></button> </div></div> <p>Before you can train a model on a dataset, it needs to be preprocessed into the expected model input format. Whether your data is text, images, or audio, they need to be converted and assembled into batches of tensors. 🤗 Transformers provides a set of preprocessing classes to help prepare your data for the model. In this tutorial, you’ll learn that for:</p> <ul><li>Text, use a <a href="./main_classes/tokenizer">Tokenizer</a> to convert text into a sequence of tokens, create a numerical representation of the tokens, and assemble them into tensors.</li> <li>Speech and audio, use a <a href="./main_classes/feature_extractor">Feature extractor</a> to extract sequential features from audio waveforms and convert them into tensors.</li> <li>Image inputs use a <a href="./main_classes/image">ImageProcessor</a> to convert images into tensors.</li> <li>Multimodal inputs, use a <a href="./main_classes/processors">Processor</a> to combine a tokenizer and a feature extractor or image processor.</li></ul> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p><code>AutoProcessor</code> <strong>always</strong> works and automatically chooses the correct class for the model you’re using, whether you’re using a tokenizer, image processor, feature extractor or processor.</p></div> <p>Before you begin, install 🤗 Datasets so you can load some datasets to experiment with:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install datasets</pre></div> <h2 class="relative group"><a id="natural-language-processing" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#natural-language-processing"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Natural Language Processing</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/Yffk5aydLzg" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>The main tool for preprocessing textual data is a <a href="main_classes/tokenizer">tokenizer</a>. A tokenizer splits text into <em>tokens</em> according to a set of rules. The tokens are converted into numbers and then tensors, which become the model inputs. Any additional inputs required by the model are added by the tokenizer.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>If you plan on using a pretrained model, it’s important to use the associated pretrained tokenizer. This ensures the text is split the same way as the pretraining corpus, and uses the same corresponding tokens-to-index (usually referred to as the <em>vocab</em>) during pretraining.</p></div> <p>Get started by loading a pretrained tokenizer with the <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer.from_pretrained">AutoTokenizer.from_pretrained()</a> method. This downloads the <em>vocab</em> a model was pretrained with:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>)</pre></div> <p>Then pass your text to the tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>encoded_input = tokenizer(<span class="hljs-string">"Do not meddle in the affairs of wizards, for they are subtle and quick to anger."</span>) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(encoded_input) {<span class="hljs-string">'input_ids'</span>: [<span class="hljs-number">101</span>, <span class="hljs-number">2079</span>, <span class="hljs-number">2025</span>, <span class="hljs-number">19960</span>, <span class="hljs-number">10362</span>, <span class="hljs-number">1999</span>, <span class="hljs-number">1996</span>, <span class="hljs-number">3821</span>, <span class="hljs-number">1997</span>, <span class="hljs-number">16657</span>, <span class="hljs-number">1010</span>, <span class="hljs-number">2005</span>, <span class="hljs-number">2027</span>, <span class="hljs-number">2024</span>, <span class="hljs-number">11259</span>, <span class="hljs-number">1998</span>, <span class="hljs-number">4248</span>, <span class="hljs-number">2000</span>, <span class="hljs-number">4963</span>, <span class="hljs-number">1012</span>, <span class="hljs-number">102</span>], <span class="hljs-string">'token_type_ids'</span>: [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], <span class="hljs-string">'attention_mask'</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]}</pre></div> <p>The tokenizer returns a dictionary with three important items:</p> <ul><li><a href="glossary#input-ids">input_ids</a> are the indices corresponding to each token in the sentence.</li> <li><a href="glossary#attention-mask">attention_mask</a> indicates whether a token should be attended to or not.</li> <li><a href="glossary#token-type-ids">token_type_ids</a> identifies which sequence a token belongs to when there is more than one sequence.</li></ul> <p>Return your input by decoding the <code>input_ids</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.decode(encoded_input[<span class="hljs-string">"input_ids"</span>]) <span class="hljs-string">'[CLS] Do not meddle in the affairs of wizards, for they are subtle and quick to anger. [SEP]'</span></pre></div> <p>As you can see, the tokenizer added two special tokens - <code>CLS</code> and <code>SEP</code> (classifier and separator) - to the sentence. Not all models need special tokens, but if they do, the tokenizer automatically adds them for you.</p> <p>If there are several sentences you want to preprocess, pass them as a list to the tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>batch_sentences = [ <span class="hljs-meta">... </span> <span class="hljs-string">"But what about second breakfast?"</span>, <span class="hljs-meta">... </span> <span class="hljs-string">"Don't think he knows about second breakfast, Pip."</span>, <span class="hljs-meta">... </span> <span class="hljs-string">"What about elevensies?"</span>, <span class="hljs-meta">... </span>] <span class="hljs-meta">&gt;&gt;&gt; </span>encoded_inputs = tokenizer(batch_sentences) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(encoded_inputs) {<span class="hljs-string">'input_ids'</span>: [[<span class="hljs-number">101</span>, <span class="hljs-number">1252</span>, <span class="hljs-number">1184</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">1248</span>, <span class="hljs-number">6462</span>, <span class="hljs-number">136</span>, <span class="hljs-number">102</span>], [<span class="hljs-number">101</span>, <span class="hljs-number">1790</span>, <span class="hljs-number">112</span>, <span class="hljs-number">189</span>, <span class="hljs-number">1341</span>, <span class="hljs-number">1119</span>, <span class="hljs-number">3520</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">1248</span>, <span class="hljs-number">6462</span>, <span class="hljs-number">117</span>, <span class="hljs-number">21902</span>, <span class="hljs-number">1643</span>, <span class="hljs-number">119</span>, <span class="hljs-number">102</span>], [<span class="hljs-number">101</span>, <span class="hljs-number">1327</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">5450</span>, <span class="hljs-number">23434</span>, <span class="hljs-number">136</span>, <span class="hljs-number">102</span>]], <span class="hljs-string">'token_type_ids'</span>: [[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]], <span class="hljs-string">'attention_mask'</span>: [[<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]]}</pre></div> <h3 class="relative group"><a id="pad" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pad"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Pad</span></h3> <p>Sentences aren’t always the same length which can be an issue because tensors, the model inputs, need to have a uniform shape. Padding is a strategy for ensuring tensors are rectangular by adding a special <em>padding token</em> to shorter sentences.</p> <p>Set the <code>padding</code> parameter to <code>True</code> to pad the shorter sequences in the batch to match the longest sequence:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>batch_sentences = [ <span class="hljs-meta">... </span> <span class="hljs-string">"But what about second breakfast?"</span>, <span class="hljs-meta">... </span> <span class="hljs-string">"Don't think he knows about second breakfast, Pip."</span>, <span class="hljs-meta">... </span> <span class="hljs-string">"What about elevensies?"</span>, <span class="hljs-meta">... </span>] <span class="hljs-meta">&gt;&gt;&gt; </span>encoded_input = tokenizer(batch_sentences, padding=<span class="hljs-literal">True</span>) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(encoded_input) {<span class="hljs-string">'input_ids'</span>: [[<span class="hljs-number">101</span>, <span class="hljs-number">1252</span>, <span class="hljs-number">1184</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">1248</span>, <span class="hljs-number">6462</span>, <span class="hljs-number">136</span>, <span class="hljs-number">102</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">101</span>, <span class="hljs-number">1790</span>, <span class="hljs-number">112</span>, <span class="hljs-number">189</span>, <span class="hljs-number">1341</span>, <span class="hljs-number">1119</span>, <span class="hljs-number">3520</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">1248</span>, <span class="hljs-number">6462</span>, <span class="hljs-number">117</span>, <span class="hljs-number">21902</span>, <span class="hljs-number">1643</span>, <span class="hljs-number">119</span>, <span class="hljs-number">102</span>], [<span class="hljs-number">101</span>, <span class="hljs-number">1327</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">5450</span>, <span class="hljs-number">23434</span>, <span class="hljs-number">136</span>, <span class="hljs-number">102</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]], <span class="hljs-string">'token_type_ids'</span>: [[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]], <span class="hljs-string">'attention_mask'</span>: [[<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]]}</pre></div> <p>The first and third sentences are now padded with <code>0</code>’s because they are shorter.</p> <h3 class="relative group"><a id="truncation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#truncation"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Truncation</span></h3> <p>On the other end of the spectrum, sometimes a sequence may be too long for a model to handle. In this case, you’ll need to truncate the sequence to a shorter length.</p> <p>Set the <code>truncation</code> parameter to <code>True</code> to truncate a sequence to the maximum length accepted by the model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>batch_sentences = [ <span class="hljs-meta">... </span> <span class="hljs-string">"But what about second breakfast?"</span>, <span class="hljs-meta">... </span> <span class="hljs-string">"Don't think he knows about second breakfast, Pip."</span>, <span class="hljs-meta">... </span> <span class="hljs-string">"What about elevensies?"</span>, <span class="hljs-meta">... </span>] <span class="hljs-meta">&gt;&gt;&gt; </span>encoded_input = tokenizer(batch_sentences, padding=<span class="hljs-literal">True</span>, truncation=<span class="hljs-literal">True</span>) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(encoded_input) {<span class="hljs-string">'input_ids'</span>: [[<span class="hljs-number">101</span>, <span class="hljs-number">1252</span>, <span class="hljs-number">1184</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">1248</span>, <span class="hljs-number">6462</span>, <span class="hljs-number">136</span>, <span class="hljs-number">102</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">101</span>, <span class="hljs-number">1790</span>, <span class="hljs-number">112</span>, <span class="hljs-number">189</span>, <span class="hljs-number">1341</span>, <span class="hljs-number">1119</span>, <span class="hljs-number">3520</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">1248</span>, <span class="hljs-number">6462</span>, <span class="hljs-number">117</span>, <span class="hljs-number">21902</span>, <span class="hljs-number">1643</span>, <span class="hljs-number">119</span>, <span class="hljs-number">102</span>], [<span class="hljs-number">101</span>, <span class="hljs-number">1327</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">5450</span>, <span class="hljs-number">23434</span>, <span class="hljs-number">136</span>, <span class="hljs-number">102</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]], <span class="hljs-string">'token_type_ids'</span>: [[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]], <span class="hljs-string">'attention_mask'</span>: [[<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]]}</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>Check out the <a href="./pad_truncation">Padding and truncation</a> concept guide to learn more different padding and truncation arguments.</p></div> <h3 class="relative group"><a id="build-tensors" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#build-tensors"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Build tensors</span></h3> <p>Finally, you want the tokenizer to return the actual tensors that get fed to the model.</p> <p>Set the <code>return_tensors</code> parameter to either <code>pt</code> for PyTorch, or <code>tf</code> for TensorFlow:</p> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>batch_sentences = [ <span class="hljs-meta">... </span> <span class="hljs-string">"But what about second breakfast?"</span>, <span class="hljs-meta">... </span> <span class="hljs-string">"Don't think he knows about second breakfast, Pip."</span>, <span class="hljs-meta">... </span> <span class="hljs-string">"What about elevensies?"</span>, <span class="hljs-meta">... </span>] <span class="hljs-meta">&gt;&gt;&gt; </span>encoded_input = tokenizer(batch_sentences, padding=<span class="hljs-literal">True</span>, truncation=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(encoded_input) {<span class="hljs-string">'input_ids'</span>: tensor([[<span class="hljs-number">101</span>, <span class="hljs-number">1252</span>, <span class="hljs-number">1184</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">1248</span>, <span class="hljs-number">6462</span>, <span class="hljs-number">136</span>, <span class="hljs-number">102</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">101</span>, <span class="hljs-number">1790</span>, <span class="hljs-number">112</span>, <span class="hljs-number">189</span>, <span class="hljs-number">1341</span>, <span class="hljs-number">1119</span>, <span class="hljs-number">3520</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">1248</span>, <span class="hljs-number">6462</span>, <span class="hljs-number">117</span>, <span class="hljs-number">21902</span>, <span class="hljs-number">1643</span>, <span class="hljs-number">119</span>, <span class="hljs-number">102</span>], [<span class="hljs-number">101</span>, <span class="hljs-number">1327</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">5450</span>, <span class="hljs-number">23434</span>, <span class="hljs-number">136</span>, <span class="hljs-number">102</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]]), <span class="hljs-string">'token_type_ids'</span>: tensor([[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]]), <span class="hljs-string">'attention_mask'</span>: tensor([[<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]])}</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>batch_sentences = [ <span class="hljs-meta">... </span> <span class="hljs-string">"But what about second breakfast?"</span>, <span class="hljs-meta">... </span> <span class="hljs-string">"Don't think he knows about second breakfast, Pip."</span>, <span class="hljs-meta">... </span> <span class="hljs-string">"What about elevensies?"</span>, <span class="hljs-meta">... </span>] <span class="hljs-meta">&gt;&gt;&gt; </span>encoded_input = tokenizer(batch_sentences, padding=<span class="hljs-literal">True</span>, truncation=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">"tf"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(encoded_input) {<span class="hljs-string">'input_ids'</span>: &lt;tf.Tensor: shape=(<span class="hljs-number">2</span>, <span class="hljs-number">9</span>), dtype=int32, numpy= array([[<span class="hljs-number">101</span>, <span class="hljs-number">1252</span>, <span class="hljs-number">1184</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">1248</span>, <span class="hljs-number">6462</span>, <span class="hljs-number">136</span>, <span class="hljs-number">102</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">101</span>, <span class="hljs-number">1790</span>, <span class="hljs-number">112</span>, <span class="hljs-number">189</span>, <span class="hljs-number">1341</span>, <span class="hljs-number">1119</span>, <span class="hljs-number">3520</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">1248</span>, <span class="hljs-number">6462</span>, <span class="hljs-number">117</span>, <span class="hljs-number">21902</span>, <span class="hljs-number">1643</span>, <span class="hljs-number">119</span>, <span class="hljs-number">102</span>], [<span class="hljs-number">101</span>, <span class="hljs-number">1327</span>, <span class="hljs-number">1164</span>, <span class="hljs-number">5450</span>, <span class="hljs-number">23434</span>, <span class="hljs-number">136</span>, <span class="hljs-number">102</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]], dtype=int32)&gt;, <span class="hljs-string">'token_type_ids'</span>: &lt;tf.Tensor: shape=(<span class="hljs-number">2</span>, <span class="hljs-number">9</span>), dtype=int32, numpy= array([[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]], dtype=int32)&gt;, <span class="hljs-string">'attention_mask'</span>: &lt;tf.Tensor: shape=(<span class="hljs-number">2</span>, <span class="hljs-number">9</span>), dtype=int32, numpy= array([[<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>], [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]], dtype=int32)&gt;}</pre></div></div></div> </div> <h2 class="relative group"><a id="audio" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#audio"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Audio</span></h2> <p>For audio tasks, you’ll need a <a href="main_classes/feature_extractor">feature extractor</a> to prepare your dataset for the model. The feature extractor is designed to extract features from raw audio data, and convert them into tensors.</p> <p>Load the <a href="https://huggingface.co/datasets/PolyAI/minds14" rel="nofollow">MInDS-14</a> dataset (see the 🤗 <a href="https://huggingface.co/docs/datasets/load_hub.html" rel="nofollow">Datasets tutorial</a> for more details on how to load a dataset) to see how you can use a feature extractor with audio datasets:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset, Audio <span class="hljs-meta">&gt;&gt;&gt; </span>dataset = load_dataset(<span class="hljs-string">"PolyAI/minds14"</span>, name=<span class="hljs-string">"en-US"</span>, split=<span class="hljs-string">"train"</span>)</pre></div> <p>Access the first element of the <code>audio</code> column to take a look at the input. Calling the <code>audio</code> column automatically loads and resamples the audio file:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>dataset[<span class="hljs-number">0</span>][<span class="hljs-string">"audio"</span>] {<span class="hljs-string">'array'</span>: array([ <span class="hljs-number">0.</span> , <span class="hljs-number">0.00024414</span>, -<span class="hljs-number">0.00024414</span>, ..., -<span class="hljs-number">0.00024414</span>, <span class="hljs-number">0.</span> , <span class="hljs-number">0.</span> ], dtype=float32), <span class="hljs-string">'path'</span>: <span class="hljs-string">'/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav'</span>, <span class="hljs-string">'sampling_rate'</span>: <span class="hljs-number">8000</span>}</pre></div> <p>This returns three items:</p> <ul><li><code>array</code> is the speech signal loaded - and potentially resampled - as a 1D array.</li> <li><code>path</code> points to the location of the audio file.</li> <li><code>sampling_rate</code> refers to how many data points in the speech signal are measured per second.</li></ul> <p>For this tutorial, you’ll use the <a href="https://huggingface.co/facebook/wav2vec2-base" rel="nofollow">Wav2Vec2</a> model. Take a look at the model card, and you’ll learn Wav2Vec2 is pretrained on 16kHz sampled speech audio. It is important your audio data’s sampling rate matches the sampling rate of the dataset used to pretrain the model. If your data’s sampling rate isn’t the same, then you need to resample your data.</p> <ol><li>Use 🤗 Datasets’ <a href="https://huggingface.co/docs/datasets/v2.12.0/en/package_reference/main_classes#datasets.Dataset.cast_column" rel="nofollow">cast_column</a> method to upsample the sampling rate to 16kHz:</li></ol> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>dataset = dataset.cast_column(<span class="hljs-string">"audio"</span>, Audio(sampling_rate=<span class="hljs-number">16_000</span>))</pre></div> <ol start="2"><li>Call the <code>audio</code> column again to resample the audio file:</li></ol> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>dataset[<span class="hljs-number">0</span>][<span class="hljs-string">"audio"</span>] {<span class="hljs-string">'array'</span>: array([ <span class="hljs-number">2.3443763e-05</span>, <span class="hljs-number">2.1729663e-04</span>, <span class="hljs-number">2.2145823e-04</span>, ..., <span class="hljs-number">3.8356509e-05</span>, -<span class="hljs-number">7.3497440e-06</span>, -<span class="hljs-number">2.1754686e-05</span>], dtype=float32), <span class="hljs-string">'path'</span>: <span class="hljs-string">'/root/.cache/huggingface/datasets/downloads/extracted/f14948e0e84be638dd7943ac36518a4cf3324e8b7aa331c5ab11541518e9368c/en-US~JOINT_ACCOUNT/602ba55abb1e6d0fbce92065.wav'</span>, <span class="hljs-string">'sampling_rate'</span>: <span class="hljs-number">16000</span>}</pre></div> <p>Next, load a feature extractor to normalize and pad the input. When padding textual data, a <code>0</code> is added for shorter sequences. The same idea applies to audio data. The feature extractor adds a <code>0</code> - interpreted as silence - to <code>array</code>.</p> <p>Load the feature extractor with <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoFeatureExtractor.from_pretrained">AutoFeatureExtractor.from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoFeatureExtractor <span class="hljs-meta">&gt;&gt;&gt; </span>feature_extractor = AutoFeatureExtractor.from_pretrained(<span class="hljs-string">"facebook/wav2vec2-base"</span>)</pre></div> <p>Pass the audio <code>array</code> to the feature extractor. We also recommend adding the <code>sampling_rate</code> argument in the feature extractor in order to better debug any silent errors that may occur.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>audio_input = [dataset[<span class="hljs-number">0</span>][<span class="hljs-string">"audio"</span>][<span class="hljs-string">"array"</span>]] <span class="hljs-meta">&gt;&gt;&gt; </span>feature_extractor(audio_input, sampling_rate=<span class="hljs-number">16000</span>) {<span class="hljs-string">'input_values'</span>: [array([ <span class="hljs-number">3.8106556e-04</span>, <span class="hljs-number">2.7506407e-03</span>, <span class="hljs-number">2.8015103e-03</span>, ..., <span class="hljs-number">5.6335266e-04</span>, <span class="hljs-number">4.6588284e-06</span>, -<span class="hljs-number">1.7142107e-04</span>], dtype=float32)]}</pre></div> <p>Just like the tokenizer, you can apply padding or truncation to handle variable sequences in a batch. Take a look at the sequence length of these two audio samples:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>dataset[<span class="hljs-number">0</span>][<span class="hljs-string">"audio"</span>][<span class="hljs-string">"array"</span>].shape (<span class="hljs-number">173398</span>,) <span class="hljs-meta">&gt;&gt;&gt; </span>dataset[<span class="hljs-number">1</span>][<span class="hljs-string">"audio"</span>][<span class="hljs-string">"array"</span>].shape (<span class="hljs-number">106496</span>,)</pre></div> <p>Create a function to preprocess the dataset so the audio samples are the same lengths. Specify a maximum sample length, and the feature extractor will either pad or truncate the sequences to match it:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_function</span>(<span class="hljs-params">examples</span>): <span class="hljs-meta">... </span> audio_arrays = [x[<span class="hljs-string">"array"</span>] <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> examples[<span class="hljs-string">"audio"</span>]] <span class="hljs-meta">... </span> inputs = feature_extractor( <span class="hljs-meta">... </span> audio_arrays, <span class="hljs-meta">... </span> sampling_rate=<span class="hljs-number">16000</span>, <span class="hljs-meta">... </span> padding=<span class="hljs-literal">True</span>, <span class="hljs-meta">... </span> max_length=<span class="hljs-number">100000</span>, <span class="hljs-meta">... </span> truncation=<span class="hljs-literal">True</span>, <span class="hljs-meta">... </span> ) <span class="hljs-meta">... </span> <span class="hljs-keyword">return</span> inputs</pre></div> <p>Apply the <code>preprocess_function</code> to the the first few examples in the dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>processed_dataset = preprocess_function(dataset[:<span class="hljs-number">5</span>])</pre></div> <p>The sample lengths are now the same and match the specified maximum length. You can pass your processed dataset to the model now!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>processed_dataset[<span class="hljs-string">"input_values"</span>][<span class="hljs-number">0</span>].shape (<span class="hljs-number">100000</span>,) <span class="hljs-meta">&gt;&gt;&gt; </span>processed_dataset[<span class="hljs-string">"input_values"</span>][<span class="hljs-number">1</span>].shape (<span class="hljs-number">100000</span>,)</pre></div> <h2 class="relative group"><a id="computer-vision" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#computer-vision"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Computer vision</span></h2> <p>For computer vision tasks, you’ll need an <a href="main_classes/image_processor">image processor</a> to prepare your dataset for the model. Image preprocessing consists of several steps that convert images into the input expected by the model. These steps include but are not limited to resizing, normalizing, color channel correction, and converting images to tensors.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>Image preprocessing often follows some form of image augmentation. Both image preprocessing and image augmentation transform image data, but they serve different purposes:</p> <ul><li>Image augmentation alters images in a way that can help prevent overfitting and increase the robustness of the model. You can get creative in how you augment your data - adjust brightness and colors, crop, rotate, resize, zoom, etc. However, be mindful not to change the meaning of the images with your augmentations.</li> <li>Image preprocessing guarantees that the images match the model’s expected input format. When fine-tuning a computer vision model, images must be preprocessed exactly as when the model was initially trained.</li></ul> <p>You can use any library you like for image augmentation. For image preprocessing, use the <code>ImageProcessor</code> associated with the model.</p></div> <p>Load the <a href="https://huggingface.co/datasets/food101" rel="nofollow">food101</a> dataset (see the 🤗 <a href="https://huggingface.co/docs/datasets/load_hub.html" rel="nofollow">Datasets tutorial</a> for more details on how to load a dataset) to see how you can use an image processor with computer vision datasets:</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>Use 🤗 Datasets <code>split</code> parameter to only load a small sample from the training split since the dataset is quite large!</p></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-meta">&gt;&gt;&gt; </span>dataset = load_dataset(<span class="hljs-string">"food101"</span>, split=<span class="hljs-string">"train[:100]"</span>)</pre></div> <p>Next, take a look at the image with 🤗 Datasets <a href="https://huggingface.co/docs/datasets/package_reference/main_classes.html?highlight=image#datasets.Image" rel="nofollow"><code>Image</code></a> feature:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>dataset[<span class="hljs-number">0</span>][<span class="hljs-string">"image"</span>]</pre></div> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vision-preprocess-tutorial.png"></div> <p>Load the image processor with <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoImageProcessor.from_pretrained">AutoImageProcessor.from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoImageProcessor <span class="hljs-meta">&gt;&gt;&gt; </span>image_processor = AutoImageProcessor.from_pretrained(<span class="hljs-string">"google/vit-base-patch16-224"</span>)</pre></div> <p>First, let’s add some image augmentation. You can use any library you prefer, but in this tutorial, we’ll use torchvision’s <a href="https://pytorch.org/vision/stable/transforms.html" rel="nofollow"><code>transforms</code></a> module. If you’re interested in using another data augmentation library, learn how in the <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_albumentations.ipynb" rel="nofollow">Albumentations</a> or <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_kornia.ipynb" rel="nofollow">Kornia notebooks</a>.</p> <ol><li>Here we use <a href="https://pytorch.org/vision/master/generated/torchvision.transforms.Compose.html" rel="nofollow"><code>Compose</code></a> to chain together a couple of transforms - <a href="https://pytorch.org/vision/main/generated/torchvision.transforms.RandomResizedCrop.html" rel="nofollow"><code>RandomResizedCrop</code></a> and <a href="https://pytorch.org/vision/main/generated/torchvision.transforms.ColorJitter.html" rel="nofollow"><code>ColorJitter</code></a>. Note that for resizing, we can get the image size requirements from the <code>image_processor</code>. For some models, an exact height and width are expected, for others only the <code>shortest_edge</code> is defined.</li></ol> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> torchvision.transforms <span class="hljs-keyword">import</span> RandomResizedCrop, ColorJitter, Compose <span class="hljs-meta">&gt;&gt;&gt; </span>size = ( <span class="hljs-meta">... </span> image_processor.size[<span class="hljs-string">"shortest_edge"</span>] <span class="hljs-meta">... </span> <span class="hljs-keyword">if</span> <span class="hljs-string">"shortest_edge"</span> <span class="hljs-keyword">in</span> image_processor.size <span class="hljs-meta">... </span> <span class="hljs-keyword">else</span> (image_processor.size[<span class="hljs-string">"height"</span>], image_processor.size[<span class="hljs-string">"width"</span>]) <span class="hljs-meta">... </span>) <span class="hljs-meta">&gt;&gt;&gt; </span>_transforms = Compose([RandomResizedCrop(size), ColorJitter(brightness=<span class="hljs-number">0.5</span>, hue=<span class="hljs-number">0.5</span>)])</pre></div> <ol start="2"><li>The model accepts <a href="model_doc/visionencoderdecoder#transformers.VisionEncoderDecoderModel.forward.pixel_values"><code>pixel_values</code></a> as its input. <code>ImageProcessor</code> can take care of normalizing the images, and generating appropriate tensors. Create a function that combines image augmentation and image preprocessing for a batch of images and generates <code>pixel_values</code>:</li></ol> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">transforms</span>(<span class="hljs-params">examples</span>): <span class="hljs-meta">... </span> images = [_transforms(img.convert(<span class="hljs-string">"RGB"</span>)) <span class="hljs-keyword">for</span> img <span class="hljs-keyword">in</span> examples[<span class="hljs-string">"image"</span>]] <span class="hljs-meta">... </span> examples[<span class="hljs-string">"pixel_values"</span>] = image_processor(images, do_resize=<span class="hljs-literal">False</span>, return_tensors=<span class="hljs-string">"pt"</span>)[<span class="hljs-string">"pixel_values"</span>] <span class="hljs-meta">... </span> <span class="hljs-keyword">return</span> examples</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>In the example above we set <code>do_resize=False</code> because we have already resized the images in the image augmentation transformation, and leveraged the <code>size</code> attribute from the appropriate <code>image_processor</code>. If you do not resize images during image augmentation, leave this parameter out. By default, <code>ImageProcessor</code> will handle the resizing.</p> <p>If you wish to normalize images as a part of the augmentation transformation, use the <code>image_processor.image_mean</code>, and <code>image_processor.image_std</code> values.</p></div> <ol start="3"><li>Then use 🤗 Datasets <a href="https://huggingface.co/docs/datasets/process.html#format-transform" rel="nofollow"><code>set_transform</code></a> to apply the transforms on the fly:</li></ol> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>dataset.set_transform(transforms)</pre></div> <ol start="4"><li>Now when you access the image, you’ll notice the image processor has added <code>pixel_values</code>. You can pass your processed dataset to the model now!</li></ol> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>dataset[<span class="hljs-number">0</span>].keys()</pre></div> <p>Here is what the image looks like after the transforms are applied. The image has been randomly cropped and it’s color properties are different.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt <span class="hljs-meta">&gt;&gt;&gt; </span>img = dataset[<span class="hljs-number">0</span>][<span class="hljs-string">"pixel_values"</span>] <span class="hljs-meta">&gt;&gt;&gt; </span>plt.imshow(img.permute(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>))</pre></div> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/preprocessed_image.png"></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>For tasks like object detection, semantic segmentation, instance segmentation, and panoptic segmentation, <code>ImageProcessor</code> offers post processing methods. These methods convert model’s raw outputs into meaningful predictions such as bounding boxes, or segmentation maps.</p></div> <h3 class="relative group"><a id="pad" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pad"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Pad</span></h3> <p>In some cases, for instance, when fine-tuning <a href="./model_doc/detr">DETR</a>, the model applies scale augmentation at training time. This may cause images to be different sizes in a batch. You can use <a href="/docs/transformers/v4.30.0/en/model_doc/detr#transformers.DetrFeatureExtractor.pad_and_create_pixel_mask">DetrImageProcessor.pad_and_create_pixel_mask()</a> from <a href="/docs/transformers/v4.30.0/en/model_doc/detr#transformers.DetrImageProcessor">DetrImageProcessor</a> and define a custom <code>collate_fn</code> to batch images together.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">collate_fn</span>(<span class="hljs-params">batch</span>): <span class="hljs-meta">... </span> pixel_values = [item[<span class="hljs-string">"pixel_values"</span>] <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> batch] <span class="hljs-meta">... </span> encoding = image_processor.pad_and_create_pixel_mask(pixel_values, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">... </span> labels = [item[<span class="hljs-string">"labels"</span>] <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> batch] <span class="hljs-meta">... </span> batch = {} <span class="hljs-meta">... </span> batch[<span class="hljs-string">"pixel_values"</span>] = encoding[<span class="hljs-string">"pixel_values"</span>] <span class="hljs-meta">... </span> batch[<span class="hljs-string">"pixel_mask"</span>] = encoding[<span class="hljs-string">"pixel_mask"</span>] <span class="hljs-meta">... </span> batch[<span class="hljs-string">"labels"</span>] = labels <span class="hljs-meta">... </span> <span class="hljs-keyword">return</span> batch</pre></div> <h2 class="relative group"><a id="multimodal" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#multimodal"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Multimodal</span></h2> <p>For tasks involving multimodal inputs, you’ll need a <a href="main_classes/processors">processor</a> to prepare your dataset for the model. A processor couples together two processing objects such as as tokenizer and feature extractor.</p> <p>Load the <a href="https://huggingface.co/datasets/lj_speech" rel="nofollow">LJ Speech</a> dataset (see the 🤗 <a href="https://huggingface.co/docs/datasets/load_hub.html" rel="nofollow">Datasets tutorial</a> for more details on how to load a dataset) to see how you can use a processor for automatic speech recognition (ASR):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-meta">&gt;&gt;&gt; </span>lj_speech = load_dataset(<span class="hljs-string">"lj_speech"</span>, split=<span class="hljs-string">"train"</span>)</pre></div> <p>For ASR, you’re mainly focused on <code>audio</code> and <code>text</code> so you can remove the other columns:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>lj_speech = lj_speech.<span class="hljs-built_in">map</span>(remove_columns=[<span class="hljs-string">"file"</span>, <span class="hljs-string">"id"</span>, <span class="hljs-string">"normalized_text"</span>])</pre></div> <p>Now take a look at the <code>audio</code> and <code>text</code> columns:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>lj_speech[<span class="hljs-number">0</span>][<span class="hljs-string">"audio"</span>] {<span class="hljs-string">'array'</span>: array([-<span class="hljs-number">7.3242188e-04</span>, -<span class="hljs-number">7.6293945e-04</span>, -<span class="hljs-number">6.4086914e-04</span>, ..., <span class="hljs-number">7.3242188e-04</span>, <span class="hljs-number">2.1362305e-04</span>, <span class="hljs-number">6.1035156e-05</span>], dtype=float32), <span class="hljs-string">'path'</span>: <span class="hljs-string">'/root/.cache/huggingface/datasets/downloads/extracted/917ece08c95cf0c4115e45294e3cd0dee724a1165b7fc11798369308a465bd26/LJSpeech-1.1/wavs/LJ001-0001.wav'</span>, <span class="hljs-string">'sampling_rate'</span>: <span class="hljs-number">22050</span>} <span class="hljs-meta">&gt;&gt;&gt; </span>lj_speech[<span class="hljs-number">0</span>][<span class="hljs-string">"text"</span>] <span class="hljs-string">'Printing, in the only sense with which we are at present concerned, differs from most if not from all the arts and crafts represented in the Exhibition'</span></pre></div> <p>Remember you should always <a href="preprocessing#audio">resample</a> your audio dataset’s sampling rate to match the sampling rate of the dataset used to pretrain a model!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>lj_speech = lj_speech.cast_column(<span class="hljs-string">"audio"</span>, Audio(sampling_rate=<span class="hljs-number">16_000</span>))</pre></div> <p>Load a processor with <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoProcessor.from_pretrained">AutoProcessor.from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoProcessor <span class="hljs-meta">&gt;&gt;&gt; </span>processor = AutoProcessor.from_pretrained(<span class="hljs-string">"facebook/wav2vec2-base-960h"</span>)</pre></div> <ol><li>Create a function to process the audio data contained in <code>array</code> to <code>input_values</code>, and tokenize <code>text</code> to <code>labels</code>. These are the inputs to the model:</li></ol> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">prepare_dataset</span>(<span class="hljs-params">example</span>): <span class="hljs-meta">... </span> audio = example[<span class="hljs-string">"audio"</span>] <span class="hljs-meta">... </span> example.update(processor(audio=audio[<span class="hljs-string">"array"</span>], text=example[<span class="hljs-string">"text"</span>], sampling_rate=<span class="hljs-number">16000</span>)) <span class="hljs-meta">... </span> <span class="hljs-keyword">return</span> example</pre></div> <ol start="2"><li>Apply the <code>prepare_dataset</code> function to a sample:</li></ol> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>prepare_dataset(lj_speech[<span class="hljs-number">0</span>])</pre></div> <p>The processor has now added <code>input_values</code> and <code>labels</code>, and the sampling rate has also been correctly downsampled to 16kHz. You can pass your processed dataset to the model now!</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/autoclass_tutorial" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Write portable code with AutoClass</a> <a href="/docs/transformers/training" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Fine-tune a pretrained model<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Preprocess&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocess&quot;,&quot;url&quot;:&quot;#preprocess&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;natural-language-processing&quot;,&quot;url&quot;:&quot;#natural-language-processing&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Pad&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad&quot;,&quot;url&quot;:&quot;#pad&quot;},{&quot;title&quot;:&quot;Truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;truncation&quot;,&quot;url&quot;:&quot;#truncation&quot;},{&quot;title&quot;:&quot;Build tensors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;build-tensors&quot;,&quot;url&quot;:&quot;#build-tensors&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;audio&quot;,&quot;url&quot;:&quot;#audio&quot;},{&quot;title&quot;:&quot;Computer vision&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;computer-vision&quot;,&quot;url&quot;:&quot;#computer-vision&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Pad&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad&quot;,&quot;url&quot;:&quot;#pad&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multimodal&quot;,&quot;url&quot;:&quot;#multimodal&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#preprocess" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-preprocess"><wbr>Preprocess</a> <a href="#natural-language-processing" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-natural-language-processing"><wbr>Natural <wbr>Language <wbr>Processing</a> <a href="#pad" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-pad"><wbr>Pad</a> <a href="#truncation" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-truncation"><wbr>Truncation</a> <a href="#build-tensors" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-build-tensors"><wbr>Build tensors</a> <a href="#audio" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-audio"><wbr>Audio</a> <a href="#computer-vision" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-computer-vision"><wbr>Computer vision</a> <a href="#pad" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-pad"><wbr>Pad</a> <a href="#multimodal" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-multimodal"><wbr>Multimodal</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/preprocessing" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/preprocessing"); } </script> <iframe name="__privateStripeMetricsController6260" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fpreprocessing&amp;title=Preprocess&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:06.595Z
Fine-tune a pretrained model
https://huggingface.co/docs/transformers/training
## [](#train-a-tensorflow-model-with-keras)Train a TensorFlow model with Keras You can also train 🤗 Transformers models in TensorFlow with the Keras API! ### [](#loading-data-for-keras)Loading data for Keras When you want to train a 🤗 Transformers model with the Keras API, you need to convert your dataset to a format that Keras understands. If your dataset is small, you can just convert the whole thing to NumPy arrays and pass it to Keras. Let’s try that first before we do anything more complicated. First, load a dataset. We’ll use the CoLA dataset from the [GLUE benchmark](https://huggingface.co/datasets/glue), since it’s a simple binary text classification task, and just take the training split for now. ``` from datasets import load_dataset dataset = load_dataset("glue", "cola") dataset = dataset["train"] ``` Next, load a tokenizer and tokenize the data as NumPy arrays. Note that the labels are already a list of 0 and 1s, so we can just convert that directly to a NumPy array without tokenization! ``` from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-cased") tokenized_data = tokenizer(dataset["sentence"], return_tensors="np", padding=True) tokenized_data = dict(tokenized_data) labels = np.array(dataset["label"]) ``` Finally, load, [`compile`](https://keras.io/api/models/model_training_apis/#compile-method), and [`fit`](https://keras.io/api/models/model_training_apis/#fit-method) the model. Note that Transformers models all have a default task-relevant loss function, so you don’t need to specify one unless you want to: ``` from transformers import TFAutoModelForSequenceClassification from tensorflow.keras.optimizers import Adam model = TFAutoModelForSequenceClassification.from_pretrained("bert-base-cased") model.compile(optimizer=Adam(3e-5)) model.fit(tokenized_data, labels)``` You don’t have to pass a loss argument to your models when you `compile()` them! Hugging Face models automatically choose a loss that is appropriate for their task and model architecture if this argument is left blank. You can always override this by specifying a loss yourself if you want to! This approach works great for smaller datasets, but for larger datasets, you might find it starts to become a problem. Why? Because the tokenized array and labels would have to be fully loaded into memory, and because NumPy doesn’t handle “jagged” arrays, so every tokenized sample would have to be padded to the length of the longest sample in the whole dataset. That’s going to make your array even bigger, and all those padding tokens will slow down training too! ### [](#loading-data-as-a-tfdatadataset)Loading data as a tf.data.Dataset If you want to avoid slowing down training, you can load your data as a `tf.data.Dataset` instead. Although you can write your own `tf.data` pipeline if you want, we have two convenience methods for doing this: - [prepare\_tf\_dataset()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset): This is the method we recommend in most cases. Because it is a method on your model, it can inspect the model to automatically figure out which columns are usable as model inputs, and discard the others to make a simpler, more performant dataset. - [to\_tf\_dataset](https://huggingface.co/docs/datasets/v2.12.0/en/package_reference/main_classes#datasets.Dataset.to_tf_dataset): This method is more low-level, and is useful when you want to exactly control how your dataset is created, by specifying exactly which `columns` and `label_cols` to include. Before you can use [prepare\_tf\_dataset()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset), you will need to add the tokenizer outputs to your dataset as columns, as shown in the following code sample: ``` def tokenize_dataset(data): return tokenizer(data["text"]) dataset = dataset.map(tokenize_dataset)``` Remember that Hugging Face datasets are stored on disk by default, so this will not inflate your memory usage! Once the columns have been added, you can stream batches from the dataset and add padding to each batch, which greatly reduces the number of padding tokens compared to padding the entire dataset. ``` >>> tf_dataset = model.prepare_tf_dataset(dataset["train"], batch_size=16, shuffle=True, tokenizer=tokenizer)``` Note that in the code sample above, you need to pass the tokenizer to `prepare_tf_dataset` so it can correctly pad batches as they’re loaded. If all the samples in your dataset are the same length and no padding is necessary, you can skip this argument. If you need to do something more complex than just padding samples (e.g. corrupting tokens for masked language modelling), you can use the `collate_fn` argument instead to pass a function that will be called to transform the list of samples into a batch and apply any preprocessing you want. See our [examples](https://github.com/huggingface/transformers/tree/main/examples) or [notebooks](https://huggingface.co/docs/transformers/notebooks) to see this approach in action. Once you’ve created a `tf.data.Dataset`, you can compile and fit the model as before: ``` model.compile(optimizer=Adam(3e-5)) model.fit(tf_dataset)```
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Fine-tune a pretrained model"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/training"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Fine-tune a pretrained model</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;finetune-a-pretrained-model&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;prepare-a-dataset&quot;,&quot;title&quot;:&quot;Prepare a dataset&quot;},{&quot;local&quot;:&quot;train&quot;,&quot;title&quot;:&quot;Train&quot;},{&quot;local&quot;:&quot;train-with-pytorch-trainer&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;training-hyperparameters&quot;,&quot;title&quot;:&quot;Training hyperparameters&quot;},{&quot;local&quot;:&quot;evaluate&quot;,&quot;title&quot;:&quot;Evaluate&quot;},{&quot;local&quot;:&quot;trainer&quot;,&quot;title&quot;:&quot;Trainer&quot;}],&quot;title&quot;:&quot;Train with PyTorch Trainer&quot;},{&quot;local&quot;:&quot;train-a-tensorflow-model-with-keras&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;loading-data-for-keras&quot;,&quot;title&quot;:&quot;Loading data for Keras&quot;},{&quot;local&quot;:&quot;loading-data-as-a-tfdatadataset&quot;,&quot;title&quot;:&quot;Loading data as a tf.data.Dataset&quot;}],&quot;title&quot;:&quot;Train a TensorFlow model with Keras&quot;},{&quot;local&quot;:&quot;train-in-native-pytorch&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;dataloader&quot;,&quot;title&quot;:&quot;DataLoader&quot;},{&quot;local&quot;:&quot;optimizer-and-learning-rate-scheduler&quot;,&quot;title&quot;:&quot;Optimizer and learning rate scheduler&quot;},{&quot;local&quot;:&quot;training-loop&quot;,&quot;title&quot;:&quot;Training loop&quot;},{&quot;local&quot;:&quot;evaluate&quot;,&quot;title&quot;:&quot;Evaluate&quot;}],&quot;title&quot;:&quot;Train in native PyTorch&quot;},{&quot;local&quot;:&quot;additional-resources&quot;,&quot;title&quot;:&quot;Additional resources&quot;}],&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;training&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Fine-tune a pretrained model</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="finetune-a-pretrained-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#finetune-a-pretrained-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Fine-tune a pretrained model</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"> <div class="relative colab-dropdown "><button class=" " type="button"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></button> </div> <div class="relative colab-dropdown "><button class=" " type="button"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></button> </div></div> <p>There are significant benefits to using a pretrained model. It reduces computation costs, your carbon footprint, and allows you to use state-of-the-art models without having to train one from scratch. 🤗 Transformers provides access to thousands of pretrained models for a wide range of tasks. When you use a pretrained model, you train it on a dataset specific to your task. This is known as fine-tuning, an incredibly powerful training technique. In this tutorial, you will fine-tune a pretrained model with a deep learning framework of your choice:</p> <ul><li>Fine-tune a pretrained model with 🤗 Transformers <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a>.</li> <li>Fine-tune a pretrained model in TensorFlow with Keras.</li> <li>Fine-tune a pretrained model in native PyTorch.</li></ul> <a id="data-processing"></a> <h2 class="relative group"><a id="prepare-a-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#prepare-a-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Prepare a dataset</span></h2> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/_BZearw7f0w" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Before you can fine-tune a pretrained model, download a dataset and prepare it for training. The previous tutorial showed you how to process data for training, and now you get an opportunity to put those skills to the test!</p> <p>Begin by loading the <a href="https://huggingface.co/datasets/yelp_review_full" rel="nofollow">Yelp Reviews</a> dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset <span class="hljs-meta">&gt;&gt;&gt; </span>dataset = load_dataset(<span class="hljs-string">"yelp_review_full"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>dataset[<span class="hljs-string">"train"</span>][<span class="hljs-number">100</span>] {<span class="hljs-string">'label'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'text'</span>: <span class="hljs-string">'My expectations for McDonalds are t rarely high. But for one to still fail so spectacularly...that takes something special!\\nThe cashier took my friends\'s order, then promptly ignored me. I had to force myself in front of a cashier who opened his register to wait on the person BEHIND me. I waited over five minutes for a gigantic order that included precisely one kid\'s meal. After watching two people who ordered after me be handed their food, I asked where mine was. The manager started yelling at the cashiers for \\"serving off their orders\\" when they didn\'t have their food. But neither cashier was anywhere near those controls, and the manager was the one serving food to customers and clearing the boards.\\nThe manager was rude when giving me my order. She didn\'t make sure that I had everything ON MY RECEIPT, and never even had the decency to apologize that I felt I was getting poor service.\\nI\'ve eaten at various McDonalds restaurants for over 30 years. I\'ve worked at more than one location. I expect bad days, bad moods, and the occasional mistake. But I have yet to have a decent experience at this store. It will remain a place I avoid unless someone in my party needs to avoid illness from low blood sugar. Perhaps I should go back to the racially biased service of Steak n Shake instead!'</span>}</pre></div> <p>As you now know, you need a tokenizer to process the text and include a padding and truncation strategy to handle any variable sequence lengths. To process your dataset in one step, use 🤗 Datasets <a href="https://huggingface.co/docs/datasets/process.html#map" rel="nofollow"><code>map</code></a> method to apply a preprocessing function over the entire dataset:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_function</span>(<span class="hljs-params">examples</span>): <span class="hljs-meta">... </span> <span class="hljs-keyword">return</span> tokenizer(examples[<span class="hljs-string">"text"</span>], padding=<span class="hljs-string">"max_length"</span>, truncation=<span class="hljs-literal">True</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenized_datasets = dataset.<span class="hljs-built_in">map</span>(tokenize_function, batched=<span class="hljs-literal">True</span>)</pre></div> <p>If you like, you can create a smaller subset of the full dataset to fine-tune on to reduce the time it takes:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>small_train_dataset = tokenized_datasets[<span class="hljs-string">"train"</span>].shuffle(seed=<span class="hljs-number">42</span>).select(<span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>)) <span class="hljs-meta">&gt;&gt;&gt; </span>small_eval_dataset = tokenized_datasets[<span class="hljs-string">"test"</span>].shuffle(seed=<span class="hljs-number">42</span>).select(<span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>))</pre></div> <a id="trainer"></a> <h2 class="relative group"><a id="train" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#train"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Train</span></h2> <p>At this point, you should follow the section corresponding to the framework you want to use. You can use the links in the right sidebar to jump to the one you want - and if you want to hide all of the content for a given framework, just use the button at the top-right of that framework’s block!</p> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/nvBXf7s7vTI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <h2 class="relative group"><a id="train-with-pytorch-trainer" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#train-with-pytorch-trainer"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Train with PyTorch Trainer</span></h2> <p>🤗 Transformers provides a <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a> class optimized for training 🤗 Transformers models, making it easier to start training without manually writing your own training loop. The <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a> API supports a wide range of training options and features such as logging, gradient accumulation, and mixed precision.</p> <p>Start by loading your model and specify the number of expected labels. From the Yelp Review <a href="https://huggingface.co/datasets/yelp_review_full#data-fields" rel="nofollow">dataset card</a>, you know there are five labels:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>, num_labels=<span class="hljs-number">5</span>)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>You will see a warning about some of the pretrained weights not being used and some weights being randomly initialized. Don’t worry, this is completely normal! The pretrained head of the BERT model is discarded, and replaced with a randomly initialized classification head. You will fine-tune this new model head on your sequence classification task, transferring the knowledge of the pretrained model to it.</p></div> <h3 class="relative group"><a id="training-hyperparameters" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-hyperparameters"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training hyperparameters</span></h3> <p>Next, create a <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a> class which contains all the hyperparameters you can tune as well as flags for activating different training options. For this tutorial you can start with the default training <a href="https://huggingface.co/docs/transformers/main_classes/trainer#transformers.TrainingArguments" rel="nofollow">hyperparameters</a>, but feel free to experiment with these to find your optimal settings.</p> <p>Specify where to save the checkpoints from your training:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments <span class="hljs-meta">&gt;&gt;&gt; </span>training_args = TrainingArguments(output_dir=<span class="hljs-string">"test_trainer"</span>)</pre></div> <h3 class="relative group"><a id="evaluate" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#evaluate"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Evaluate</span></h3> <p><a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a> does not automatically evaluate model performance during training. You’ll need to pass <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a> a function to compute and report metrics. The <a href="https://huggingface.co/docs/evaluate/index" rel="nofollow">🤗 Evaluate</a> library provides a simple <a href="https://huggingface.co/spaces/evaluate-metric/accuracy" rel="nofollow"><code>accuracy</code></a> function you can load with the <a href="https://huggingface.co/docs/evaluate/v0.4.0/en/package_reference/loading_methods#evaluate.load" rel="nofollow">evaluate.load</a> (see this <a href="https://huggingface.co/docs/evaluate/a_quick_tour" rel="nofollow">quicktour</a> for more information) function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> evaluate <span class="hljs-meta">&gt;&gt;&gt; </span>metric = evaluate.load(<span class="hljs-string">"accuracy"</span>)</pre></div> <p>Call <code>compute</code> on <code>metric</code> to calculate the accuracy of your predictions. Before passing your predictions to <code>compute</code>, you need to convert the predictions to logits (remember all 🤗 Transformers models return logits):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_pred</span>): <span class="hljs-meta">... </span> logits, labels = eval_pred <span class="hljs-meta">... </span> predictions = np.argmax(logits, axis=-<span class="hljs-number">1</span>) <span class="hljs-meta">... </span> <span class="hljs-keyword">return</span> metric.compute(predictions=predictions, references=labels)</pre></div> <p>If you’d like to monitor your evaluation metrics during fine-tuning, specify the <code>evaluation_strategy</code> parameter in your training arguments to report the evaluation metric at the end of each epoch:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments, Trainer <span class="hljs-meta">&gt;&gt;&gt; </span>training_args = TrainingArguments(output_dir=<span class="hljs-string">"test_trainer"</span>, evaluation_strategy=<span class="hljs-string">"epoch"</span>)</pre></div> <h3 class="relative group"><a id="trainer" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#trainer"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Trainer</span></h3> <p>Create a <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a> object with your model, training arguments, training and test datasets, and evaluation function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>trainer = Trainer( <span class="hljs-meta">... </span> model=model, <span class="hljs-meta">... </span> args=training_args, <span class="hljs-meta">... </span> train_dataset=small_train_dataset, <span class="hljs-meta">... </span> eval_dataset=small_eval_dataset, <span class="hljs-meta">... </span> compute_metrics=compute_metrics, <span class="hljs-meta">... </span>)</pre></div> <p>Then fine-tune your model by calling <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer.train">train()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>trainer.train()</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><a id="keras"></a> <iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/rnTGBy2ax1c" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <h2 class="relative group"><a id="train-a-tensorflow-model-with-keras" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#train-a-tensorflow-model-with-keras"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Train a TensorFlow model with Keras</span></h2> <p>You can also train 🤗 Transformers models in TensorFlow with the Keras API!</p> <h3 class="relative group"><a id="loading-data-for-keras" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-data-for-keras"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Loading data for Keras</span></h3> <p>When you want to train a 🤗 Transformers model with the Keras API, you need to convert your dataset to a format that Keras understands. If your dataset is small, you can just convert the whole thing to NumPy arrays and pass it to Keras. Let’s try that first before we do anything more complicated.</p> <p>First, load a dataset. We’ll use the CoLA dataset from the <a href="https://huggingface.co/datasets/glue" rel="nofollow">GLUE benchmark</a>, since it’s a simple binary text classification task, and just take the training split for now.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset dataset = load_dataset(<span class="hljs-string">"glue"</span>, <span class="hljs-string">"cola"</span>) dataset = dataset[<span class="hljs-string">"train"</span>] <span class="hljs-comment"># Just take the training split for now</span></pre></div> <p>Next, load a tokenizer and tokenize the data as NumPy arrays. Note that the labels are already a list of 0 and 1s, so we can just convert that directly to a NumPy array without tokenization!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>) tokenized_data = tokenizer(dataset[<span class="hljs-string">"sentence"</span>], return_tensors=<span class="hljs-string">"np"</span>, padding=<span class="hljs-literal">True</span>) <span class="hljs-comment"># Tokenizer returns a BatchEncoding, but we convert that to a dict for Keras</span> tokenized_data = <span class="hljs-built_in">dict</span>(tokenized_data) labels = np.array(dataset[<span class="hljs-string">"label"</span>]) <span class="hljs-comment"># Label is already an array of 0 and 1</span></pre></div> <p>Finally, load, <a href="https://keras.io/api/models/model_training_apis/#compile-method" rel="nofollow"><code>compile</code></a>, and <a href="https://keras.io/api/models/model_training_apis/#fit-method" rel="nofollow"><code>fit</code></a> the model. Note that Transformers models all have a default task-relevant loss function, so you don’t need to specify one unless you want to:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForSequenceClassification <span class="hljs-keyword">from</span> tensorflow.keras.optimizers <span class="hljs-keyword">import</span> Adam <span class="hljs-comment"># Load and compile our model</span> model = TFAutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>) <span class="hljs-comment"># Lower learning rates are often better for fine-tuning transformers</span> model.<span class="hljs-built_in">compile</span>(optimizer=Adam(<span class="hljs-number">3e-5</span>)) <span class="hljs-comment"># No loss argument!</span> model.fit(tokenized_data, labels)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>You don’t have to pass a loss argument to your models when you <code>compile()</code> them! Hugging Face models automatically choose a loss that is appropriate for their task and model architecture if this argument is left blank. You can always override this by specifying a loss yourself if you want to!</p></div> <p>This approach works great for smaller datasets, but for larger datasets, you might find it starts to become a problem. Why? Because the tokenized array and labels would have to be fully loaded into memory, and because NumPy doesn’t handle “jagged” arrays, so every tokenized sample would have to be padded to the length of the longest sample in the whole dataset. That’s going to make your array even bigger, and all those padding tokens will slow down training too!</p> <h3 class="relative group"><a id="loading-data-as-a-tfdatadataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-data-as-a-tfdatadataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Loading data as a tf.data.Dataset</span></h3> <p>If you want to avoid slowing down training, you can load your data as a <code>tf.data.Dataset</code> instead. Although you can write your own <code>tf.data</code> pipeline if you want, we have two convenience methods for doing this:</p> <ul><li><a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset">prepare_tf_dataset()</a>: This is the method we recommend in most cases. Because it is a method on your model, it can inspect the model to automatically figure out which columns are usable as model inputs, and discard the others to make a simpler, more performant dataset.</li> <li><a href="https://huggingface.co/docs/datasets/v2.12.0/en/package_reference/main_classes#datasets.Dataset.to_tf_dataset" rel="nofollow">to_tf_dataset</a>: This method is more low-level, and is useful when you want to exactly control how your dataset is created, by specifying exactly which <code>columns</code> and <code>label_cols</code> to include.</li></ul> <p>Before you can use <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.prepare_tf_dataset">prepare_tf_dataset()</a>, you will need to add the tokenizer outputs to your dataset as columns, as shown in the following code sample:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_dataset</span>(<span class="hljs-params">data</span>): <span class="hljs-comment"># Keys of the returned dictionary will be added to the dataset as columns</span> <span class="hljs-keyword">return</span> tokenizer(data[<span class="hljs-string">"text"</span>]) dataset = dataset.<span class="hljs-built_in">map</span>(tokenize_dataset)</pre></div> <p>Remember that Hugging Face datasets are stored on disk by default, so this will not inflate your memory usage! Once the columns have been added, you can stream batches from the dataset and add padding to each batch, which greatly reduces the number of padding tokens compared to padding the entire dataset.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tf_dataset = model.prepare_tf_dataset(dataset[<span class="hljs-string">"train"</span>], batch_size=<span class="hljs-number">16</span>, shuffle=<span class="hljs-literal">True</span>, tokenizer=tokenizer)</pre></div> <p>Note that in the code sample above, you need to pass the tokenizer to <code>prepare_tf_dataset</code> so it can correctly pad batches as they’re loaded. If all the samples in your dataset are the same length and no padding is necessary, you can skip this argument. If you need to do something more complex than just padding samples (e.g. corrupting tokens for masked language modelling), you can use the <code>collate_fn</code> argument instead to pass a function that will be called to transform the list of samples into a batch and apply any preprocessing you want. See our <a href="https://github.com/huggingface/transformers/tree/main/examples" rel="nofollow">examples</a> or <a href="https://huggingface.co/docs/transformers/notebooks" rel="nofollow">notebooks</a> to see this approach in action.</p> <p>Once you’ve created a <code>tf.data.Dataset</code>, you can compile and fit the model as before:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>model.<span class="hljs-built_in">compile</span>(optimizer=Adam(<span class="hljs-number">3e-5</span>)) <span class="hljs-comment"># No loss argument!</span> model.fit(tf_dataset)</pre></div></div></div> </div> <a id="pytorch_native"></a> <h2 class="relative group"><a id="train-in-native-pytorch" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#train-in-native-pytorch"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Train in native PyTorch</span></h2> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/Dh9CL8fyG80" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p><a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a> takes care of the training loop and allows you to fine-tune a model in a single line of code. For users who prefer to write their own training loop, you can also fine-tune a 🤗 Transformers model in native PyTorch.</p> <p>At this point, you may need to restart your notebook or execute the following code to free some memory:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">del</span> model <span class="hljs-keyword">del</span> trainer torch.cuda.empty_cache()</pre></div> <p>Next, manually postprocess <code>tokenized_dataset</code> to prepare it for training.</p> <ol><li><p>Remove the <code>text</code> column because the model does not accept raw text as an input:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tokenized_datasets = tokenized_datasets.remove_columns([<span class="hljs-string">"text"</span>])</pre></div></li> <li><p>Rename the <code>label</code> column to <code>labels</code> because the model expects the argument to be named <code>labels</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tokenized_datasets = tokenized_datasets.rename_column(<span class="hljs-string">"label"</span>, <span class="hljs-string">"labels"</span>)</pre></div></li> <li><p>Set the format of the dataset to return PyTorch tensors instead of lists:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tokenized_datasets.set_format(<span class="hljs-string">"torch"</span>)</pre></div></li></ol> <p>Then create a smaller subset of the dataset as previously shown to speed up the fine-tuning:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>small_train_dataset = tokenized_datasets[<span class="hljs-string">"train"</span>].shuffle(seed=<span class="hljs-number">42</span>).select(<span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>)) <span class="hljs-meta">&gt;&gt;&gt; </span>small_eval_dataset = tokenized_datasets[<span class="hljs-string">"test"</span>].shuffle(seed=<span class="hljs-number">42</span>).select(<span class="hljs-built_in">range</span>(<span class="hljs-number">1000</span>))</pre></div> <h3 class="relative group"><a id="dataloader" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#dataloader"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>DataLoader</span></h3> <p>Create a <code>DataLoader</code> for your training and test datasets so you can iterate over batches of data:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader <span class="hljs-meta">&gt;&gt;&gt; </span>train_dataloader = DataLoader(small_train_dataset, shuffle=<span class="hljs-literal">True</span>, batch_size=<span class="hljs-number">8</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>eval_dataloader = DataLoader(small_eval_dataset, batch_size=<span class="hljs-number">8</span>)</pre></div> <p>Load your model with the number of expected labels:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSequenceClassification.from_pretrained(<span class="hljs-string">"bert-base-cased"</span>, num_labels=<span class="hljs-number">5</span>)</pre></div> <h3 class="relative group"><a id="optimizer-and-learning-rate-scheduler" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#optimizer-and-learning-rate-scheduler"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Optimizer and learning rate scheduler</span></h3> <p>Create an optimizer and learning rate scheduler to fine-tune the model. Let’s use the <a href="https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html" rel="nofollow"><code>AdamW</code></a> optimizer from PyTorch:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> torch.optim <span class="hljs-keyword">import</span> AdamW <span class="hljs-meta">&gt;&gt;&gt; </span>optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">5e-5</span>)</pre></div> <p>Create the default learning rate scheduler from <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler <span class="hljs-meta">&gt;&gt;&gt; </span>num_epochs = <span class="hljs-number">3</span> <span class="hljs-meta">&gt;&gt;&gt; </span>num_training_steps = num_epochs * <span class="hljs-built_in">len</span>(train_dataloader) <span class="hljs-meta">&gt;&gt;&gt; </span>lr_scheduler = get_scheduler( <span class="hljs-meta">... </span> name=<span class="hljs-string">"linear"</span>, optimizer=optimizer, num_warmup_steps=<span class="hljs-number">0</span>, num_training_steps=num_training_steps <span class="hljs-meta">... </span>)</pre></div> <p>Lastly, specify <code>device</code> to use a GPU if you have access to one. Otherwise, training on a CPU may take several hours instead of a couple of minutes.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> torch <span class="hljs-meta">&gt;&gt;&gt; </span>device = torch.device(<span class="hljs-string">"cuda"</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">"cpu"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model.to(device)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>Get free access to a cloud GPU if you don’t have one with a hosted notebook like <a href="https://colab.research.google.com/" rel="nofollow">Colaboratory</a> or <a href="https://studiolab.sagemaker.aws/" rel="nofollow">SageMaker StudioLab</a>.</p></div> <p>Great, now you are ready to train! 🥳</p> <h3 class="relative group"><a id="training-loop" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-loop"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training loop</span></h3> <p>To keep track of your training progress, use the <a href="https://tqdm.github.io/" rel="nofollow">tqdm</a> library to add a progress bar over the number of training steps:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm <span class="hljs-meta">&gt;&gt;&gt; </span>progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps)) <span class="hljs-meta">&gt;&gt;&gt; </span>model.train() <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs): <span class="hljs-meta">... </span> <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader: <span class="hljs-meta">... </span> batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()} <span class="hljs-meta">... </span> outputs = model(**batch) <span class="hljs-meta">... </span> loss = outputs.loss <span class="hljs-meta">... </span> loss.backward() <span class="hljs-meta">... </span> optimizer.step() <span class="hljs-meta">... </span> lr_scheduler.step() <span class="hljs-meta">... </span> optimizer.zero_grad() <span class="hljs-meta">... </span> progress_bar.update(<span class="hljs-number">1</span>)</pre></div> <h3 class="relative group"><a id="evaluate" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#evaluate"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Evaluate</span></h3> <p>Just like how you added an evaluation function to <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a>, you need to do the same when you write your own training loop. But instead of calculating and reporting the metric at the end of each epoch, this time you’ll accumulate all the batches with <code>add_batch</code> and calculate the metric at the very end.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> evaluate <span class="hljs-meta">&gt;&gt;&gt; </span>metric = evaluate.load(<span class="hljs-string">"accuracy"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model.<span class="hljs-built_in">eval</span>() <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> eval_dataloader: <span class="hljs-meta">... </span> batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()} <span class="hljs-meta">... </span> <span class="hljs-keyword">with</span> torch.no_grad(): <span class="hljs-meta">... </span> outputs = model(**batch) <span class="hljs-meta">... </span> logits = outputs.logits <span class="hljs-meta">... </span> predictions = torch.argmax(logits, dim=-<span class="hljs-number">1</span>) <span class="hljs-meta">... </span> metric.add_batch(predictions=predictions, references=batch[<span class="hljs-string">"labels"</span>]) <span class="hljs-meta">&gt;&gt;&gt; </span>metric.compute()</pre></div></div></div> </div> <a id="additional-resources"></a> <h2 class="relative group"><a id="additional-resources" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#additional-resources"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Additional resources</span></h2> <p>For more fine-tuning examples, refer to:</p> <ul><li><p><a href="https://github.com/huggingface/transformers/tree/main/examples" rel="nofollow">🤗 Transformers Examples</a> includes scripts to train common NLP tasks in PyTorch and TensorFlow.</p></li> <li><p><a href="notebooks">🤗 Transformers Notebooks</a> contains various notebooks on how to fine-tune a model for specific tasks in PyTorch and TensorFlow.</p></li></ul> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/preprocessing" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Preprocess data</a> <a href="/docs/transformers/run_scripts" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Train with a script<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;finetune-a-pretrained-model&quot;,&quot;url&quot;:&quot;#finetune-a-pretrained-model&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Prepare a dataset&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;prepare-a-dataset&quot;,&quot;url&quot;:&quot;#prepare-a-dataset&quot;},{&quot;title&quot;:&quot;Train&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;train&quot;,&quot;url&quot;:&quot;#train&quot;},{&quot;title&quot;:&quot;Train with PyTorch Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;train-with-pytorch-trainer&quot;,&quot;url&quot;:&quot;#train-with-pytorch-trainer&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Training hyperparameters&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training-hyperparameters&quot;,&quot;url&quot;:&quot;#training-hyperparameters&quot;},{&quot;title&quot;:&quot;Evaluate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;evaluate&quot;,&quot;url&quot;:&quot;#evaluate&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;trainer&quot;,&quot;url&quot;:&quot;#trainer&quot;}]},{&quot;title&quot;:&quot;Train a TensorFlow model with Keras&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;train-a-tensorflow-model-with-keras&quot;,&quot;url&quot;:&quot;#train-a-tensorflow-model-with-keras&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Loading data for Keras&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;loading-data-for-keras&quot;,&quot;url&quot;:&quot;#loading-data-for-keras&quot;},{&quot;title&quot;:&quot;Loading data as a tf.data.Dataset&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;loading-data-as-a-tfdatadataset&quot;,&quot;url&quot;:&quot;#loading-data-as-a-tfdatadataset&quot;}]},{&quot;title&quot;:&quot;Train in native PyTorch&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;train-in-native-pytorch&quot;,&quot;url&quot;:&quot;#train-in-native-pytorch&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;DataLoader&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;dataloader&quot;,&quot;url&quot;:&quot;#dataloader&quot;},{&quot;title&quot;:&quot;Optimizer and learning rate scheduler&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;optimizer-and-learning-rate-scheduler&quot;,&quot;url&quot;:&quot;#optimizer-and-learning-rate-scheduler&quot;},{&quot;title&quot;:&quot;Training loop&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training-loop&quot;,&quot;url&quot;:&quot;#training-loop&quot;},{&quot;title&quot;:&quot;Evaluate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;evaluate&quot;,&quot;url&quot;:&quot;#evaluate&quot;}]},{&quot;title&quot;:&quot;Additional resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;additional-resources&quot;,&quot;url&quot;:&quot;#additional-resources&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#finetune-a-pretrained-model" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-finetune-a-pretrained-model"><wbr>Fine-tune a pretrained model</a> <a href="#prepare-a-dataset" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-prepare-a-dataset"><wbr>Prepare a dataset</a> <a href="#train" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-train"><wbr>Train</a> <a href="#train-with-pytorch-trainer" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-train-with-pytorch-trainer"><wbr>Train with <wbr>Py<wbr>Torch <wbr>Trainer</a> <a href="#training-hyperparameters" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-hyperparameters"><wbr>Training hyperparameters</a> <a href="#evaluate" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-evaluate"><wbr>Evaluate</a> <a href="#trainer" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-trainer"><wbr>Trainer</a> <a href="#train-a-tensorflow-model-with-keras" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-train-a-tensorflow-model-with-keras"><wbr>Train a <wbr>Tensor<wbr>Flow model with <wbr>Keras</a> <a href="#loading-data-for-keras" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-loading-data-for-keras"><wbr>Loading data for <wbr>Keras</a> <a href="#loading-data-as-a-tfdatadataset" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-loading-data-as-a-tfdatadataset"><wbr>Loading data as a tf.data.<wbr>Dataset</a> <a href="#train-in-native-pytorch" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-train-in-native-pytorch"><wbr>Train in native <wbr>Py<wbr>Torch</a> <a href="#dataloader" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-dataloader"><wbr>Data<wbr>Loader</a> <a href="#optimizer-and-learning-rate-scheduler" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-optimizer-and-learning-rate-scheduler"><wbr>Optimizer and learning rate scheduler</a> <a href="#training-loop" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-training-loop"><wbr>Training loop</a> <a href="#evaluate" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-evaluate"><wbr>Evaluate</a> <a href="#additional-resources" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-additional-resources"><wbr>Additional resources</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/training" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/training"); } </script> <iframe name="__privateStripeMetricsController2960" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Ftraining&amp;title=Fine-tune%20a%20pretrained%20model&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:07.444Z
Train with a script
https://huggingface.co/docs/transformers/run_scripts
Along with the 🤗 Transformers [notebooks](./noteboks/README), there are also example scripts demonstrating how to train a model for a task with [PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch), [TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow), or [JAX/Flax](https://github.com/huggingface/transformers/tree/main/examples/flax). You will also find scripts we’ve used in our [research projects](https://github.com/huggingface/transformers/tree/main/examples/research_projects) and [legacy examples](https://github.com/huggingface/transformers/tree/main/examples/legacy) which are mostly community contributed. These scripts are not actively maintained and require a specific version of 🤗 Transformers that will most likely be incompatible with the latest version of the library. The example scripts are not expected to work out-of-the-box on every problem, and you may need to adapt the script to the problem you’re trying to solve. To help you with this, most of the scripts fully expose how data is preprocessed, allowing you to edit it as necessary for your use case. For any feature you’d like to implement in an example script, please discuss it on the [forum](https://discuss.huggingface.co/) or in an [issue](https://github.com/huggingface/transformers/issues) before submitting a Pull Request. While we welcome bug fixes, it is unlikely we will merge a Pull Request that adds more functionality at the cost of readability. This guide will show you how to run an example summarization training script in [PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization) and [TensorFlow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/summarization). All examples are expected to work with both frameworks unless otherwise specified. ## [](#setup)Setup To successfully run the latest version of the example scripts, you have to **install 🤗 Transformers from source** in a new virtual environment: ``` git clone https://github.com/huggingface/transformers cd transformers pip install .``` For older versions of the example scripts, click on the toggle below: Examples for older versions of 🤗 Transformers - [v4.5.1](https://github.com/huggingface/transformers/tree/v4.5.1/examples) - [v4.4.2](https://github.com/huggingface/transformers/tree/v4.4.2/examples) - [v4.3.3](https://github.com/huggingface/transformers/tree/v4.3.3/examples) - [v4.2.2](https://github.com/huggingface/transformers/tree/v4.2.2/examples) - [v4.1.1](https://github.com/huggingface/transformers/tree/v4.1.1/examples) - [v4.0.1](https://github.com/huggingface/transformers/tree/v4.0.1/examples) - [v3.5.1](https://github.com/huggingface/transformers/tree/v3.5.1/examples) - [v3.4.0](https://github.com/huggingface/transformers/tree/v3.4.0/examples) - [v3.3.1](https://github.com/huggingface/transformers/tree/v3.3.1/examples) - [v3.2.0](https://github.com/huggingface/transformers/tree/v3.2.0/examples) - [v3.1.0](https://github.com/huggingface/transformers/tree/v3.1.0/examples) - [v3.0.2](https://github.com/huggingface/transformers/tree/v3.0.2/examples) - [v2.11.0](https://github.com/huggingface/transformers/tree/v2.11.0/examples) - [v2.10.0](https://github.com/huggingface/transformers/tree/v2.10.0/examples) - [v2.9.1](https://github.com/huggingface/transformers/tree/v2.9.1/examples) - [v2.8.0](https://github.com/huggingface/transformers/tree/v2.8.0/examples) - [v2.7.0](https://github.com/huggingface/transformers/tree/v2.7.0/examples) - [v2.6.0](https://github.com/huggingface/transformers/tree/v2.6.0/examples) - [v2.5.1](https://github.com/huggingface/transformers/tree/v2.5.1/examples) - [v2.4.0](https://github.com/huggingface/transformers/tree/v2.4.0/examples) - [v2.3.0](https://github.com/huggingface/transformers/tree/v2.3.0/examples) - [v2.2.0](https://github.com/huggingface/transformers/tree/v2.2.0/examples) - [v2.1.1](https://github.com/huggingface/transformers/tree/v2.1.0/examples) - [v2.0.0](https://github.com/huggingface/transformers/tree/v2.0.0/examples) - [v1.2.0](https://github.com/huggingface/transformers/tree/v1.2.0/examples) - [v1.1.0](https://github.com/huggingface/transformers/tree/v1.1.0/examples) - [v1.0.0](https://github.com/huggingface/transformers/tree/v1.0.0/examples) Then switch your current clone of 🤗 Transformers to a specific version, like v3.5.1 for example: After you’ve setup the correct library version, navigate to the example folder of your choice and install the example specific requirements: ``` pip install -r requirements.txt``` ## [](#run-a-script)Run a script The example script downloads and preprocesses a dataset from the 🤗 [Datasets](https://huggingface.co/docs/datasets/) library. Then the script fine-tunes a dataset with the [Trainer](https://huggingface.co/docs/transformers/main_classes/trainer) on an architecture that supports summarization. The following example shows how to fine-tune [T5-small](https://huggingface.co/t5-small) on the [CNN/DailyMail](https://huggingface.co/datasets/cnn_dailymail) dataset. The T5 model requires an additional `source_prefix` argument due to how it was trained. This prompt lets T5 know this is a summarization task. ``` python examples/pytorch/summarization/run_summarization.py \ --model_name_or_path t5-small \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config "3.0.0" \ --source_prefix "summarize: " \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --overwrite_output_dir \ --predict_with_generate``` The example script downloads and preprocesses a dataset from the 🤗 [Datasets](https://huggingface.co/docs/datasets/) library. Then the script fine-tunes a dataset using Keras on an architecture that supports summarization. The following example shows how to fine-tune [T5-small](https://huggingface.co/t5-small) on the [CNN/DailyMail](https://huggingface.co/datasets/cnn_dailymail) dataset. The T5 model requires an additional `source_prefix` argument due to how it was trained. This prompt lets T5 know this is a summarization task. ``` python examples/tensorflow/summarization/run_summarization.py \ --model_name_or_path t5-small \ --dataset_name cnn_dailymail \ --dataset_config "3.0.0" \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size 8 \ --per_device_eval_batch_size 16 \ --num_train_epochs 3 \ --do_train \ --do_eval``` ## [](#distributed-training-and-mixed-precision)Distributed training and mixed precision The [Trainer](https://huggingface.co/docs/transformers/main_classes/trainer) supports distributed training and mixed precision, which means you can also use it in a script. To enable both of these features: - Add the `fp16` argument to enable mixed precision. - Set the number of GPUs to use with the `nproc_per_node` argument. ``` python -m torch.distributed.launch \ --nproc_per_node 8 pytorch/summarization/run_summarization.py \ --fp16 \ --model_name_or_path t5-small \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config "3.0.0" \ --source_prefix "summarize: " \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --overwrite_output_dir \ --predict_with_generate``` TensorFlow scripts utilize a [`MirroredStrategy`](https://www.tensorflow.org/guide/distributed_training#mirroredstrategy) for distributed training, and you don’t need to add any additional arguments to the training script. The TensorFlow script will use multiple GPUs by default if they are available. ## [](#run-a-script-on-a-tpu)Run a script on a TPU Tensor Processing Units (TPUs) are specifically designed to accelerate performance. PyTorch supports TPUs with the [XLA](https://www.tensorflow.org/xla) deep learning compiler (see [here](https://github.com/pytorch/xla/blob/master/README.md) for more details). To use a TPU, launch the `xla_spawn.py` script and use the `num_cores` argument to set the number of TPU cores you want to use. ``` python xla_spawn.py --num_cores 8 \ summarization/run_summarization.py \ --model_name_or_path t5-small \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config "3.0.0" \ --source_prefix "summarize: " \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --overwrite_output_dir \ --predict_with_generate``` Tensor Processing Units (TPUs) are specifically designed to accelerate performance. TensorFlow scripts utilize a [`TPUStrategy`](https://www.tensorflow.org/guide/distributed_training#tpustrategy) for training on TPUs. To use a TPU, pass the name of the TPU resource to the `tpu` argument. ``` python run_summarization.py \ --tpu name_of_tpu_resource \ --model_name_or_path t5-small \ --dataset_name cnn_dailymail \ --dataset_config "3.0.0" \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size 8 \ --per_device_eval_batch_size 16 \ --num_train_epochs 3 \ --do_train \ --do_eval``` ## [](#run-a-script-with-accelerate)Run a script with 🤗 Accelerate 🤗 [Accelerate](https://huggingface.co/docs/accelerate) is a PyTorch-only library that offers a unified method for training a model on several types of setups (CPU-only, multiple GPUs, TPUs) while maintaining complete visibility into the PyTorch training loop. Make sure you have 🤗 Accelerate installed if you don’t already have it: > Note: As Accelerate is rapidly developing, the git version of accelerate must be installed to run the scripts > > ``` > pip install git+https://github.com/huggingface/accelerate``` Instead of the `run_summarization.py` script, you need to use the `run_summarization_no_trainer.py` script. 🤗 Accelerate supported scripts will have a `task_no_trainer.py` file in the folder. Begin by running the following command to create and save a configuration file: Test your setup to make sure it is configured correctly: Now you are ready to launch the training: ``` accelerate launch run_summarization_no_trainer.py \ --model_name_or_path t5-small \ --dataset_name cnn_dailymail \ --dataset_config "3.0.0" \ --source_prefix "summarize: " \ --output_dir ~/tmp/tst-summarization``` ## [](#use-a-custom-dataset)Use a custom dataset The summarization script supports custom datasets as long as they are a CSV or JSON Line file. When you use your own dataset, you need to specify several additional arguments: - `train_file` and `validation_file` specify the path to your training and validation files. - `text_column` is the input text to summarize. - `summary_column` is the target text to output. A summarization script using a custom dataset would look like this: ``` python examples/pytorch/summarization/run_summarization.py \ --model_name_or_path t5-small \ --do_train \ --do_eval \ --train_file path_to_csv_or_jsonlines_file \ --validation_file path_to_csv_or_jsonlines_file \ --text_column text_column_name \ --summary_column summary_column_name \ --source_prefix "summarize: " \ --output_dir /tmp/tst-summarization \ --overwrite_output_dir \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --predict_with_generate``` ## [](#test-a-script)Test a script It is often a good idea to run your script on a smaller number of dataset examples to ensure everything works as expected before committing to an entire dataset which may take hours to complete. Use the following arguments to truncate the dataset to a maximum number of samples: - `max_train_samples` - `max_eval_samples` - `max_predict_samples` ``` python examples/pytorch/summarization/run_summarization.py \ --model_name_or_path t5-small \ --max_train_samples 50 \ --max_eval_samples 50 \ --max_predict_samples 50 \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config "3.0.0" \ --source_prefix "summarize: " \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --overwrite_output_dir \ --predict_with_generate``` Not all example scripts support the `max_predict_samples` argument. If you aren’t sure whether your script supports this argument, add the `-h` argument to check: ``` examples/pytorch/summarization/run_summarization.py -h``` ## [](#resume-training-from-checkpoint)Resume training from checkpoint Another helpful option to enable is resuming training from a previous checkpoint. This will ensure you can pick up where you left off without starting over if your training gets interrupted. There are two methods to resume training from a checkpoint. The first method uses the `output_dir previous_output_dir` argument to resume training from the latest checkpoint stored in `output_dir`. In this case, you should remove `overwrite_output_dir`: ``` python examples/pytorch/summarization/run_summarization.py --model_name_or_path t5-small \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config "3.0.0" \ --source_prefix "summarize: " \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --output_dir previous_output_dir \ --predict_with_generate``` The second method uses the `resume_from_checkpoint path_to_specific_checkpoint` argument to resume training from a specific checkpoint folder. ``` python examples/pytorch/summarization/run_summarization.py --model_name_or_path t5-small \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config "3.0.0" \ --source_prefix "summarize: " \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --overwrite_output_dir \ --resume_from_checkpoint path_to_specific_checkpoint \ --predict_with_generate``` ## Share your model All scripts can upload your final model to the [Model Hub](https://huggingface.co/models). Make sure you are logged into Hugging Face before you begin: Then add the `push_to_hub` argument to the script. This argument will create a repository with your Hugging Face username and the folder name specified in `output_dir`. To give your repository a specific name, use the `push_to_hub_model_id` argument to add it. The repository will be automatically listed under your namespace. The following example shows how to upload a model with a specific repository name: ``` python examples/pytorch/summarization/run_summarization.py --model_name_or_path t5-small \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config "3.0.0" \ --source_prefix "summarize: " \ --push_to_hub \ --push_to_hub_model_id finetuned-t5-cnn_dailymail \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --overwrite_output_dir \ --predict_with_generate```
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Train with a script"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/run_scripts"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Train with a script</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;train-with-a-script&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;setup&quot;,&quot;title&quot;:&quot;Setup&quot;},{&quot;local&quot;:&quot;run-a-script&quot;,&quot;title&quot;:&quot;Run a script&quot;},{&quot;local&quot;:&quot;distributed-training-and-mixed-precision&quot;,&quot;title&quot;:&quot;Distributed training and mixed precision&quot;},{&quot;local&quot;:&quot;run-a-script-on-a-tpu&quot;,&quot;title&quot;:&quot;Run a script on a TPU&quot;},{&quot;local&quot;:&quot;run-a-script-with-accelerate&quot;,&quot;title&quot;:&quot;Run a script with 🤗 Accelerate&quot;},{&quot;local&quot;:&quot;use-a-custom-dataset&quot;,&quot;title&quot;:&quot;Use a custom dataset&quot;},{&quot;local&quot;:&quot;test-a-script&quot;,&quot;title&quot;:&quot;Test a script&quot;},{&quot;local&quot;:&quot;resume-training-from-checkpoint&quot;,&quot;title&quot;:&quot;Resume training from checkpoint&quot;},{&quot;local&quot;:&quot;share-your-model&quot;,&quot;title&quot;:&quot;Share your model&quot;}],&quot;title&quot;:&quot;Train with a script&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;run_scripts&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Train with a script&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Train with a script</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="train-with-a-script" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#train-with-a-script"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Train with a script</span></h1> <p>Along with the 🤗 Transformers <a href="./noteboks/README">notebooks</a>, there are also example scripts demonstrating how to train a model for a task with <a href="https://github.com/huggingface/transformers/tree/main/examples/pytorch" rel="nofollow">PyTorch</a>, <a href="https://github.com/huggingface/transformers/tree/main/examples/tensorflow" rel="nofollow">TensorFlow</a>, or <a href="https://github.com/huggingface/transformers/tree/main/examples/flax" rel="nofollow">JAX/Flax</a>.</p> <p>You will also find scripts we’ve used in our <a href="https://github.com/huggingface/transformers/tree/main/examples/research_projects" rel="nofollow">research projects</a> and <a href="https://github.com/huggingface/transformers/tree/main/examples/legacy" rel="nofollow">legacy examples</a> which are mostly community contributed. These scripts are not actively maintained and require a specific version of 🤗 Transformers that will most likely be incompatible with the latest version of the library.</p> <p>The example scripts are not expected to work out-of-the-box on every problem, and you may need to adapt the script to the problem you’re trying to solve. To help you with this, most of the scripts fully expose how data is preprocessed, allowing you to edit it as necessary for your use case.</p> <p>For any feature you’d like to implement in an example script, please discuss it on the <a href="https://discuss.huggingface.co/" rel="nofollow">forum</a> or in an <a href="https://github.com/huggingface/transformers/issues" rel="nofollow">issue</a> before submitting a Pull Request. While we welcome bug fixes, it is unlikely we will merge a Pull Request that adds more functionality at the cost of readability.</p> <p>This guide will show you how to run an example summarization training script in <a href="https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization" rel="nofollow">PyTorch</a> and <a href="https://github.com/huggingface/transformers/tree/main/examples/tensorflow/summarization" rel="nofollow">TensorFlow</a>. All examples are expected to work with both frameworks unless otherwise specified.</p> <h2 class="relative group"><a id="setup" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#setup"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Setup</span></h2> <p>To successfully run the latest version of the example scripts, you have to <strong>install 🤗 Transformers from source</strong> in a new virtual environment:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>git <span class="hljs-built_in">clone</span> https://github.com/huggingface/transformers <span class="hljs-built_in">cd</span> transformers pip install .</pre></div> <p>For older versions of the example scripts, click on the toggle below:</p> <details><summary>Examples for older versions of 🤗 Transformers</summary> <ul><li><a href="https://github.com/huggingface/transformers/tree/v4.5.1/examples">v4.5.1</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v4.4.2/examples">v4.4.2</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v4.3.3/examples">v4.3.3</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v4.2.2/examples">v4.2.2</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v4.1.1/examples">v4.1.1</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v4.0.1/examples">v4.0.1</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v3.5.1/examples">v3.5.1</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v3.4.0/examples">v3.4.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v3.3.1/examples">v3.3.1</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v3.2.0/examples">v3.2.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v3.1.0/examples">v3.1.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v3.0.2/examples">v3.0.2</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v2.11.0/examples">v2.11.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v2.10.0/examples">v2.10.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v2.9.1/examples">v2.9.1</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v2.8.0/examples">v2.8.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v2.7.0/examples">v2.7.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v2.6.0/examples">v2.6.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v2.5.1/examples">v2.5.1</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v2.4.0/examples">v2.4.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v2.3.0/examples">v2.3.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v2.2.0/examples">v2.2.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v2.1.0/examples">v2.1.1</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v2.0.0/examples">v2.0.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v1.2.0/examples">v1.2.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v1.1.0/examples">v1.1.0</a></li> <li><a href="https://github.com/huggingface/transformers/tree/v1.0.0/examples">v1.0.0</a></li></ul></details> <p>Then switch your current clone of 🤗 Transformers to a specific version, like v3.5.1 for example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>git checkout tags/v3.5.1</pre></div> <p>After you’ve setup the correct library version, navigate to the example folder of your choice and install the example specific requirements:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install -r requirements.txt</pre></div> <h2 class="relative group"><a id="run-a-script" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#run-a-script"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Run a script</span></h2> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><p>The example script downloads and preprocesses a dataset from the 🤗 <a href="https://huggingface.co/docs/datasets/" rel="nofollow">Datasets</a> library. Then the script fine-tunes a dataset with the <a href="https://huggingface.co/docs/transformers/main_classes/trainer" rel="nofollow">Trainer</a> on an architecture that supports summarization. The following example shows how to fine-tune <a href="https://huggingface.co/t5-small" rel="nofollow">T5-small</a> on the <a href="https://huggingface.co/datasets/cnn_dailymail" rel="nofollow">CNN/DailyMail</a> dataset. The T5 model requires an additional <code>source_prefix</code> argument due to how it was trained. This prompt lets T5 know this is a summarization task.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python examples/pytorch/summarization/run_summarization.py \ --model_name_or_path t5-small \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config <span class="hljs-string">"3.0.0"</span> \ --source_prefix <span class="hljs-string">"summarize: "</span> \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --overwrite_output_dir \ --predict_with_generate</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><p>The example script downloads and preprocesses a dataset from the 🤗 <a href="https://huggingface.co/docs/datasets/" rel="nofollow">Datasets</a> library. Then the script fine-tunes a dataset using Keras on an architecture that supports summarization. The following example shows how to fine-tune <a href="https://huggingface.co/t5-small" rel="nofollow">T5-small</a> on the <a href="https://huggingface.co/datasets/cnn_dailymail" rel="nofollow">CNN/DailyMail</a> dataset. The T5 model requires an additional <code>source_prefix</code> argument due to how it was trained. This prompt lets T5 know this is a summarization task.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python examples/tensorflow/summarization/run_summarization.py \ --model_name_or_path t5-small \ --dataset_name cnn_dailymail \ --dataset_config <span class="hljs-string">"3.0.0"</span> \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size 8 \ --per_device_eval_batch_size 16 \ --num_train_epochs 3 \ --do_train \ --do_eval</pre></div></div></div> </div> <h2 class="relative group"><a id="distributed-training-and-mixed-precision" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#distributed-training-and-mixed-precision"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Distributed training and mixed precision</span></h2> <p>The <a href="https://huggingface.co/docs/transformers/main_classes/trainer" rel="nofollow">Trainer</a> supports distributed training and mixed precision, which means you can also use it in a script. To enable both of these features:</p> <ul><li>Add the <code>fp16</code> argument to enable mixed precision.</li> <li>Set the number of GPUs to use with the <code>nproc_per_node</code> argument.</li></ul> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python -m torch.distributed.launch \ --nproc_per_node 8 pytorch/summarization/run_summarization.py \ --fp16 \ --model_name_or_path t5-small \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config <span class="hljs-string">"3.0.0"</span> \ --source_prefix <span class="hljs-string">"summarize: "</span> \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --overwrite_output_dir \ --predict_with_generate</pre></div> <p>TensorFlow scripts utilize a <a href="https://www.tensorflow.org/guide/distributed_training#mirroredstrategy" rel="nofollow"><code>MirroredStrategy</code></a> for distributed training, and you don’t need to add any additional arguments to the training script. The TensorFlow script will use multiple GPUs by default if they are available.</p> <h2 class="relative group"><a id="run-a-script-on-a-tpu" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#run-a-script-on-a-tpu"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Run a script on a TPU</span></h2> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><p>Tensor Processing Units (TPUs) are specifically designed to accelerate performance. PyTorch supports TPUs with the <a href="https://www.tensorflow.org/xla" rel="nofollow">XLA</a> deep learning compiler (see <a href="https://github.com/pytorch/xla/blob/master/README.md" rel="nofollow">here</a> for more details). To use a TPU, launch the <code>xla_spawn.py</code> script and use the <code>num_cores</code> argument to set the number of TPU cores you want to use.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python xla_spawn.py --num_cores 8 \ summarization/run_summarization.py \ --model_name_or_path t5-small \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config <span class="hljs-string">"3.0.0"</span> \ --source_prefix <span class="hljs-string">"summarize: "</span> \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --overwrite_output_dir \ --predict_with_generate</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><p>Tensor Processing Units (TPUs) are specifically designed to accelerate performance. TensorFlow scripts utilize a <a href="https://www.tensorflow.org/guide/distributed_training#tpustrategy" rel="nofollow"><code>TPUStrategy</code></a> for training on TPUs. To use a TPU, pass the name of the TPU resource to the <code>tpu</code> argument.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python run_summarization.py \ --tpu name_of_tpu_resource \ --model_name_or_path t5-small \ --dataset_name cnn_dailymail \ --dataset_config <span class="hljs-string">"3.0.0"</span> \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size 8 \ --per_device_eval_batch_size 16 \ --num_train_epochs 3 \ --do_train \ --do_eval</pre></div></div></div> </div> <h2 class="relative group"><a id="run-a-script-with-accelerate" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#run-a-script-with-accelerate"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Run a script with 🤗 Accelerate</span></h2> <p>🤗 <a href="https://huggingface.co/docs/accelerate" rel="nofollow">Accelerate</a> is a PyTorch-only library that offers a unified method for training a model on several types of setups (CPU-only, multiple GPUs, TPUs) while maintaining complete visibility into the PyTorch training loop. Make sure you have 🤗 Accelerate installed if you don’t already have it:</p> <blockquote><p>Note: As Accelerate is rapidly developing, the git version of accelerate must be installed to run the scripts</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install git+https://github.com/huggingface/accelerate</pre></div></blockquote> <p>Instead of the <code>run_summarization.py</code> script, you need to use the <code>run_summarization_no_trainer.py</code> script. 🤗 Accelerate supported scripts will have a <code>task_no_trainer.py</code> file in the folder. Begin by running the following command to create and save a configuration file:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>accelerate config</pre></div> <p>Test your setup to make sure it is configured correctly:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>accelerate <span class="hljs-built_in">test</span></pre></div> <p>Now you are ready to launch the training:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>accelerate launch run_summarization_no_trainer.py \ --model_name_or_path t5-small \ --dataset_name cnn_dailymail \ --dataset_config <span class="hljs-string">"3.0.0"</span> \ --source_prefix <span class="hljs-string">"summarize: "</span> \ --output_dir ~/tmp/tst-summarization</pre></div> <h2 class="relative group"><a id="use-a-custom-dataset" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#use-a-custom-dataset"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Use a custom dataset</span></h2> <p>The summarization script supports custom datasets as long as they are a CSV or JSON Line file. When you use your own dataset, you need to specify several additional arguments:</p> <ul><li><code>train_file</code> and <code>validation_file</code> specify the path to your training and validation files.</li> <li><code>text_column</code> is the input text to summarize.</li> <li><code>summary_column</code> is the target text to output.</li></ul> <p>A summarization script using a custom dataset would look like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python examples/pytorch/summarization/run_summarization.py \ --model_name_or_path t5-small \ --do_train \ --do_eval \ --train_file path_to_csv_or_jsonlines_file \ --validation_file path_to_csv_or_jsonlines_file \ --text_column text_column_name \ --summary_column summary_column_name \ --source_prefix <span class="hljs-string">"summarize: "</span> \ --output_dir /tmp/tst-summarization \ --overwrite_output_dir \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --predict_with_generate</pre></div> <h2 class="relative group"><a id="test-a-script" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#test-a-script"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Test a script</span></h2> <p>It is often a good idea to run your script on a smaller number of dataset examples to ensure everything works as expected before committing to an entire dataset which may take hours to complete. Use the following arguments to truncate the dataset to a maximum number of samples:</p> <ul><li><code>max_train_samples</code></li> <li><code>max_eval_samples</code></li> <li><code>max_predict_samples</code></li></ul> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python examples/pytorch/summarization/run_summarization.py \ --model_name_or_path t5-small \ --max_train_samples 50 \ --max_eval_samples 50 \ --max_predict_samples 50 \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config <span class="hljs-string">"3.0.0"</span> \ --source_prefix <span class="hljs-string">"summarize: "</span> \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --overwrite_output_dir \ --predict_with_generate</pre></div> <p>Not all example scripts support the <code>max_predict_samples</code> argument. If you aren’t sure whether your script supports this argument, add the <code>-h</code> argument to check:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>examples/pytorch/summarization/run_summarization.py -h</pre></div> <h2 class="relative group"><a id="resume-training-from-checkpoint" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#resume-training-from-checkpoint"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Resume training from checkpoint</span></h2> <p>Another helpful option to enable is resuming training from a previous checkpoint. This will ensure you can pick up where you left off without starting over if your training gets interrupted. There are two methods to resume training from a checkpoint.</p> <p>The first method uses the <code>output_dir previous_output_dir</code> argument to resume training from the latest checkpoint stored in <code>output_dir</code>. In this case, you should remove <code>overwrite_output_dir</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python examples/pytorch/summarization/run_summarization.py --model_name_or_path t5-small \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config <span class="hljs-string">"3.0.0"</span> \ --source_prefix <span class="hljs-string">"summarize: "</span> \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --output_dir previous_output_dir \ --predict_with_generate</pre></div> <p>The second method uses the <code>resume_from_checkpoint path_to_specific_checkpoint</code> argument to resume training from a specific checkpoint folder.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python examples/pytorch/summarization/run_summarization.py --model_name_or_path t5-small \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config <span class="hljs-string">"3.0.0"</span> \ --source_prefix <span class="hljs-string">"summarize: "</span> \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --overwrite_output_dir \ --resume_from_checkpoint path_to_specific_checkpoint \ --predict_with_generate</pre></div> <h2 class="relative group"><a id="share-your-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#share-your-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Share your model</span></h2> <p>All scripts can upload your final model to the <a href="https://huggingface.co/models" rel="nofollow">Model Hub</a>. Make sure you are logged into Hugging Face before you begin:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-cli login</pre></div> <p>Then add the <code>push_to_hub</code> argument to the script. This argument will create a repository with your Hugging Face username and the folder name specified in <code>output_dir</code>.</p> <p>To give your repository a specific name, use the <code>push_to_hub_model_id</code> argument to add it. The repository will be automatically listed under your namespace.</p> <p>The following example shows how to upload a model with a specific repository name:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python examples/pytorch/summarization/run_summarization.py --model_name_or_path t5-small \ --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --dataset_config <span class="hljs-string">"3.0.0"</span> \ --source_prefix <span class="hljs-string">"summarize: "</span> \ --push_to_hub \ --push_to_hub_model_id finetuned-t5-cnn_dailymail \ --output_dir /tmp/tst-summarization \ --per_device_train_batch_size=4 \ --per_device_eval_batch_size=4 \ --overwrite_output_dir \ --predict_with_generate</pre></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/training" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Fine-tune a pretrained model</a> <a href="/docs/transformers/accelerate" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Set up distributed training with 🤗 Accelerate<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;train-with-a-script&quot;,&quot;url&quot;:&quot;#train-with-a-script&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Setup&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;setup&quot;,&quot;url&quot;:&quot;#setup&quot;},{&quot;title&quot;:&quot;Run a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run-a-script&quot;,&quot;url&quot;:&quot;#run-a-script&quot;},{&quot;title&quot;:&quot;Distributed training and mixed precision&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;distributed-training-and-mixed-precision&quot;,&quot;url&quot;:&quot;#distributed-training-and-mixed-precision&quot;},{&quot;title&quot;:&quot;Run a script on a TPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run-a-script-on-a-tpu&quot;,&quot;url&quot;:&quot;#run-a-script-on-a-tpu&quot;},{&quot;title&quot;:&quot;Run a script with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run-a-script-with-accelerate&quot;,&quot;url&quot;:&quot;#run-a-script-with-accelerate&quot;},{&quot;title&quot;:&quot;Use a custom dataset&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;use-a-custom-dataset&quot;,&quot;url&quot;:&quot;#use-a-custom-dataset&quot;},{&quot;title&quot;:&quot;Test a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;test-a-script&quot;,&quot;url&quot;:&quot;#test-a-script&quot;},{&quot;title&quot;:&quot;Resume training from checkpoint&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;resume-training-from-checkpoint&quot;,&quot;url&quot;:&quot;#resume-training-from-checkpoint&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;share-your-model&quot;,&quot;url&quot;:&quot;#share-your-model&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#train-with-a-script" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-train-with-a-script"><wbr>Train with a script</a> <a href="#setup" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-setup"><wbr>Setup</a> <a href="#run-a-script" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-run-a-script"><wbr>Run a script</a> <a href="#distributed-training-and-mixed-precision" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-distributed-training-and-mixed-precision"><wbr>Distributed training and mixed precision</a> <a href="#run-a-script-on-a-tpu" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-run-a-script-on-a-tpu"><wbr>Run a script on a TPU</a> <a href="#run-a-script-with-accelerate" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-run-a-script-with-accelerate"><wbr>Run a script with 🤗 <wbr>Accelerate</a> <a href="#use-a-custom-dataset" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-use-a-custom-dataset"><wbr>Use a custom dataset</a> <a href="#test-a-script" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-test-a-script"><wbr>Test a script</a> <a href="#resume-training-from-checkpoint" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-resume-training-from-checkpoint"><wbr>Resume training from checkpoint</a> <a href="#share-your-model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-share-your-model"><wbr>Share your model</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/run_scripts" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/run_scripts"); } </script> <iframe name="__privateStripeMetricsController6920" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Frun_scripts&amp;title=Train%20with%20a%20script&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:07.551Z
Distributed training with 🤗 Accelerate
https://huggingface.co/docs/transformers/accelerate
As models get bigger, parallelism has emerged as a strategy for training larger models on limited hardware and accelerating training speed by several orders of magnitude. At Hugging Face, we created the [🤗 Accelerate](https://huggingface.co/docs/accelerate) library to help users easily train a 🤗 Transformers model on any type of distributed setup, whether it is multiple GPU’s on one machine or multiple GPU’s across several machines. In this tutorial, learn how to customize your native PyTorch training loop to enable training in a distributed environment. ## [](#setup)Setup Get started by installing 🤗 Accelerate: Then import and create an [Accelerator](https://huggingface.co/docs/accelerate/v0.20.3/en/package_reference/accelerator#accelerate.Accelerator) object. The [Accelerator](https://huggingface.co/docs/accelerate/v0.20.3/en/package_reference/accelerator#accelerate.Accelerator) will automatically detect your type of distributed setup and initialize all the necessary components for training. You don’t need to explicitly place your model on a device. ``` >>> from accelerate import Accelerator >>> accelerator = Accelerator()``` ## [](#prepare-to-accelerate)Prepare to accelerate The next step is to pass all the relevant training objects to the [prepare](https://huggingface.co/docs/accelerate/v0.20.3/en/package_reference/accelerator#accelerate.Accelerator.prepare) method. This includes your training and evaluation DataLoaders, a model and an optimizer: ``` >>> train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( ... train_dataloader, eval_dataloader, model, optimizer ... )``` ## [](#backward)Backward The last addition is to replace the typical `loss.backward()` in your training loop with 🤗 Accelerate’s [backward](https://huggingface.co/docs/accelerate/v0.20.3/en/package_reference/accelerator#accelerate.Accelerator.backward)method: ``` >>> for epoch in range(num_epochs): ... for batch in train_dataloader: ... outputs = model(**batch) ... loss = outputs.loss ... accelerator.backward(loss) ... optimizer.step() ... lr_scheduler.step() ... optimizer.zero_grad() ... progress_bar.update(1)``` As you can see in the following code, you only need to add four additional lines of code to your training loop to enable distributed training! ``` + from accelerate import Accelerator from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler + accelerator = Accelerator() model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) optimizer = AdamW(model.parameters(), lr=3e-5) - device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") - model.to(device) + train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( + train_dataloader, eval_dataloader, model, optimizer + ) num_epochs = 3 num_training_steps = num_epochs * len(train_dataloader) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps ) progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dataloader: - batch = {k: v.to(device) for k, v in batch.items()} outputs = model(**batch) loss = outputs.loss - loss.backward() + accelerator.backward(loss) optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1)``` ## [](#train)Train Once you’ve added the relevant lines of code, launch your training in a script or a notebook like Colaboratory. ### [](#train-with-a-script)Train with a script If you are running your training from a script, run the following command to create and save a configuration file: Then launch your training with: ``` accelerate launch train.py``` ### [](#train-with-a-notebook)Train with a notebook 🤗 Accelerate can also run in a notebook if you’re planning on using Colaboratory’s TPUs. Wrap all the code responsible for training in a function, and pass it to [notebook\_launcher](https://huggingface.co/docs/accelerate/v0.20.3/en/package_reference/launchers#accelerate.notebook_launcher): ``` >>> from accelerate import notebook_launcher >>> notebook_launcher(training_function)``` For more information about 🤗 Accelerate and it’s rich features, refer to the [documentation](https://huggingface.co/docs/accelerate).
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Distributed training with 🤗 Accelerate"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/accelerate"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Distributed training with 🤗 Accelerate</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;distributed-training-with-accelerate&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;setup&quot;,&quot;title&quot;:&quot;Setup&quot;},{&quot;local&quot;:&quot;prepare-to-accelerate&quot;,&quot;title&quot;:&quot;Prepare to accelerate&quot;},{&quot;local&quot;:&quot;backward&quot;,&quot;title&quot;:&quot;Backward&quot;},{&quot;local&quot;:&quot;train&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;train-with-a-script&quot;,&quot;title&quot;:&quot;Train with a script&quot;},{&quot;local&quot;:&quot;train-with-a-notebook&quot;,&quot;title&quot;:&quot;Train with a notebook&quot;}],&quot;title&quot;:&quot;Train&quot;}],&quot;title&quot;:&quot;Distributed training with 🤗 Accelerate&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;accelerate&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Distributed training with 🤗 Accelerate&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Distributed training with 🤗 Accelerate</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="distributed-training-with-accelerate" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#distributed-training-with-accelerate"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Distributed training with 🤗 Accelerate</span></h1> <p>As models get bigger, parallelism has emerged as a strategy for training larger models on limited hardware and accelerating training speed by several orders of magnitude. At Hugging Face, we created the <a href="https://huggingface.co/docs/accelerate" rel="nofollow">🤗 Accelerate</a> library to help users easily train a 🤗 Transformers model on any type of distributed setup, whether it is multiple GPU’s on one machine or multiple GPU’s across several machines. In this tutorial, learn how to customize your native PyTorch training loop to enable training in a distributed environment.</p> <h2 class="relative group"><a id="setup" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#setup"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Setup</span></h2> <p>Get started by installing 🤗 Accelerate:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install accelerate</pre></div> <p>Then import and create an <a href="https://huggingface.co/docs/accelerate/v0.20.3/en/package_reference/accelerator#accelerate.Accelerator" rel="nofollow">Accelerator</a> object. The <a href="https://huggingface.co/docs/accelerate/v0.20.3/en/package_reference/accelerator#accelerate.Accelerator" rel="nofollow">Accelerator</a> will automatically detect your type of distributed setup and initialize all the necessary components for training. You don’t need to explicitly place your model on a device.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator <span class="hljs-meta">&gt;&gt;&gt; </span>accelerator = Accelerator()</pre></div> <h2 class="relative group"><a id="prepare-to-accelerate" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#prepare-to-accelerate"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Prepare to accelerate</span></h2> <p>The next step is to pass all the relevant training objects to the <a href="https://huggingface.co/docs/accelerate/v0.20.3/en/package_reference/accelerator#accelerate.Accelerator.prepare" rel="nofollow">prepare</a> method. This includes your training and evaluation DataLoaders, a model and an optimizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare( <span class="hljs-meta">... </span> train_dataloader, eval_dataloader, model, optimizer <span class="hljs-meta">... </span>)</pre></div> <h2 class="relative group"><a id="backward" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#backward"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Backward</span></h2> <p>The last addition is to replace the typical <code>loss.backward()</code> in your training loop with 🤗 Accelerate’s <a href="https://huggingface.co/docs/accelerate/v0.20.3/en/package_reference/accelerator#accelerate.Accelerator.backward" rel="nofollow">backward</a>method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs): <span class="hljs-meta">... </span> <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader: <span class="hljs-meta">... </span> outputs = model(**batch) <span class="hljs-meta">... </span> loss = outputs.loss <span class="hljs-meta">... </span> accelerator.backward(loss) <span class="hljs-meta">... </span> optimizer.step() <span class="hljs-meta">... </span> lr_scheduler.step() <span class="hljs-meta">... </span> optimizer.zero_grad() <span class="hljs-meta">... </span> progress_bar.update(<span class="hljs-number">1</span>)</pre></div> <p>As you can see in the following code, you only need to add four additional lines of code to your training loop to enable distributed training!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-addition">+ from accelerate import Accelerator</span> from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler <span class="hljs-addition">+ accelerator = Accelerator()</span> model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) optimizer = AdamW(model.parameters(), lr=3e-5) <span class="hljs-deletion">- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")</span> <span class="hljs-deletion">- model.to(device)</span> <span class="hljs-addition">+ train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare(</span> <span class="hljs-addition">+ train_dataloader, eval_dataloader, model, optimizer</span> <span class="hljs-addition">+ )</span> num_epochs = 3 num_training_steps = num_epochs * len(train_dataloader) lr_scheduler = get_scheduler( "linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps ) progress_bar = tqdm(range(num_training_steps)) model.train() for epoch in range(num_epochs): for batch in train_dataloader: <span class="hljs-deletion">- batch = {k: v.to(device) for k, v in batch.items()}</span> outputs = model(**batch) loss = outputs.loss <span class="hljs-deletion">- loss.backward()</span> <span class="hljs-addition">+ accelerator.backward(loss)</span> optimizer.step() lr_scheduler.step() optimizer.zero_grad() progress_bar.update(1)</pre></div> <h2 class="relative group"><a id="train" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#train"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Train</span></h2> <p>Once you’ve added the relevant lines of code, launch your training in a script or a notebook like Colaboratory.</p> <h3 class="relative group"><a id="train-with-a-script" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#train-with-a-script"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Train with a script</span></h3> <p>If you are running your training from a script, run the following command to create and save a configuration file:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>accelerate config</pre></div> <p>Then launch your training with:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>accelerate launch train.py</pre></div> <h3 class="relative group"><a id="train-with-a-notebook" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#train-with-a-notebook"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Train with a notebook</span></h3> <p>🤗 Accelerate can also run in a notebook if you’re planning on using Colaboratory’s TPUs. Wrap all the code responsible for training in a function, and pass it to <a href="https://huggingface.co/docs/accelerate/v0.20.3/en/package_reference/launchers#accelerate.notebook_launcher" rel="nofollow">notebook_launcher</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> notebook_launcher <span class="hljs-meta">&gt;&gt;&gt; </span>notebook_launcher(training_function)</pre></div> <p>For more information about 🤗 Accelerate and it’s rich features, refer to the <a href="https://huggingface.co/docs/accelerate" rel="nofollow">documentation</a>.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/run_scripts" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Train with a script</a> <a href="/docs/transformers/model_sharing" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Share your model<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;distributed-training-with-accelerate&quot;,&quot;url&quot;:&quot;#distributed-training-with-accelerate&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Setup&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;setup&quot;,&quot;url&quot;:&quot;#setup&quot;},{&quot;title&quot;:&quot;Prepare to accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;prepare-to-accelerate&quot;,&quot;url&quot;:&quot;#prepare-to-accelerate&quot;},{&quot;title&quot;:&quot;Backward&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;backward&quot;,&quot;url&quot;:&quot;#backward&quot;},{&quot;title&quot;:&quot;Train&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;train&quot;,&quot;url&quot;:&quot;#train&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;train-with-a-script&quot;,&quot;url&quot;:&quot;#train-with-a-script&quot;},{&quot;title&quot;:&quot;Train with a notebook&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;train-with-a-notebook&quot;,&quot;url&quot;:&quot;#train-with-a-notebook&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#distributed-training-with-accelerate" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-distributed-training-with-accelerate"><wbr>Distributed training with 🤗 <wbr>Accelerate</a> <a href="#setup" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-setup"><wbr>Setup</a> <a href="#prepare-to-accelerate" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-prepare-to-accelerate"><wbr>Prepare to accelerate</a> <a href="#backward" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-backward"><wbr>Backward</a> <a href="#train" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-train"><wbr>Train</a> <a href="#train-with-a-script" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-train-with-a-script"><wbr>Train with a script</a> <a href="#train-with-a-notebook" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-train-with-a-notebook"><wbr>Train with a notebook</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/accelerate" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/accelerate"); } </script> <iframe name="__privateStripeMetricsController6890" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Faccelerate&amp;title=Distributed%20training%20with%20%F0%9F%A4%97%20Accelerate&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:08.150Z
Share a model
https://huggingface.co/docs/transformers/model_sharing
The last two tutorials showed how you can fine-tune a model with PyTorch, Keras, and 🤗 Accelerate for distributed setups. The next step is to share your model with the community! At Hugging Face, we believe in openly sharing knowledge and resources to democratize artificial intelligence for everyone. We encourage you to consider sharing your model with the community to help others save time and resources. In this tutorial, you will learn two methods for sharing a trained or fine-tuned model on the [Model Hub](https://huggingface.co/models): - Programmatically push your files to the Hub. - Drag-and-drop your files to the Hub with the web interface. To share a model with the community, you need an account on [huggingface.co](https://huggingface.co/join). You can also join an existing organization or create a new one. ## [](#repository-features)Repository features Each repository on the Model Hub behaves like a typical GitHub repository. Our repositories offer versioning, commit history, and the ability to visualize differences. The Model Hub’s built-in versioning is based on git and [git-lfs](https://git-lfs.github.com/). In other words, you can treat one model as one repository, enabling greater access control and scalability. Version control allows _revisions_, a method for pinning a specific version of a model with a commit hash, tag or branch. As a result, you can load a specific model version with the `revision` parameter: ``` >>> model = AutoModel.from_pretrained( ... "julien-c/EsperBERTo-small", revision="v2.0.1" ... )``` Files are also easily edited in a repository, and you can view the commit history as well as the difference: ![vis_diff](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vis_diff.png) ## [](#setup)Setup Before sharing a model to the Hub, you will need your Hugging Face credentials. If you have access to a terminal, run the following command in the virtual environment where 🤗 Transformers is installed. This will store your access token in your Hugging Face cache folder (`~/.cache/` by default): If you are using a notebook like Jupyter or Colaboratory, make sure you have the [`huggingface_hub`](https://huggingface.co/docs/hub/adding-a-library) library installed. This library allows you to programmatically interact with the Hub. ``` pip install huggingface_hub``` Then use `notebook_login` to sign-in to the Hub, and follow the link [here](https://huggingface.co/settings/token) to generate a token to login with: ``` >>> from huggingface_hub import notebook_login >>> notebook_login()``` ## [](#convert-a-model-for-all-frameworks)Convert a model for all frameworks To ensure your model can be used by someone working with a different framework, we recommend you convert and upload your model with both PyTorch and TensorFlow checkpoints. While users are still able to load your model from a different framework if you skip this step, it will be slower because 🤗 Transformers will need to convert the checkpoint on-the-fly. Converting a checkpoint for another framework is easy. Make sure you have PyTorch and TensorFlow installed (see [here](installation) for installation instructions), and then find the specific model for your task in the other framework. Specify `from_tf=True` to convert a checkpoint from TensorFlow to PyTorch: ``` >>> pt_model = DistilBertForSequenceClassification.from_pretrained("path/to/awesome-name-you-picked", from_tf=True) >>> pt_model.save_pretrained("path/to/awesome-name-you-picked")``` Specify `from_pt=True` to convert a checkpoint from PyTorch to TensorFlow: ``` >>> tf_model = TFDistilBertForSequenceClassification.from_pretrained("path/to/awesome-name-you-picked", from_pt=True)``` Then you can save your new TensorFlow model with it’s new checkpoint: ``` >>> tf_model.save_pretrained("path/to/awesome-name-you-picked")``` If a model is available in Flax, you can also convert a checkpoint from PyTorch to Flax: ``` >>> flax_model = FlaxDistilBertForSequenceClassification.from_pretrained( ... "path/to/awesome-name-you-picked", from_pt=True ... )``` ## [](#push-a-model-during-training)Push a model during training Sharing a model to the Hub is as simple as adding an extra parameter or callback. Remember from the [fine-tuning tutorial](training), the [TrainingArguments](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.TrainingArguments) class is where you specify hyperparameters and additional training options. One of these training options includes the ability to push a model directly to the Hub. Set `push_to_hub=True` in your [TrainingArguments](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.TrainingArguments): ``` >>> training_args = TrainingArguments(output_dir="my-awesome-model", push_to_hub=True)``` Pass your training arguments as usual to [Trainer](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer): ``` >>> trainer = Trainer( ... model=model, ... args=training_args, ... train_dataset=small_train_dataset, ... eval_dataset=small_eval_dataset, ... compute_metrics=compute_metrics, ... )``` After you fine-tune your model, call [push\_to\_hub()](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer.push_to_hub) on [Trainer](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer) to push the trained model to the Hub. 🤗 Transformers will even automatically add training hyperparameters, training results and framework versions to your model card! ``` >>> trainer.push_to_hub()``` Share a model to the Hub with [PushToHubCallback](/docs/transformers/v4.30.0/en/main_classes/keras_callbacks#transformers.PushToHubCallback). In the [PushToHubCallback](/docs/transformers/v4.30.0/en/main_classes/keras_callbacks#transformers.PushToHubCallback) function, add: - An output directory for your model. - A tokenizer. - The `hub_model_id`, which is your Hub username and model name. ``` >>> from transformers import PushToHubCallback >>> push_to_hub_callback = PushToHubCallback( ... output_dir="./your_model_save_path", tokenizer=tokenizer, hub_model_id="your-username/my-awesome-model" ... )``` Add the callback to [`fit`](https://keras.io/api/models/model_training_apis/), and 🤗 Transformers will push the trained model to the Hub: ``` >>> model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=3, callbacks=push_to_hub_callback)``` ## [](#use-the-pushtohub-function)Use the `push_to_hub` function You can also call `push_to_hub` directly on your model to upload it to the Hub. Specify your model name in `push_to_hub`: ``` >>> pt_model.push_to_hub("my-awesome-model")``` This creates a repository under your username with the model name `my-awesome-model`. Users can now load your model with the `from_pretrained` function: ``` >>> from transformers import AutoModel >>> model = AutoModel.from_pretrained("your_username/my-awesome-model")``` If you belong to an organization and want to push your model under the organization name instead, just add it to the `repo_id`: ``` >>> pt_model.push_to_hub("my-awesome-org/my-awesome-model")``` The `push_to_hub` function can also be used to add other files to a model repository. For example, add a tokenizer to a model repository: ``` >>> tokenizer.push_to_hub("my-awesome-model")``` Or perhaps you’d like to add the TensorFlow version of your fine-tuned PyTorch model: ``` >>> tf_model.push_to_hub("my-awesome-model")``` Now when you navigate to the your Hugging Face profile, you should see your newly created model repository. Clicking on the **Files** tab will display all the files you’ve uploaded to the repository. For more details on how to create and upload files to a repository, refer to the Hub documentation [here](https://huggingface.co/docs/hub/how-to-upstream). ## [](#upload-with-the-web-interface)Upload with the web interface Users who prefer a no-code approach are able to upload a model through the Hub’s web interface. Visit [huggingface.co/new](https://huggingface.co/new) to create a new repository: ![new_model_repo](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/new_model_repo.png) From here, add some information about your model: - Select the **owner** of the repository. This can be yourself or any of the organizations you belong to. - Pick a name for your model, which will also be the repository name. - Choose whether your model is public or private. - Specify the license usage for your model. Now click on the **Files** tab and click on the **Add file** button to upload a new file to your repository. Then drag-and-drop a file to upload and add a commit message. ![upload_file](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/upload_file.png) ## [](#add-a-model-card)Add a model card To make sure users understand your model’s capabilities, limitations, potential biases and ethical considerations, please add a model card to your repository. The model card is defined in the `README.md` file. You can add a model card by: - Manually creating and uploading a `README.md` file. - Clicking on the **Edit model card** button in your model repository. Take a look at the DistilBert [model card](https://huggingface.co/distilbert-base-uncased) for a good example of the type of information a model card should include. For more details about other options you can control in the `README.md` file such as a model’s carbon footprint or widget examples, refer to the documentation [here](https://huggingface.co/docs/hub/models-cards).
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Share a model"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/model_sharing"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Share a model</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;share-a-model&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;repository-features&quot;,&quot;title&quot;:&quot;Repository features&quot;},{&quot;local&quot;:&quot;setup&quot;,&quot;title&quot;:&quot;Setup&quot;},{&quot;local&quot;:&quot;convert-a-model-for-all-frameworks&quot;,&quot;title&quot;:&quot;Convert a model for all frameworks&quot;},{&quot;local&quot;:&quot;push-a-model-during-training&quot;,&quot;title&quot;:&quot;Push a model during training&quot;},{&quot;local&quot;:&quot;use-the-pushtohub-function&quot;,&quot;title&quot;:&quot;Use the `push_to_hub` function&quot;},{&quot;local&quot;:&quot;upload-with-the-web-interface&quot;,&quot;title&quot;:&quot;Upload with the web interface&quot;},{&quot;local&quot;:&quot;add-a-model-card&quot;,&quot;title&quot;:&quot;Add a model card&quot;}],&quot;title&quot;:&quot;Share a model&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;model_sharing&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Share a model&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Share a model</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="share-a-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#share-a-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Share a model</span></h1> <p>The last two tutorials showed how you can fine-tune a model with PyTorch, Keras, and 🤗 Accelerate for distributed setups. The next step is to share your model with the community! At Hugging Face, we believe in openly sharing knowledge and resources to democratize artificial intelligence for everyone. We encourage you to consider sharing your model with the community to help others save time and resources.</p> <p>In this tutorial, you will learn two methods for sharing a trained or fine-tuned model on the <a href="https://huggingface.co/models" rel="nofollow">Model Hub</a>:</p> <ul><li>Programmatically push your files to the Hub.</li> <li>Drag-and-drop your files to the Hub with the web interface.</li></ul> <iframe width="560" height="315" src="https://www.youtube.com/embed/XvSGPZFEjDY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>To share a model with the community, you need an account on <a href="https://huggingface.co/join" rel="nofollow">huggingface.co</a>. You can also join an existing organization or create a new one.</p></div> <h2 class="relative group"><a id="repository-features" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#repository-features"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Repository features</span></h2> <p>Each repository on the Model Hub behaves like a typical GitHub repository. Our repositories offer versioning, commit history, and the ability to visualize differences.</p> <p>The Model Hub’s built-in versioning is based on git and <a href="https://git-lfs.github.com/" rel="nofollow">git-lfs</a>. In other words, you can treat one model as one repository, enabling greater access control and scalability. Version control allows <em>revisions</em>, a method for pinning a specific version of a model with a commit hash, tag or branch.</p> <p>As a result, you can load a specific model version with the <code>revision</code> parameter:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModel.from_pretrained( <span class="hljs-meta">... </span> <span class="hljs-string">"julien-c/EsperBERTo-small"</span>, revision=<span class="hljs-string">"v2.0.1"</span> <span class="hljs-comment"># tag name, or branch name, or commit hash</span> <span class="hljs-meta">... </span>)</pre></div> <p>Files are also easily edited in a repository, and you can view the commit history as well as the difference:</p> <p><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vis_diff.png" alt="vis_diff"></p> <h2 class="relative group"><a id="setup" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#setup"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Setup</span></h2> <p>Before sharing a model to the Hub, you will need your Hugging Face credentials. If you have access to a terminal, run the following command in the virtual environment where 🤗 Transformers is installed. This will store your access token in your Hugging Face cache folder (<code>~/.cache/</code> by default):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-cli login</pre></div> <p>If you are using a notebook like Jupyter or Colaboratory, make sure you have the <a href="https://huggingface.co/docs/hub/adding-a-library" rel="nofollow"><code>huggingface_hub</code></a> library installed. This library allows you to programmatically interact with the Hub.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install huggingface_hub</pre></div> <p>Then use <code>notebook_login</code> to sign-in to the Hub, and follow the link <a href="https://huggingface.co/settings/token" rel="nofollow">here</a> to generate a token to login with:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login <span class="hljs-meta">&gt;&gt;&gt; </span>notebook_login()</pre></div> <h2 class="relative group"><a id="convert-a-model-for-all-frameworks" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#convert-a-model-for-all-frameworks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Convert a model for all frameworks</span></h2> <p>To ensure your model can be used by someone working with a different framework, we recommend you convert and upload your model with both PyTorch and TensorFlow checkpoints. While users are still able to load your model from a different framework if you skip this step, it will be slower because 🤗 Transformers will need to convert the checkpoint on-the-fly.</p> <p>Converting a checkpoint for another framework is easy. Make sure you have PyTorch and TensorFlow installed (see <a href="installation">here</a> for installation instructions), and then find the specific model for your task in the other framework.</p> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><p>Specify <code>from_tf=True</code> to convert a checkpoint from TensorFlow to PyTorch:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>pt_model = DistilBertForSequenceClassification.from_pretrained(<span class="hljs-string">"path/to/awesome-name-you-picked"</span>, from_tf=<span class="hljs-literal">True</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>pt_model.save_pretrained(<span class="hljs-string">"path/to/awesome-name-you-picked"</span>)</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><p>Specify <code>from_pt=True</code> to convert a checkpoint from PyTorch to TensorFlow:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tf_model = TFDistilBertForSequenceClassification.from_pretrained(<span class="hljs-string">"path/to/awesome-name-you-picked"</span>, from_pt=<span class="hljs-literal">True</span>)</pre></div> <p>Then you can save your new TensorFlow model with it’s new checkpoint:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tf_model.save_pretrained(<span class="hljs-string">"path/to/awesome-name-you-picked"</span>)</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.73em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 451 260.81"><style>.J { stroke: #dce0df; } .K { stroke-linejoin: round; } </style><g fill="#5e97f6" class="J K"><path d="M50.5 130.4l-25 43.31h50l25-43.31h-50z"></path><path d="M.5 217.01l25-43.3h50l-25 43.3H.5z"></path><path d="M125.5 173.71h-50l-25 43.3h50l25-43.3z"></path><path d="M175.5 173.71h-50l-25 43.3h50l25-43.3z"></path><path d="M150.5 130.4l-25 43.31h50l25-43.31h-50z"></path><path d="M175.5 87.1l-25 43.3h50l25-43.3h-50z"></path><path d="M200.5 43.8l-25 43.3h50l25-43.3h-50z"></path><path d="M225.5.5l-25 43.3h50l25-43.3h-50z"></path></g><g fill="#2a56c6" class="J K"><path d="M.5 217.01l25 43.3h50l-25-43.3H.5z"></path><path d="M125.5 260.31h-50l-25-43.3h50l25 43.3z"></path><path d="M175.5 260.31h-50l-25-43.3h50l25 43.3z"></path></g><g fill="#00796b" class="J K"><path d="M200.5 217.01l-25-43.3-25 43.3 25 43.3 25-43.3zm50-86.61l-25-43.3-25 43.3h50z"></path><path d="M250.5 43.8l-25 43.3 25 43.3 25-43.3-25-43.3z"></path></g><path d="M125.5 173.71l-25-43.31-25 43.31h50z" fill="#3367d6" class="J K"></path><g fill="#26a69a" class="J K"><path d="M250.5 130.4h-50l-25 43.31h50l25-43.31z"></path><path d="M300.5 130.4h-50l-25 43.31h50l25-43.31z"></path></g><g fill="#9c27b0" class="J K"><path d="M350.5 43.8L325.5.5l-25 43.3 25 43.3 25-43.3z"></path><path d="M375.5 87.1l-25-43.3-25 43.3 25 43.3 25-43.3z"></path><path d="M400.5 130.4l-25-43.3-25 43.3 25 43.31 25-43.31z"></path><path d="M425.5 173.71l-25-43.31-25 43.31 25 43.3 25-43.3z"></path><path d="M450.5 217.01l-25-43.3-25 43.3 25 43.3 25-43.3zM425.5.5l-25 43.3 25 43.3 25-43.3-25-43.3z"></path><path d="M375.5 87.1l25-43.3 25 43.3-25 43.3-25-43.3zm-25 43.3l-25 43.31 25 43.3 25-43.3-25-43.31z"></path><path d="M325.5 260.31l-25-43.3 25-43.3 25 43.3-25 43.3z"></path></g><path d="M275.5 260.31l-25-43.3h50l25 43.3h-50z" fill="#6a1b9a" class="J K"></path><g fill="#00695c" class="J K"><path d="M225.5 173.71h-50l25 43.3h50l-25-43.3z"></path><path d="M275.5 173.71h-50l25 43.3 25-43.3zm0-86.61l25 43.3h50l-25-43.3h-50z"></path><path d="M300.5 43.8h-50l25 43.3h50l-25-43.3zm125 216.51l-25-43.3h-50l25 43.3h50z"></path><path d="M375.5 173.71l-25 43.3h50l-25-43.3z"></path></g><g fill="#ea80fc" class="J K"><path d="M325.5.5h-50l-25 43.3h50l25-43.3zm0 173.21h-50l-25 43.3h50l25-43.3z"></path><path d="M350.5 130.4h-50l-25 43.31h50l25-43.31zM425.5.5h-50l-25 43.3h50l25-43.3z"></path><path d="M375.5 87.1l-25-43.3h50l-25 43.3z"></path></g></svg> <span>JAX</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide JAX content</span></div></div> <div class="framework-content"><p>If a model is available in Flax, you can also convert a checkpoint from PyTorch to Flax:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>flax_model = FlaxDistilBertForSequenceClassification.from_pretrained( <span class="hljs-meta">... </span> <span class="hljs-string">"path/to/awesome-name-you-picked"</span>, from_pt=<span class="hljs-literal">True</span> <span class="hljs-meta">... </span>)</pre></div></div></div></div> <h2 class="relative group"><a id="push-a-model-during-training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#push-a-model-during-training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Push a model during training</span></h2> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><iframe class="w-full xl:w-4/6 h-80" src="https://www.youtube-nocookie.com/embed/Z1-XMy-GNLQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p>Sharing a model to the Hub is as simple as adding an extra parameter or callback. Remember from the <a href="training">fine-tuning tutorial</a>, the <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a> class is where you specify hyperparameters and additional training options. One of these training options includes the ability to push a model directly to the Hub. Set <code>push_to_hub=True</code> in your <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>training_args = TrainingArguments(output_dir=<span class="hljs-string">"my-awesome-model"</span>, push_to_hub=<span class="hljs-literal">True</span>)</pre></div> <p>Pass your training arguments as usual to <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>trainer = Trainer( <span class="hljs-meta">... </span> model=model, <span class="hljs-meta">... </span> args=training_args, <span class="hljs-meta">... </span> train_dataset=small_train_dataset, <span class="hljs-meta">... </span> eval_dataset=small_eval_dataset, <span class="hljs-meta">... </span> compute_metrics=compute_metrics, <span class="hljs-meta">... </span>)</pre></div> <p>After you fine-tune your model, call <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer.push_to_hub">push_to_hub()</a> on <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a> to push the trained model to the Hub. 🤗 Transformers will even automatically add training hyperparameters, training results and framework versions to your model card!</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>trainer.push_to_hub()</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><p>Share a model to the Hub with <a href="/docs/transformers/v4.30.0/en/main_classes/keras_callbacks#transformers.PushToHubCallback">PushToHubCallback</a>. In the <a href="/docs/transformers/v4.30.0/en/main_classes/keras_callbacks#transformers.PushToHubCallback">PushToHubCallback</a> function, add:</p> <ul><li>An output directory for your model.</li> <li>A tokenizer.</li> <li>The <code>hub_model_id</code>, which is your Hub username and model name.</li></ul> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PushToHubCallback <span class="hljs-meta">&gt;&gt;&gt; </span>push_to_hub_callback = PushToHubCallback( <span class="hljs-meta">... </span> output_dir=<span class="hljs-string">"./your_model_save_path"</span>, tokenizer=tokenizer, hub_model_id=<span class="hljs-string">"your-username/my-awesome-model"</span> <span class="hljs-meta">... </span>)</pre></div> <p>Add the callback to <a href="https://keras.io/api/models/model_training_apis/" rel="nofollow"><code>fit</code></a>, and 🤗 Transformers will push the trained model to the Hub:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=<span class="hljs-number">3</span>, callbacks=push_to_hub_callback)</pre></div></div></div> </div> <h2 class="relative group"><a id="use-the-pushtohub-function" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#use-the-pushtohub-function"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Use the <code>push_to_hub</code> function</span></h2> <p>You can also call <code>push_to_hub</code> directly on your model to upload it to the Hub.</p> <p>Specify your model name in <code>push_to_hub</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>pt_model.push_to_hub(<span class="hljs-string">"my-awesome-model"</span>)</pre></div> <p>This creates a repository under your username with the model name <code>my-awesome-model</code>. Users can now load your model with the <code>from_pretrained</code> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModel <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModel.from_pretrained(<span class="hljs-string">"your_username/my-awesome-model"</span>)</pre></div> <p>If you belong to an organization and want to push your model under the organization name instead, just add it to the <code>repo_id</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>pt_model.push_to_hub(<span class="hljs-string">"my-awesome-org/my-awesome-model"</span>)</pre></div> <p>The <code>push_to_hub</code> function can also be used to add other files to a model repository. For example, add a tokenizer to a model repository:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.push_to_hub(<span class="hljs-string">"my-awesome-model"</span>)</pre></div> <p>Or perhaps you’d like to add the TensorFlow version of your fine-tuned PyTorch model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tf_model.push_to_hub(<span class="hljs-string">"my-awesome-model"</span>)</pre></div> <p>Now when you navigate to the your Hugging Face profile, you should see your newly created model repository. Clicking on the <strong>Files</strong> tab will display all the files you’ve uploaded to the repository.</p> <p>For more details on how to create and upload files to a repository, refer to the Hub documentation <a href="https://huggingface.co/docs/hub/how-to-upstream" rel="nofollow">here</a>.</p> <h2 class="relative group"><a id="upload-with-the-web-interface" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#upload-with-the-web-interface"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Upload with the web interface</span></h2> <p>Users who prefer a no-code approach are able to upload a model through the Hub’s web interface. Visit <a href="https://huggingface.co/new" rel="nofollow">huggingface.co/new</a> to create a new repository:</p> <p><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/new_model_repo.png" alt="new_model_repo"></p> <p>From here, add some information about your model:</p> <ul><li>Select the <strong>owner</strong> of the repository. This can be yourself or any of the organizations you belong to.</li> <li>Pick a name for your model, which will also be the repository name.</li> <li>Choose whether your model is public or private.</li> <li>Specify the license usage for your model.</li></ul> <p>Now click on the <strong>Files</strong> tab and click on the <strong>Add file</strong> button to upload a new file to your repository. Then drag-and-drop a file to upload and add a commit message.</p> <p><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/upload_file.png" alt="upload_file"></p> <h2 class="relative group"><a id="add-a-model-card" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#add-a-model-card"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Add a model card</span></h2> <p>To make sure users understand your model’s capabilities, limitations, potential biases and ethical considerations, please add a model card to your repository. The model card is defined in the <code>README.md</code> file. You can add a model card by:</p> <ul><li>Manually creating and uploading a <code>README.md</code> file.</li> <li>Clicking on the <strong>Edit model card</strong> button in your model repository.</li></ul> <p>Take a look at the DistilBert <a href="https://huggingface.co/distilbert-base-uncased" rel="nofollow">model card</a> for a good example of the type of information a model card should include. For more details about other options you can control in the <code>README.md</code> file such as a model’s carbon footprint or widget examples, refer to the documentation <a href="https://huggingface.co/docs/hub/models-cards" rel="nofollow">here</a>.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/accelerate" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Set up distributed training with 🤗 Accelerate</a> <a href="/docs/transformers/transformers_agents" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Agents<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Share a model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;share-a-model&quot;,&quot;url&quot;:&quot;#share-a-model&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Repository features&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;repository-features&quot;,&quot;url&quot;:&quot;#repository-features&quot;},{&quot;title&quot;:&quot;Setup&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;setup&quot;,&quot;url&quot;:&quot;#setup&quot;},{&quot;title&quot;:&quot;Convert a model for all frameworks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;convert-a-model-for-all-frameworks&quot;,&quot;url&quot;:&quot;#convert-a-model-for-all-frameworks&quot;},{&quot;title&quot;:&quot;Push a model during training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;push-a-model-during-training&quot;,&quot;url&quot;:&quot;#push-a-model-during-training&quot;},{&quot;title&quot;:&quot;Use the `push_to_hub` function&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;use-the-pushtohub-function&quot;,&quot;url&quot;:&quot;#use-the-pushtohub-function&quot;},{&quot;title&quot;:&quot;Upload with the web interface&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;upload-with-the-web-interface&quot;,&quot;url&quot;:&quot;#upload-with-the-web-interface&quot;},{&quot;title&quot;:&quot;Add a model card&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add-a-model-card&quot;,&quot;url&quot;:&quot;#add-a-model-card&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#share-a-model" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-share-a-model"><wbr>Share a model</a> <a href="#repository-features" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-repository-features"><wbr>Repository features</a> <a href="#setup" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-setup"><wbr>Setup</a> <a href="#convert-a-model-for-all-frameworks" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-convert-a-model-for-all-frameworks"><wbr>Convert a model for all frameworks</a> <a href="#push-a-model-during-training" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-push-a-model-during-training"><wbr>Push a model during training</a> <a href="#use-the-pushtohub-function" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-use-the-pushtohub-function"><wbr>Use the `push_to_hub` function</a> <a href="#upload-with-the-web-interface" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-upload-with-the-web-interface"><wbr>Upload with the web interface</a> <a href="#add-a-model-card" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-add-a-model-card"><wbr>Add a model card</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/model_sharing" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/model_sharing"); } </script> <iframe name="__privateStripeMetricsController6770" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fmodel_sharing&amp;title=Share%20a%20model&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:08.698Z
Transformers Agent
https://huggingface.co/docs/transformers/transformers_agents
## [](#transformers-agent)Transformers Agent Transformers Agent is an experimental API which is subject to change at any time. Results returned by the agents can vary as the APIs or underlying models are prone to change. Transformers version v4.29.0, building on the concept of _tools_ and _agents_. You can play with in [this colab](https://colab.research.google.com/drive/1c7MHD-T1forUPGcC_jlwsIptOzpG3hSj). In short, it provides a natural language API on top of transformers: we define a set of curated tools and design an agent to interpret natural language and to use these tools. It is extensible by design; we curated some relevant tools, but we’ll show you how the system can be extended easily to use any tool developed by the community. Let’s start with a few examples of what can be achieved with this new API. It is particularly powerful when it comes to multimodal tasks, so let’s take it for a spin to generate images and read text out loud. ``` agent.run("Caption the following image", image=image)``` | **Input** | **Output** | | --- | --- | | ![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/beaver.png) | A beaver is swimming in the water | --- ``` agent.run("Read the following text out loud", text=text)``` | **Input** | **Output** | | --- | --- | | A beaver is swimming in the water | your browser does not support the audio element. </audio> | --- ``` agent.run( "In the following `document`, where will the TRRF Scientific Advisory Council Meeting take place?", document=document, )``` | **Input** | **Output** | | --- | --- | | ![](https://datasets-server.huggingface.co/assets/hf-internal-testing/example-documents/--/hf-internal-testing--example-documents/test/0/image/image.jpg) | ballroom foyer | ## [](#quickstart)Quickstart Before being able to use `agent.run`, you will need to instantiate an agent, which is a large language model (LLM). We provide support for openAI models as well as opensource alternatives from BigCode and OpenAssistant. The openAI models perform better (but require you to have an openAI API key, so cannot be used for free); Hugging Face is providing free access to endpoints for BigCode and OpenAssistant models. To start with, please install the `agents` extras in order to install all default dependencies. ``` pip install transformers[agents]``` To use openAI models, you instantiate an [OpenAiAgent](/docs/transformers/v4.30.0/en/main_classes/agent#transformers.OpenAiAgent) after installing the `openai` dependency: ``` from transformers import OpenAiAgent agent = OpenAiAgent(model="text-davinci-003", api_key="<your_api_key>")``` To use BigCode or OpenAssistant, start by logging in to have access to the Inference API: ``` from huggingface_hub import login login("<YOUR_TOKEN>")``` Then, instantiate the agent ``` from transformers import HfAgent agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder") ``` This is using the inference API that Hugging Face provides for free at the moment. If you have your own inference endpoint for this model (or another one) you can replace the URL above with your URL endpoint. StarCoder and OpenAssistant are free to use and perform admirably well on simple tasks. However, the checkpoints don’t hold up when handling more complex prompts. If you’re facing such an issue, we recommend trying out the OpenAI model which, while sadly not open-source, performs better at this given time. You’re now good to go! Let’s dive into the two APIs that you now have at your disposal. ### [](#single-execution-run)Single execution (run) The single execution method is when using the [run()](/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.run) method of the agent: ``` agent.run("Draw me a picture of rivers and lakes.")``` ![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes.png) It automatically selects the tool (or tools) appropriate for the task you want to perform and runs them appropriately. It can perform one or several tasks in the same instruction (though the more complex your instruction, the more likely the agent is to fail). ``` agent.run("Draw me a picture of the sea then transform the picture to add an island")``` ![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/sea_and_island.png) Every [run()](/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.run) operation is independent, so you can run it several times in a row with different tasks. Note that your `agent` is just a large-language model, so small variations in your prompt might yield completely different results. It’s important to explain as clearly as possible the task you want to perform. We go more in-depth on how to write good prompts [here](custom_tools#writing-good-user-inputs). If you’d like to keep a state across executions or to pass non-text objects to the agent, you can do so by specifying variables that you would like the agent to use. For example, you could generate the first image of rivers and lakes, and ask the model to update that picture to add an island by doing the following: ``` picture = agent.run("Generate a picture of rivers and lakes.") updated_picture = agent.run("Transform the image in `picture` to add an island to it.", picture=picture)``` This can be helpful when the model is unable to understand your request and mixes tools. An example would be: ``` agent.run("Draw me the picture of a capybara swimming in the sea")``` Here, the model could interpret in two ways: - Have the `text-to-image` generate a capybara swimming in the sea - Or, have the `text-to-image` generate capybara, then use the `image-transformation` tool to have it swim in the sea In case you would like to force the first scenario, you could do so by passing it the prompt as an argument: ``` agent.run("Draw me a picture of the `prompt`", prompt="a capybara swimming in the sea")``` ### [](#chatbased-execution-chat)Chat-based execution (chat) The agent also has a chat-based approach, using the [chat()](/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.chat) method: ``` agent.chat("Generate a picture of rivers and lakes")``` ![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes.png) ``` agent.chat("Transform the picture so that there is a rock in there")``` ![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes_and_beaver.png) This is an interesting approach when you want to keep the state across instructions. It’s better for experimentation, but will tend to be much better at single instructions rather than complex instructions (which the [run()](/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.run) method is better at handling). This method can also take arguments if you would like to pass non-text types or specific prompts. ### [](#remote-execution)⚠️ Remote execution For demonstration purposes and so that this can be used with all setups, we have created remote executors for several of the default tools the agent has access. These are created using [inference endpoints](https://huggingface.co/inference-endpoints). To see how to set up remote executors tools yourself, we recommend reading the [custom tool guide](./custom_tools). In order to run with remote tools, specifying `remote=True` to either [run()](/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.run) or [chat()](/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.chat) is sufficient. For example, the following command could be run on any device efficiently, without needing significant RAM or GPU: ``` agent.run("Draw me a picture of rivers and lakes", remote=True)``` The same can be said for [chat()](/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.chat): ``` agent.chat("Draw me a picture of rivers and lakes", remote=True)``` ### [](#whats-happening-here-what-are-tools-and-what-are-agents)What's happening here? What are tools, and what are agents? ![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/diagram.png) #### [](#agents)Agents The “agent” here is a large language model, and we’re prompting it so that it has access to a specific set of tools. LLMs are pretty good at generating small samples of code, so this API takes advantage of that by prompting the LLM gives a small sample of code performing a task with a set of tools. This prompt is then completed by the task you give your agent and the description of the tools you give it. This way it gets access to the doc of the tools you are using, especially their expected inputs and outputs, and can generate the relevant code. #### [](#tools)Tools Tools are very simple: they’re a single function, with a name, and a description. We then use these tools’ descriptions to prompt the agent. Through the prompt, we show the agent how it would leverage tools to perform what was requested in the query. This is using brand-new tools and not pipelines, because the agent writes better code with very atomic tools. Pipelines are more refactored and often combine several tasks in one. Tools are meant to be focused on one very simple task only. #### [](#codeexecution)Code-execution?! This code is then executed with our small Python interpreter on the set of inputs passed along with your tools. We hear you screaming “Arbitrary code execution!” in the back, but let us explain why that is not the case. The only functions that can be called are the tools you provided and the print function, so you’re already limited in what can be executed. You should be safe if it’s limited to Hugging Face tools. Then, we don’t allow any attribute lookup or imports (which shouldn’t be needed anyway for passing along inputs/outputs to a small set of functions) so all the most obvious attacks (and you’d need to prompt the LLM to output them anyway) shouldn’t be an issue. If you want to be on the super safe side, you can execute the run() method with the additional argument return\_code=True, in which case the agent will just return the code to execute and you can decide whether to do it or not. The execution will stop at any line trying to perform an illegal operation or if there is a regular Python error with the code generated by the agent. ### [](#a-curated-set-of-tools)A curated set of tools We identify a set of tools that can empower such agents. Here is an updated list of the tools we have integrated in `transformers`: - **Document question answering**: given a document (such as a PDF) in image format, answer a question on this document ([Donut](./model_doc/donut)) - **Text question answering**: given a long text and a question, answer the question in the text ([Flan-T5](./model_doc/flan-t5)) - **Unconditional image captioning**: Caption the image! ([BLIP](./model_doc/blip)) - **Image question answering**: given an image, answer a question on this image ([VILT](./model_doc/vilt)) - **Image segmentation**: given an image and a prompt, output the segmentation mask of that prompt ([CLIPSeg](./model_doc/clipseg)) - **Speech to text**: given an audio recording of a person talking, transcribe the speech into text ([Whisper](./model_doc/whisper)) - **Text to speech**: convert text to speech ([SpeechT5](./model_doc/speecht5)) - **Zero-shot text classification**: given a text and a list of labels, identify to which label the text corresponds the most ([BART](./model_doc/bart)) - **Text summarization**: summarize a long text in one or a few sentences ([BART](./model_doc/bart)) - **Translation**: translate the text into a given language ([NLLB](./model_doc/nllb)) These tools have an integration in transformers, and can be used manually as well, for example: ``` from transformers import load_tool tool = load_tool("text-to-speech") audio = tool("This is a text to speech tool")``` ### [](#custom-tools)Custom tools While we identify a curated set of tools, we strongly believe that the main value provided by this implementation is the ability to quickly create and share custom tools. By pushing the code of a tool to a Hugging Face Space or a model repository, you’re then able to leverage the tool directly with the agent. We’ve added a few **transformers-agnostic** tools to the [`huggingface-tools` organization](https://huggingface.co/huggingface-tools): - **Text downloader**: to download a text from a web URL - **Text to image**: generate an image according to a prompt, leveraging stable diffusion - **Image transformation**: modify an image given an initial image and a prompt, leveraging instruct pix2pix stable diffusion - **Text to video**: generate a small video according to a prompt, leveraging damo-vilab The text-to-image tool we have been using since the beginning is a remote tool that lives in [_huggingface-tools/text-to-image_](https://huggingface.co/spaces/huggingface-tools/text-to-image)! We will continue releasing such tools on this and other organizations, to further supercharge this implementation. The agents have by default access to tools that reside on [`huggingface-tools`](https://huggingface.co/huggingface-tools). We explain how to you can write and share your tools as well as leverage any custom tool that resides on the Hub in [following guide](custom_tools). ### [](#code-generation)Code generation So far we have shown how to use the agents to perform actions for you. However, the agent is only generating code that we then execute using a very restricted Python interpreter. In case you would like to use the code generated in a different setting, the agent can be prompted to return the code, along with tool definition and accurate imports. For example, the following instruction ``` agent.run("Draw me a picture of rivers and lakes", return_code=True)``` returns the following code ``` from transformers import load_tool image_generator = load_tool("huggingface-tools/text-to-image") image = image_generator(prompt="rivers and lakes")``` that you can then modify and execute yourself.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Transformers Agent"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/transformers_agents"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Transformers Agent</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;transformers-agent&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;quickstart&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;single-execution-run&quot;,&quot;title&quot;:&quot;Single execution (run)&quot;},{&quot;local&quot;:&quot;chatbased-execution-chat&quot;,&quot;title&quot;:&quot;Chat-based execution (chat)&quot;},{&quot;local&quot;:&quot;remote-execution&quot;,&quot;title&quot;:&quot;⚠️ Remote execution&quot;},{&quot;local&quot;:&quot;whats-happening-here-what-are-tools-and-what-are-agents&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;agents&quot;,&quot;title&quot;:&quot;Agents&quot;},{&quot;local&quot;:&quot;tools&quot;,&quot;title&quot;:&quot;Tools&quot;},{&quot;local&quot;:&quot;codeexecution&quot;,&quot;title&quot;:&quot;Code-execution?!&quot;}],&quot;title&quot;:&quot;What's happening here? What are tools, and what are agents?&quot;},{&quot;local&quot;:&quot;a-curated-set-of-tools&quot;,&quot;title&quot;:&quot;A curated set of tools&quot;},{&quot;local&quot;:&quot;custom-tools&quot;,&quot;title&quot;:&quot;Custom tools&quot;},{&quot;local&quot;:&quot;code-generation&quot;,&quot;title&quot;:&quot;Code generation&quot;}],&quot;title&quot;:&quot;Quickstart&quot;}],&quot;title&quot;:&quot;Transformers Agent&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;transformers_agents&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Transformers Agent&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Transformers Agent</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="transformers-agent" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers-agent"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Transformers Agent</span></h1> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>Transformers Agent is an experimental API which is subject to change at any time. Results returned by the agents can vary as the APIs or underlying models are prone to change.</p></div> <p>Transformers version v4.29.0, building on the concept of <em>tools</em> and <em>agents</em>. You can play with in <a href="https://colab.research.google.com/drive/1c7MHD-T1forUPGcC_jlwsIptOzpG3hSj" rel="nofollow">this colab</a>.</p> <p>In short, it provides a natural language API on top of transformers: we define a set of curated tools and design an agent to interpret natural language and to use these tools. It is extensible by design; we curated some relevant tools, but we’ll show you how the system can be extended easily to use any tool developed by the community.</p> <p>Let’s start with a few examples of what can be achieved with this new API. It is particularly powerful when it comes to multimodal tasks, so let’s take it for a spin to generate images and read text out loud.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>agent.run(<span class="hljs-string">"Caption the following image"</span>, image=image)</pre></div> <table><thead><tr><th><strong>Input</strong></th> <th><strong>Output</strong></th></tr></thead> <tbody><tr><td><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/beaver.png" width="200"></td> <td>A beaver is swimming in the water</td></tr></tbody></table> <hr> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>agent.run(<span class="hljs-string">"Read the following text out loud"</span>, text=text)</pre></div> <table><thead><tr><th><strong>Input</strong></th> <th><strong>Output</strong></th></tr></thead> <tbody><tr><td>A beaver is swimming in the water</td> <td><audio controls=""><source src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tts_example.wav" type="audio/wav"> your browser does not support the audio element. &lt;/audio&gt;</audio></td></tr></tbody></table> <hr> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>agent.run( <span class="hljs-string">"In the following `document`, where will the TRRF Scientific Advisory Council Meeting take place?"</span>, document=document, )</pre></div> <table><thead><tr><th><strong>Input</strong></th> <th><strong>Output</strong></th></tr></thead> <tbody><tr><td><img src="https://datasets-server.huggingface.co/assets/hf-internal-testing/example-documents/--/hf-internal-testing--example-documents/test/0/image/image.jpg" width="200"></td> <td>ballroom foyer</td></tr></tbody></table> <h2 class="relative group"><a id="quickstart" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#quickstart"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Quickstart</span></h2> <p>Before being able to use <code>agent.run</code>, you will need to instantiate an agent, which is a large language model (LLM). We provide support for openAI models as well as opensource alternatives from BigCode and OpenAssistant. The openAI models perform better (but require you to have an openAI API key, so cannot be used for free); Hugging Face is providing free access to endpoints for BigCode and OpenAssistant models.</p> <p>To start with, please install the <code>agents</code> extras in order to install all default dependencies.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install transformers[agents]</pre></div> <p>To use openAI models, you instantiate an <a href="/docs/transformers/v4.30.0/en/main_classes/agent#transformers.OpenAiAgent">OpenAiAgent</a> after installing the <code>openai</code> dependency:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install openai</pre></div> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> OpenAiAgent agent = OpenAiAgent(model=<span class="hljs-string">"text-davinci-003"</span>, api_key=<span class="hljs-string">"&lt;your_api_key&gt;"</span>)</pre></div> <p>To use BigCode or OpenAssistant, start by logging in to have access to the Inference API:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> login login(<span class="hljs-string">"&lt;YOUR_TOKEN&gt;"</span>)</pre></div> <p>Then, instantiate the agent</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> HfAgent <span class="hljs-comment"># Starcoder</span> agent = HfAgent(<span class="hljs-string">"https://api-inference.huggingface.co/models/bigcode/starcoder"</span>) <span class="hljs-comment"># StarcoderBase</span> <span class="hljs-comment"># agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoderbase")</span> <span class="hljs-comment"># OpenAssistant</span> <span class="hljs-comment"># agent = HfAgent(url_endpoint="https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5")</span></pre></div> <p>This is using the inference API that Hugging Face provides for free at the moment. If you have your own inference endpoint for this model (or another one) you can replace the URL above with your URL endpoint.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>StarCoder and OpenAssistant are free to use and perform admirably well on simple tasks. However, the checkpoints don’t hold up when handling more complex prompts. If you’re facing such an issue, we recommend trying out the OpenAI model which, while sadly not open-source, performs better at this given time.</p></div> <p>You’re now good to go! Let’s dive into the two APIs that you now have at your disposal.</p> <h3 class="relative group"><a id="single-execution-run" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#single-execution-run"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Single execution (run)</span></h3> <p>The single execution method is when using the <a href="/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.run">run()</a> method of the agent:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>agent.run(<span class="hljs-string">"Draw me a picture of rivers and lakes."</span>)</pre></div> <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes.png" width="200"> <p>It automatically selects the tool (or tools) appropriate for the task you want to perform and runs them appropriately. It can perform one or several tasks in the same instruction (though the more complex your instruction, the more likely the agent is to fail).</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>agent.run(<span class="hljs-string">"Draw me a picture of the sea then transform the picture to add an island"</span>)</pre></div> <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/sea_and_island.png" width="200"> <br> <p>Every <a href="/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.run">run()</a> operation is independent, so you can run it several times in a row with different tasks.</p> <p>Note that your <code>agent</code> is just a large-language model, so small variations in your prompt might yield completely different results. It’s important to explain as clearly as possible the task you want to perform. We go more in-depth on how to write good prompts <a href="custom_tools#writing-good-user-inputs">here</a>.</p> <p>If you’d like to keep a state across executions or to pass non-text objects to the agent, you can do so by specifying variables that you would like the agent to use. For example, you could generate the first image of rivers and lakes, and ask the model to update that picture to add an island by doing the following:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>picture = agent.run(<span class="hljs-string">"Generate a picture of rivers and lakes."</span>) updated_picture = agent.run(<span class="hljs-string">"Transform the image in `picture` to add an island to it."</span>, picture=picture)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>This can be helpful when the model is unable to understand your request and mixes tools. An example would be:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>agent.run(<span class="hljs-string">"Draw me the picture of a capybara swimming in the sea"</span>)</pre></div> <p>Here, the model could interpret in two ways:</p> <ul><li>Have the <code>text-to-image</code> generate a capybara swimming in the sea</li> <li>Or, have the <code>text-to-image</code> generate capybara, then use the <code>image-transformation</code> tool to have it swim in the sea</li></ul> <p>In case you would like to force the first scenario, you could do so by passing it the prompt as an argument:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>agent.run(<span class="hljs-string">"Draw me a picture of the `prompt`"</span>, prompt=<span class="hljs-string">"a capybara swimming in the sea"</span>)</pre></div></div> <h3 class="relative group"><a id="chatbased-execution-chat" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#chatbased-execution-chat"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Chat-based execution (chat)</span></h3> <p>The agent also has a chat-based approach, using the <a href="/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.chat">chat()</a> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>agent.chat(<span class="hljs-string">"Generate a picture of rivers and lakes"</span>)</pre></div> <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes.png" width="200"> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>agent.chat(<span class="hljs-string">"Transform the picture so that there is a rock in there"</span>)</pre></div> <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes_and_beaver.png" width="200"> <br> <p>This is an interesting approach when you want to keep the state across instructions. It’s better for experimentation, but will tend to be much better at single instructions rather than complex instructions (which the <a href="/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.run">run()</a> method is better at handling).</p> <p>This method can also take arguments if you would like to pass non-text types or specific prompts.</p> <h3 class="relative group"><a id="remote-execution" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#remote-execution"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>⚠️ Remote execution</span></h3> <p>For demonstration purposes and so that this can be used with all setups, we have created remote executors for several of the default tools the agent has access. These are created using <a href="https://huggingface.co/inference-endpoints" rel="nofollow">inference endpoints</a>. To see how to set up remote executors tools yourself, we recommend reading the <a href="./custom_tools">custom tool guide</a>.</p> <p>In order to run with remote tools, specifying <code>remote=True</code> to either <a href="/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.run">run()</a> or <a href="/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.chat">chat()</a> is sufficient.</p> <p>For example, the following command could be run on any device efficiently, without needing significant RAM or GPU:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>agent.run(<span class="hljs-string">"Draw me a picture of rivers and lakes"</span>, remote=<span class="hljs-literal">True</span>)</pre></div> <p>The same can be said for <a href="/docs/transformers/v4.30.0/en/main_classes/agent#transformers.Agent.chat">chat()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>agent.chat(<span class="hljs-string">"Draw me a picture of rivers and lakes"</span>, remote=<span class="hljs-literal">True</span>)</pre></div> <h3 class="relative group"><a id="whats-happening-here-what-are-tools-and-what-are-agents" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#whats-happening-here-what-are-tools-and-what-are-agents"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>What's happening here? What are tools, and what are agents?</span></h3> <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/diagram.png"> <h4 class="relative group"><a id="agents" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#agents"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Agents</span></h4> <p>The “agent” here is a large language model, and we’re prompting it so that it has access to a specific set of tools.</p> <p>LLMs are pretty good at generating small samples of code, so this API takes advantage of that by prompting the LLM gives a small sample of code performing a task with a set of tools. This prompt is then completed by the task you give your agent and the description of the tools you give it. This way it gets access to the doc of the tools you are using, especially their expected inputs and outputs, and can generate the relevant code.</p> <h4 class="relative group"><a id="tools" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tools"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Tools</span></h4> <p>Tools are very simple: they’re a single function, with a name, and a description. We then use these tools’ descriptions to prompt the agent. Through the prompt, we show the agent how it would leverage tools to perform what was requested in the query.</p> <p>This is using brand-new tools and not pipelines, because the agent writes better code with very atomic tools. Pipelines are more refactored and often combine several tasks in one. Tools are meant to be focused on one very simple task only.</p> <h4 class="relative group"><a id="codeexecution" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#codeexecution"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Code-execution?!</span></h4> <p>This code is then executed with our small Python interpreter on the set of inputs passed along with your tools. We hear you screaming “Arbitrary code execution!” in the back, but let us explain why that is not the case.</p> <p>The only functions that can be called are the tools you provided and the print function, so you’re already limited in what can be executed. You should be safe if it’s limited to Hugging Face tools.</p> <p>Then, we don’t allow any attribute lookup or imports (which shouldn’t be needed anyway for passing along inputs/outputs to a small set of functions) so all the most obvious attacks (and you’d need to prompt the LLM to output them anyway) shouldn’t be an issue. If you want to be on the super safe side, you can execute the run() method with the additional argument return_code=True, in which case the agent will just return the code to execute and you can decide whether to do it or not.</p> <p>The execution will stop at any line trying to perform an illegal operation or if there is a regular Python error with the code generated by the agent.</p> <h3 class="relative group"><a id="a-curated-set-of-tools" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#a-curated-set-of-tools"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>A curated set of tools</span></h3> <p>We identify a set of tools that can empower such agents. Here is an updated list of the tools we have integrated in <code>transformers</code>:</p> <ul><li><strong>Document question answering</strong>: given a document (such as a PDF) in image format, answer a question on this document (<a href="./model_doc/donut">Donut</a>)</li> <li><strong>Text question answering</strong>: given a long text and a question, answer the question in the text (<a href="./model_doc/flan-t5">Flan-T5</a>)</li> <li><strong>Unconditional image captioning</strong>: Caption the image! (<a href="./model_doc/blip">BLIP</a>)</li> <li><strong>Image question answering</strong>: given an image, answer a question on this image (<a href="./model_doc/vilt">VILT</a>)</li> <li><strong>Image segmentation</strong>: given an image and a prompt, output the segmentation mask of that prompt (<a href="./model_doc/clipseg">CLIPSeg</a>)</li> <li><strong>Speech to text</strong>: given an audio recording of a person talking, transcribe the speech into text (<a href="./model_doc/whisper">Whisper</a>)</li> <li><strong>Text to speech</strong>: convert text to speech (<a href="./model_doc/speecht5">SpeechT5</a>)</li> <li><strong>Zero-shot text classification</strong>: given a text and a list of labels, identify to which label the text corresponds the most (<a href="./model_doc/bart">BART</a>)</li> <li><strong>Text summarization</strong>: summarize a long text in one or a few sentences (<a href="./model_doc/bart">BART</a>)</li> <li><strong>Translation</strong>: translate the text into a given language (<a href="./model_doc/nllb">NLLB</a>)</li></ul> <p>These tools have an integration in transformers, and can be used manually as well, for example:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> load_tool tool = load_tool(<span class="hljs-string">"text-to-speech"</span>) audio = tool(<span class="hljs-string">"This is a text to speech tool"</span>)</pre></div> <h3 class="relative group"><a id="custom-tools" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#custom-tools"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Custom tools</span></h3> <p>While we identify a curated set of tools, we strongly believe that the main value provided by this implementation is the ability to quickly create and share custom tools.</p> <p>By pushing the code of a tool to a Hugging Face Space or a model repository, you’re then able to leverage the tool directly with the agent. We’ve added a few <strong>transformers-agnostic</strong> tools to the <a href="https://huggingface.co/huggingface-tools" rel="nofollow"><code>huggingface-tools</code> organization</a>:</p> <ul><li><strong>Text downloader</strong>: to download a text from a web URL</li> <li><strong>Text to image</strong>: generate an image according to a prompt, leveraging stable diffusion</li> <li><strong>Image transformation</strong>: modify an image given an initial image and a prompt, leveraging instruct pix2pix stable diffusion</li> <li><strong>Text to video</strong>: generate a small video according to a prompt, leveraging damo-vilab</li></ul> <p>The text-to-image tool we have been using since the beginning is a remote tool that lives in <a href="https://huggingface.co/spaces/huggingface-tools/text-to-image" rel="nofollow"><em>huggingface-tools/text-to-image</em></a>! We will continue releasing such tools on this and other organizations, to further supercharge this implementation.</p> <p>The agents have by default access to tools that reside on <a href="https://huggingface.co/huggingface-tools" rel="nofollow"><code>huggingface-tools</code></a>. We explain how to you can write and share your tools as well as leverage any custom tool that resides on the Hub in <a href="custom_tools">following guide</a>.</p> <h3 class="relative group"><a id="code-generation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#code-generation"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Code generation</span></h3> <p>So far we have shown how to use the agents to perform actions for you. However, the agent is only generating code that we then execute using a very restricted Python interpreter. In case you would like to use the code generated in a different setting, the agent can be prompted to return the code, along with tool definition and accurate imports.</p> <p>For example, the following instruction</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>agent.run(<span class="hljs-string">"Draw me a picture of rivers and lakes"</span>, return_code=<span class="hljs-literal">True</span>)</pre></div> <p>returns the following code</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> load_tool image_generator = load_tool(<span class="hljs-string">"huggingface-tools/text-to-image"</span>) image = image_generator(prompt=<span class="hljs-string">"rivers and lakes"</span>)</pre></div> <p>that you can then modify and execute yourself.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/model_sharing" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Share your model</a> <a href="/docs/transformers/tasks/sequence_classification" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Text classification<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Transformers Agent&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers-agent&quot;,&quot;url&quot;:&quot;#transformers-agent&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Quickstart&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quickstart&quot;,&quot;url&quot;:&quot;#quickstart&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Single execution (run)&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;single-execution-run&quot;,&quot;url&quot;:&quot;#single-execution-run&quot;},{&quot;title&quot;:&quot;Chat-based execution (chat)&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;chatbased-execution-chat&quot;,&quot;url&quot;:&quot;#chatbased-execution-chat&quot;},{&quot;title&quot;:&quot;⚠️ Remote execution&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;remote-execution&quot;,&quot;url&quot;:&quot;#remote-execution&quot;},{&quot;title&quot;:&quot;What's happening here? What are tools, and what are agents?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;whats-happening-here-what-are-tools-and-what-are-agents&quot;,&quot;url&quot;:&quot;#whats-happening-here-what-are-tools-and-what-are-agents&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;agents&quot;,&quot;url&quot;:&quot;#agents&quot;},{&quot;title&quot;:&quot;Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tools&quot;,&quot;url&quot;:&quot;#tools&quot;},{&quot;title&quot;:&quot;Code-execution?!&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;codeexecution&quot;,&quot;url&quot;:&quot;#codeexecution&quot;}]},{&quot;title&quot;:&quot;A curated set of tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;a-curated-set-of-tools&quot;,&quot;url&quot;:&quot;#a-curated-set-of-tools&quot;},{&quot;title&quot;:&quot;Custom tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom-tools&quot;,&quot;url&quot;:&quot;#custom-tools&quot;},{&quot;title&quot;:&quot;Code generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;code-generation&quot;,&quot;url&quot;:&quot;#code-generation&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#transformers-agent" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-transformers-agent"><wbr>Transformers <wbr>Agent</a> <a href="#quickstart" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-quickstart"><wbr>Quickstart</a> <a href="#single-execution-run" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-single-execution-run"><wbr>Single execution (run)</a> <a href="#chatbased-execution-chat" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-chatbased-execution-chat"><wbr>Chat-based execution (chat)</a> <a href="#remote-execution" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-remote-execution">⚠️ <wbr>Remote execution</a> <a href="#whats-happening-here-what-are-tools-and-what-are-agents" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-whats-happening-here-what-are-tools-and-what-are-agents"><wbr>What's happening here? <wbr>What are tools, and what are agents?</a> <a href="#agents" class="pl-12 text-xs text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-agents"><wbr>Agents</a><a href="#tools" class="pl-12 text-xs text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tools"><wbr>Tools</a><a href="#codeexecution" class="pl-12 text-xs text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-codeexecution"><wbr>Code-execution?!</a><a href="#a-curated-set-of-tools" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-a-curated-set-of-tools"><wbr>A curated set of tools</a> <a href="#custom-tools" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-custom-tools"><wbr>Custom tools</a> <a href="#code-generation" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-code-generation"><wbr>Code generation</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/transformers_agents" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/transformers_agents"); } </script> <iframe name="__privateStripeMetricsController3240" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Ftransformers_agents&amp;title=Transformers%20Agent&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:09.363Z
Use tokenizers from 🤗 Tokenizers
https://huggingface.co/docs/transformers/fast_tokenizers
Transformers documentation Use tokenizers from 🤗 Tokenizers Natural Language Processing Performance and scalability Reinforcement learning models The [PreTrainedTokenizerFast](/docs/transformers/v4.30.0/en/main_classes/tokenizer#transformers.PreTrainedTokenizerFast) depends on the [🤗 Tokenizers](https://huggingface.co/docs/tokenizers) library. The tokenizers obtained from the 🤗 Tokenizers library can be loaded very simply into 🤗 Transformers. Before getting in the specifics, let’s first start by creating a dummy tokenizer in a few lines: ``` >>> from tokenizers import Tokenizer >>> from tokenizers.models import BPE >>> from tokenizers.trainers import BpeTrainer >>> from tokenizers.pre_tokenizers import Whitespace >>> tokenizer = Tokenizer(BPE(unk_token="[UNK]")) >>> trainer = BpeTrainer(special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"]) >>> tokenizer.pre_tokenizer = Whitespace() >>> files = [...] >>> tokenizer.train(files, trainer)``` We now have a tokenizer trained on the files we defined. We can either continue using it in that runtime, or save it to a JSON file for future re-use. ## [](#loading-directly-from-the-tokenizer-object)Loading directly from the tokenizer object Let’s see how to leverage this tokenizer object in the 🤗 Transformers library. The [PreTrainedTokenizerFast](/docs/transformers/v4.30.0/en/main_classes/tokenizer#transformers.PreTrainedTokenizerFast) class allows for easy instantiation, by accepting the instantiated _tokenizer_ object as an argument: ``` >>> from transformers import PreTrainedTokenizerFast >>> fast_tokenizer = PreTrainedTokenizerFast(tokenizer_object=tokenizer)``` This object can now be used with all the methods shared by the 🤗 Transformers tokenizers! Head to [the tokenizer page](main_classes/tokenizer) for more information. ## [](#loading-from-a-json-file)Loading from a JSON file In order to load a tokenizer from a JSON file, let’s first start by saving our tokenizer: ``` >>> tokenizer.save("tokenizer.json")``` The path to which we saved this file can be passed to the [PreTrainedTokenizerFast](/docs/transformers/v4.30.0/en/main_classes/tokenizer#transformers.PreTrainedTokenizerFast) initialization method using the `tokenizer_file` parameter: ``` >>> from transformers import PreTrainedTokenizerFast >>> fast_tokenizer = PreTrainedTokenizerFast(tokenizer_file="tokenizer.json")``` This object can now be used with all the methods shared by the 🤗 Transformers tokenizers! Head to [the tokenizer page](main_classes/tokenizer) for more information.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Use tokenizers from 🤗 Tokenizers"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/fast_tokenizers"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Use tokenizers from 🤗 Tokenizers</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;use-tokenizers-from-tokenizers&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;loading-directly-from-the-tokenizer-object&quot;,&quot;title&quot;:&quot;Loading directly from the tokenizer object&quot;},{&quot;local&quot;:&quot;loading-from-a-json-file&quot;,&quot;title&quot;:&quot;Loading from a JSON file&quot;}],&quot;title&quot;:&quot;Use tokenizers from 🤗 Tokenizers&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;fast_tokenizers&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Use tokenizers from 🤗 Tokenizers&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Use tokenizers from 🤗 Tokenizers</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="use-tokenizers-from-tokenizers" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#use-tokenizers-from-tokenizers"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Use tokenizers from 🤗 Tokenizers</span></h1> <p>The <a href="/docs/transformers/v4.30.0/en/main_classes/tokenizer#transformers.PreTrainedTokenizerFast">PreTrainedTokenizerFast</a> depends on the <a href="https://huggingface.co/docs/tokenizers" rel="nofollow">🤗 Tokenizers</a> library. The tokenizers obtained from the 🤗 Tokenizers library can be loaded very simply into 🤗 Transformers.</p> <p>Before getting in the specifics, let’s first start by creating a dummy tokenizer in a few lines:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tokenizers <span class="hljs-keyword">import</span> Tokenizer <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tokenizers.models <span class="hljs-keyword">import</span> BPE <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tokenizers.trainers <span class="hljs-keyword">import</span> BpeTrainer <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tokenizers.pre_tokenizers <span class="hljs-keyword">import</span> Whitespace <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = Tokenizer(BPE(unk_token=<span class="hljs-string">"[UNK]"</span>)) <span class="hljs-meta">&gt;&gt;&gt; </span>trainer = BpeTrainer(special_tokens=[<span class="hljs-string">"[UNK]"</span>, <span class="hljs-string">"[CLS]"</span>, <span class="hljs-string">"[SEP]"</span>, <span class="hljs-string">"[PAD]"</span>, <span class="hljs-string">"[MASK]"</span>]) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.pre_tokenizer = Whitespace() <span class="hljs-meta">&gt;&gt;&gt; </span>files = [...] <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.train(files, trainer)</pre></div> <p>We now have a tokenizer trained on the files we defined. We can either continue using it in that runtime, or save it to a JSON file for future re-use.</p> <h2 class="relative group"><a id="loading-directly-from-the-tokenizer-object" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-directly-from-the-tokenizer-object"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Loading directly from the tokenizer object</span></h2> <p>Let’s see how to leverage this tokenizer object in the 🤗 Transformers library. The <a href="/docs/transformers/v4.30.0/en/main_classes/tokenizer#transformers.PreTrainedTokenizerFast">PreTrainedTokenizerFast</a> class allows for easy instantiation, by accepting the instantiated <em>tokenizer</em> object as an argument:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PreTrainedTokenizerFast <span class="hljs-meta">&gt;&gt;&gt; </span>fast_tokenizer = PreTrainedTokenizerFast(tokenizer_object=tokenizer)</pre></div> <p>This object can now be used with all the methods shared by the 🤗 Transformers tokenizers! Head to <a href="main_classes/tokenizer">the tokenizer page</a> for more information.</p> <h2 class="relative group"><a id="loading-from-a-json-file" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-from-a-json-file"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Loading from a JSON file</span></h2> <p>In order to load a tokenizer from a JSON file, let’s first start by saving our tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.save(<span class="hljs-string">"tokenizer.json"</span>)</pre></div> <p>The path to which we saved this file can be passed to the <a href="/docs/transformers/v4.30.0/en/main_classes/tokenizer#transformers.PreTrainedTokenizerFast">PreTrainedTokenizerFast</a> initialization method using the <code>tokenizer_file</code> parameter:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PreTrainedTokenizerFast <span class="hljs-meta">&gt;&gt;&gt; </span>fast_tokenizer = PreTrainedTokenizerFast(tokenizer_file=<span class="hljs-string">"tokenizer.json"</span>)</pre></div> <p>This object can now be used with all the methods shared by the 🤗 Transformers tokenizers! Head to <a href="main_classes/tokenizer">the tokenizer page</a> for more information.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/tasks/text-to-speech" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Text to speech</a> <a href="/docs/transformers/multilingual" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Run inference with multilingual models<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Use tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;use-tokenizers-from-tokenizers&quot;,&quot;url&quot;:&quot;#use-tokenizers-from-tokenizers&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Loading directly from the tokenizer object&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;loading-directly-from-the-tokenizer-object&quot;,&quot;url&quot;:&quot;#loading-directly-from-the-tokenizer-object&quot;},{&quot;title&quot;:&quot;Loading from a JSON file&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;loading-from-a-json-file&quot;,&quot;url&quot;:&quot;#loading-from-a-json-file&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#use-tokenizers-from-tokenizers" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-use-tokenizers-from-tokenizers"><wbr>Use tokenizers from 🤗 <wbr>Tokenizers</a> <a href="#loading-directly-from-the-tokenizer-object" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-loading-directly-from-the-tokenizer-object"><wbr>Loading directly from the tokenizer object</a> <a href="#loading-from-a-json-file" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-loading-from-a-json-file"><wbr>Loading from a JSO<wbr>N file</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/fast_tokenizers" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/fast_tokenizers"); } </script> <iframe name="__privateStripeMetricsController7740" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Ffast_tokenizers&amp;title=Use%20tokenizers%20from%20%F0%9F%A4%97%20Tokenizers&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:09.581Z
Multilingual models for inference
https://huggingface.co/docs/transformers/multilingual
There are several multilingual models in 🤗 Transformers, and their inference usage differs from monolingual models. Not _all_ multilingual model usage is different though. Some models, like [bert-base-multilingual-uncased](https://huggingface.co/bert-base-multilingual-uncased), can be used just like a monolingual model. This guide will show you how to use multilingual models whose usage differs for inference. ## [](#xlm)XLM XLM has ten different checkpoints, only one of which is monolingual. The nine remaining model checkpoints can be split into two categories: the checkpoints that use language embeddings and those that don’t. ### [](#xlm-with-language-embeddings)XLM with language embeddings The following XLM models use language embeddings to specify the language used at inference: - `xlm-mlm-ende-1024` (Masked language modeling, English-German) - `xlm-mlm-enfr-1024` (Masked language modeling, English-French) - `xlm-mlm-enro-1024` (Masked language modeling, English-Romanian) - `xlm-mlm-xnli15-1024` (Masked language modeling, XNLI languages) - `xlm-mlm-tlm-xnli15-1024` (Masked language modeling + translation, XNLI languages) - `xlm-clm-enfr-1024` (Causal language modeling, English-French) - `xlm-clm-ende-1024` (Causal language modeling, English-German) Language embeddings are represented as a tensor of the same shape as the `input_ids` passed to the model. The values in these tensors depend on the language used and are identified by the tokenizer’s `lang2id` and `id2lang` attributes. In this example, load the `xlm-clm-enfr-1024` checkpoint (Causal language modeling, English-French): ``` >>> import torch >>> from transformers import XLMTokenizer, XLMWithLMHeadModel >>> tokenizer = XLMTokenizer.from_pretrained("xlm-clm-enfr-1024") >>> model = XLMWithLMHeadModel.from_pretrained("xlm-clm-enfr-1024")``` The `lang2id` attribute of the tokenizer displays this model’s languages and their ids: ``` >>> print(tokenizer.lang2id) {'en': 0, 'fr': 1}``` Next, create an example input: ``` >>> input_ids = torch.tensor([tokenizer.encode("Wikipedia was used to")]) ``` Set the language id as `"en"` and use it to define the language embedding. The language embedding is a tensor filled with `0` since that is the language id for English. This tensor should be the same size as `input_ids`. ``` >>> language_id = tokenizer.lang2id["en"] >>> langs = torch.tensor([language_id] * input_ids.shape[1]) >>> >>> langs = langs.view(1, -1) ``` Now you can pass the `input_ids` and language embedding to the model: ``` >>> outputs = model(input_ids, langs=langs)``` The [run\_generation.py](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-generation/run_generation.py) script can generate text with language embeddings using the `xlm-clm` checkpoints. ### [](#xlm-without-language-embeddings)XLM without language embeddings The following XLM models do not require language embeddings during inference: - `xlm-mlm-17-1280` (Masked language modeling, 17 languages) - `xlm-mlm-100-1280` (Masked language modeling, 100 languages) These models are used for generic sentence representations, unlike the previous XLM checkpoints. ## [](#bert)BERT The following BERT models can be used for multilingual tasks: - `bert-base-multilingual-uncased` (Masked language modeling + Next sentence prediction, 102 languages) - `bert-base-multilingual-cased` (Masked language modeling + Next sentence prediction, 104 languages) These models do not require language embeddings during inference. They should identify the language from the context and infer accordingly. ## [](#xlmroberta)XLM-RoBERTa The following XLM-RoBERTa models can be used for multilingual tasks: - `xlm-roberta-base` (Masked language modeling, 100 languages) - `xlm-roberta-large` (Masked language modeling, 100 languages) XLM-RoBERTa was trained on 2.5TB of newly created and cleaned CommonCrawl data in 100 languages. It provides strong gains over previously released multilingual models like mBERT or XLM on downstream tasks like classification, sequence labeling, and question answering. ## [](#m2m100)M2M100 The following M2M100 models can be used for multilingual translation: - `facebook/m2m100_418M` (Translation) - `facebook/m2m100_1.2B` (Translation) In this example, load the `facebook/m2m100_418M` checkpoint to translate from Chinese to English. You can set the source language in the tokenizer: ``` >>> from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer >>> en_text = "Do not meddle in the affairs of wizards, for they are subtle and quick to anger." >>> chinese_text = "不要插手巫師的事務, 因為他們是微妙的, 很快就會發怒." >>> tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M", src_lang="zh") >>> model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")``` Tokenize the text: ``` >>> encoded_zh = tokenizer(chinese_text, return_tensors="pt")``` M2M100 forces the target language id as the first generated token to translate to the target language. Set the `forced_bos_token_id` to `en` in the `generate` method to translate to English: ``` >>> generated_tokens = model.generate(**encoded_zh, forced_bos_token_id=tokenizer.get_lang_id("en")) >>> tokenizer.batch_decode(generated_tokens, skip_special_tokens=True) 'Do not interfere with the matters of the witches, because they are delicate and will soon be angry.'``` ## [](#mbart)MBart The following MBart models can be used for multilingual translation: - `facebook/mbart-large-50-one-to-many-mmt` (One-to-many multilingual machine translation, 50 languages) - `facebook/mbart-large-50-many-to-many-mmt` (Many-to-many multilingual machine translation, 50 languages) - `facebook/mbart-large-50-many-to-one-mmt` (Many-to-one multilingual machine translation, 50 languages) - `facebook/mbart-large-50` (Multilingual translation, 50 languages) - `facebook/mbart-large-cc25` In this example, load the `facebook/mbart-large-50-many-to-many-mmt` checkpoint to translate Finnish to English. You can set the source language in the tokenizer: ``` >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM >>> en_text = "Do not meddle in the affairs of wizards, for they are subtle and quick to anger." >>> fi_text = "Älä sekaannu velhojen asioihin, sillä ne ovat hienovaraisia ja nopeasti vihaisia." >>> tokenizer = AutoTokenizer.from_pretrained("facebook/mbart-large-50-many-to-many-mmt", src_lang="fi_FI") >>> model = AutoModelForSeq2SeqLM.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")``` Tokenize the text: ``` >>> encoded_en = tokenizer(en_text, return_tensors="pt")``` MBart forces the target language id as the first generated token to translate to the target language. Set the `forced_bos_token_id` to `en` in the `generate` method to translate to English: ``` >>> generated_tokens = model.generate(**encoded_en, forced_bos_token_id=tokenizer.lang_code_to_id("en_XX")) >>> tokenizer.batch_decode(generated_tokens, skip_special_tokens=True) "Don't interfere with the wizard's affairs, because they are subtle, will soon get angry."``` If you are using the `facebook/mbart-large-50-many-to-one-mmt` checkpoint, you don’t need to force the target language id as the first generated token otherwise the usage is the same.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Multilingual models for inference"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/multilingual"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Multilingual models for inference</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;multilingual-models-for-inference&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;xlm&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;xlm-with-language-embeddings&quot;,&quot;title&quot;:&quot;XLM with language embeddings&quot;},{&quot;local&quot;:&quot;xlm-without-language-embeddings&quot;,&quot;title&quot;:&quot;XLM without language embeddings&quot;}],&quot;title&quot;:&quot;XLM&quot;},{&quot;local&quot;:&quot;bert&quot;,&quot;title&quot;:&quot;BERT&quot;},{&quot;local&quot;:&quot;xlmroberta&quot;,&quot;title&quot;:&quot;XLM-RoBERTa&quot;},{&quot;local&quot;:&quot;m2m100&quot;,&quot;title&quot;:&quot;M2M100&quot;},{&quot;local&quot;:&quot;mbart&quot;,&quot;title&quot;:&quot;MBart&quot;}],&quot;title&quot;:&quot;Multilingual models for inference&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;multilingual&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Multilingual models for inference&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Multilingual models for inference</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="multilingual-models-for-inference" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#multilingual-models-for-inference"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Multilingual models for inference</span></h1> <div class="flex space-x-1 absolute z-10 right-0 top-0"> <div class="relative colab-dropdown "><button class=" " type="button"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></button> </div> <div class="relative colab-dropdown "><button class=" " type="button"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></button> </div></div> <p>There are several multilingual models in 🤗 Transformers, and their inference usage differs from monolingual models. Not <em>all</em> multilingual model usage is different though. Some models, like <a href="https://huggingface.co/bert-base-multilingual-uncased" rel="nofollow">bert-base-multilingual-uncased</a>, can be used just like a monolingual model. This guide will show you how to use multilingual models whose usage differs for inference.</p> <h2 class="relative group"><a id="xlm" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#xlm"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>XLM</span></h2> <p>XLM has ten different checkpoints, only one of which is monolingual. The nine remaining model checkpoints can be split into two categories: the checkpoints that use language embeddings and those that don’t.</p> <h3 class="relative group"><a id="xlm-with-language-embeddings" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#xlm-with-language-embeddings"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>XLM with language embeddings</span></h3> <p>The following XLM models use language embeddings to specify the language used at inference:</p> <ul><li><code>xlm-mlm-ende-1024</code> (Masked language modeling, English-German)</li> <li><code>xlm-mlm-enfr-1024</code> (Masked language modeling, English-French)</li> <li><code>xlm-mlm-enro-1024</code> (Masked language modeling, English-Romanian)</li> <li><code>xlm-mlm-xnli15-1024</code> (Masked language modeling, XNLI languages)</li> <li><code>xlm-mlm-tlm-xnli15-1024</code> (Masked language modeling + translation, XNLI languages)</li> <li><code>xlm-clm-enfr-1024</code> (Causal language modeling, English-French)</li> <li><code>xlm-clm-ende-1024</code> (Causal language modeling, English-German)</li></ul> <p>Language embeddings are represented as a tensor of the same shape as the <code>input_ids</code> passed to the model. The values in these tensors depend on the language used and are identified by the tokenizer’s <code>lang2id</code> and <code>id2lang</code> attributes.</p> <p>In this example, load the <code>xlm-clm-enfr-1024</code> checkpoint (Causal language modeling, English-French):</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> torch <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> XLMTokenizer, XLMWithLMHeadModel <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = XLMTokenizer.from_pretrained(<span class="hljs-string">"xlm-clm-enfr-1024"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = XLMWithLMHeadModel.from_pretrained(<span class="hljs-string">"xlm-clm-enfr-1024"</span>)</pre></div> <p>The <code>lang2id</code> attribute of the tokenizer displays this model’s languages and their ids:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(tokenizer.lang2id) {<span class="hljs-string">'en'</span>: <span class="hljs-number">0</span>, <span class="hljs-string">'fr'</span>: <span class="hljs-number">1</span>}</pre></div> <p>Next, create an example input:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>input_ids = torch.tensor([tokenizer.encode(<span class="hljs-string">"Wikipedia was used to"</span>)]) <span class="hljs-comment"># batch size of 1</span></pre></div> <p>Set the language id as <code>"en"</code> and use it to define the language embedding. The language embedding is a tensor filled with <code>0</code> since that is the language id for English. This tensor should be the same size as <code>input_ids</code>.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>language_id = tokenizer.lang2id[<span class="hljs-string">"en"</span>] <span class="hljs-comment"># 0</span> <span class="hljs-meta">&gt;&gt;&gt; </span>langs = torch.tensor([language_id] * input_ids.shape[<span class="hljs-number">1</span>]) <span class="hljs-comment"># torch.tensor([0, 0, 0, ..., 0])</span> <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># We reshape it to be of size (batch_size, sequence_length)</span> <span class="hljs-meta">&gt;&gt;&gt; </span>langs = langs.view(<span class="hljs-number">1</span>, -<span class="hljs-number">1</span>) <span class="hljs-comment"># is now of shape [1, sequence_length] (we have a batch size of 1)</span></pre></div> <p>Now you can pass the <code>input_ids</code> and language embedding to the model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model(input_ids, langs=langs)</pre></div> <p>The <a href="https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-generation/run_generation.py" rel="nofollow">run_generation.py</a> script can generate text with language embeddings using the <code>xlm-clm</code> checkpoints.</p> <h3 class="relative group"><a id="xlm-without-language-embeddings" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#xlm-without-language-embeddings"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>XLM without language embeddings</span></h3> <p>The following XLM models do not require language embeddings during inference:</p> <ul><li><code>xlm-mlm-17-1280</code> (Masked language modeling, 17 languages)</li> <li><code>xlm-mlm-100-1280</code> (Masked language modeling, 100 languages)</li></ul> <p>These models are used for generic sentence representations, unlike the previous XLM checkpoints.</p> <h2 class="relative group"><a id="bert" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#bert"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>BERT</span></h2> <p>The following BERT models can be used for multilingual tasks:</p> <ul><li><code>bert-base-multilingual-uncased</code> (Masked language modeling + Next sentence prediction, 102 languages)</li> <li><code>bert-base-multilingual-cased</code> (Masked language modeling + Next sentence prediction, 104 languages)</li></ul> <p>These models do not require language embeddings during inference. They should identify the language from the context and infer accordingly.</p> <h2 class="relative group"><a id="xlmroberta" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#xlmroberta"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>XLM-RoBERTa</span></h2> <p>The following XLM-RoBERTa models can be used for multilingual tasks:</p> <ul><li><code>xlm-roberta-base</code> (Masked language modeling, 100 languages)</li> <li><code>xlm-roberta-large</code> (Masked language modeling, 100 languages)</li></ul> <p>XLM-RoBERTa was trained on 2.5TB of newly created and cleaned CommonCrawl data in 100 languages. It provides strong gains over previously released multilingual models like mBERT or XLM on downstream tasks like classification, sequence labeling, and question answering.</p> <h2 class="relative group"><a id="m2m100" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#m2m100"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>M2M100</span></h2> <p>The following M2M100 models can be used for multilingual translation:</p> <ul><li><code>facebook/m2m100_418M</code> (Translation)</li> <li><code>facebook/m2m100_1.2B</code> (Translation)</li></ul> <p>In this example, load the <code>facebook/m2m100_418M</code> checkpoint to translate from Chinese to English. You can set the source language in the tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> M2M100ForConditionalGeneration, M2M100Tokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>en_text = <span class="hljs-string">"Do not meddle in the affairs of wizards, for they are subtle and quick to anger."</span> <span class="hljs-meta">&gt;&gt;&gt; </span>chinese_text = <span class="hljs-string">"不要插手巫師的事務, 因為他們是微妙的, 很快就會發怒."</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = M2M100Tokenizer.from_pretrained(<span class="hljs-string">"facebook/m2m100_418M"</span>, src_lang=<span class="hljs-string">"zh"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = M2M100ForConditionalGeneration.from_pretrained(<span class="hljs-string">"facebook/m2m100_418M"</span>)</pre></div> <p>Tokenize the text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>encoded_zh = tokenizer(chinese_text, return_tensors=<span class="hljs-string">"pt"</span>)</pre></div> <p>M2M100 forces the target language id as the first generated token to translate to the target language. Set the <code>forced_bos_token_id</code> to <code>en</code> in the <code>generate</code> method to translate to English:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>generated_tokens = model.generate(**encoded_zh, forced_bos_token_id=tokenizer.get_lang_id(<span class="hljs-string">"en"</span>)) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.batch_decode(generated_tokens, skip_special_tokens=<span class="hljs-literal">True</span>) <span class="hljs-string">'Do not interfere with the matters of the witches, because they are delicate and will soon be angry.'</span></pre></div> <h2 class="relative group"><a id="mbart" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#mbart"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>MBart</span></h2> <p>The following MBart models can be used for multilingual translation:</p> <ul><li><code>facebook/mbart-large-50-one-to-many-mmt</code> (One-to-many multilingual machine translation, 50 languages)</li> <li><code>facebook/mbart-large-50-many-to-many-mmt</code> (Many-to-many multilingual machine translation, 50 languages)</li> <li><code>facebook/mbart-large-50-many-to-one-mmt</code> (Many-to-one multilingual machine translation, 50 languages)</li> <li><code>facebook/mbart-large-50</code> (Multilingual translation, 50 languages)</li> <li><code>facebook/mbart-large-cc25</code></li></ul> <p>In this example, load the <code>facebook/mbart-large-50-many-to-many-mmt</code> checkpoint to translate Finnish to English. You can set the source language in the tokenizer:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForSeq2SeqLM <span class="hljs-meta">&gt;&gt;&gt; </span>en_text = <span class="hljs-string">"Do not meddle in the affairs of wizards, for they are subtle and quick to anger."</span> <span class="hljs-meta">&gt;&gt;&gt; </span>fi_text = <span class="hljs-string">"Älä sekaannu velhojen asioihin, sillä ne ovat hienovaraisia ja nopeasti vihaisia."</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"facebook/mbart-large-50-many-to-many-mmt"</span>, src_lang=<span class="hljs-string">"fi_FI"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSeq2SeqLM.from_pretrained(<span class="hljs-string">"facebook/mbart-large-50-many-to-many-mmt"</span>)</pre></div> <p>Tokenize the text:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>encoded_en = tokenizer(en_text, return_tensors=<span class="hljs-string">"pt"</span>)</pre></div> <p>MBart forces the target language id as the first generated token to translate to the target language. Set the <code>forced_bos_token_id</code> to <code>en</code> in the <code>generate</code> method to translate to English:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>generated_tokens = model.generate(**encoded_en, forced_bos_token_id=tokenizer.lang_code_to_id(<span class="hljs-string">"en_XX"</span>)) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.batch_decode(generated_tokens, skip_special_tokens=<span class="hljs-literal">True</span>) <span class="hljs-string">"Don't interfere with the wizard's affairs, because they are subtle, will soon get angry."</span></pre></div> <p>If you are using the <code>facebook/mbart-large-50-many-to-one-mmt</code> checkpoint, you don’t need to force the target language id as the first generated token otherwise the usage is the same.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/fast_tokenizers" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Use fast tokenizers from 🤗 Tokenizers</a> <a href="/docs/transformers/generation_strategies" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Customize text generation strategy<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Multilingual models for inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual-models-for-inference&quot;,&quot;url&quot;:&quot;#multilingual-models-for-inference&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;XLM&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;xlm&quot;,&quot;url&quot;:&quot;#xlm&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;XLM with language embeddings&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;xlm-with-language-embeddings&quot;,&quot;url&quot;:&quot;#xlm-with-language-embeddings&quot;},{&quot;title&quot;:&quot;XLM without language embeddings&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;xlm-without-language-embeddings&quot;,&quot;url&quot;:&quot;#xlm-without-language-embeddings&quot;}]},{&quot;title&quot;:&quot;BERT&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bert&quot;,&quot;url&quot;:&quot;#bert&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;xlmroberta&quot;,&quot;url&quot;:&quot;#xlmroberta&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;m2m100&quot;,&quot;url&quot;:&quot;#m2m100&quot;},{&quot;title&quot;:&quot;MBart&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;mbart&quot;,&quot;url&quot;:&quot;#mbart&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#multilingual-models-for-inference" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-multilingual-models-for-inference"><wbr>Multilingual models for inference</a> <a href="#xlm" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-xlm">XLM</a> <a href="#xlm-with-language-embeddings" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-xlm-with-language-embeddings">XL<wbr>M with language embeddings</a> <a href="#xlm-without-language-embeddings" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-xlm-without-language-embeddings">XL<wbr>M without language embeddings</a> <a href="#bert" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-bert">BERT</a> <a href="#xlmroberta" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-xlmroberta">XL<wbr>M-<wbr>RoBER<wbr>Ta</a> <a href="#m2m100" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-m2m100"><wbr>M2<wbr>M100</a> <a href="#mbart" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-mbart">M<wbr>Bart</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/multilingual" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/multilingual"); } </script> <iframe name="__privateStripeMetricsController3130" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fmultilingual&amp;title=Multilingual%20models%20for%20inference&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:10.267Z
Text generation strategies
https://huggingface.co/docs/transformers/generation_strategies
Text generation is essential to many NLP tasks, such as open-ended text generation, summarization, translation, and more. It also plays a role in a variety of mixed-modality applications that have text as an output like speech-to-text and vision-to-text. Some of the models that can generate text include GPT2, XLNet, OpenAI GPT, CTRL, TransformerXL, XLM, Bart, T5, GIT, Whisper. Check out a few examples that use [generate()](/docs/transformers/v4.30.0/en/main_classes/text_generation#transformers.GenerationMixin.generate) method to produce text outputs for different tasks: - [Text summarization](./tasks/summarization#inference) - [Image captioning](./model_doc/git#transformers.GitForCausalLM.forward.example) - [Audio transcription](./model_doc/whisper#transformers.WhisperForConditionalGeneration.forward.example) Note that the inputs to the generate method depend on the model’s modality. They are returned by the model’s preprocessor class, such as AutoTokenizer or AutoProcessor. If a model’s preprocessor creates more than one kind of input, pass all the inputs to generate(). You can learn more about the individual model’s preprocessor in the corresponding model’s documentation. The process of selecting output tokens to generate text is known as decoding, and you can customize the decoding strategy that the `generate()` method will use. Modifying a decoding strategy does not change the values of any trainable parameters. However, it can have a noticeable impact on the quality of the generated output. It can help reduce repetition in the text and make it more coherent. This guide describes: - default generation configuration - common decoding strategies and their main parameters - saving and sharing custom generation configurations with your fine-tuned model on 🤗 Hub ## [](#default-text-generation-configuration)Default text generation configuration A decoding strategy for a model is defined in its generation configuration. When using pre-trained models for inference within a [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline), the models call the `PreTrainedModel.generate()` method that applies a default generation configuration under the hood. The default configuration is also used when no custom configuration has been saved with the model. When you load a model explicitly, you can inspect the generation configuration that comes with it through `model.generation_config`: ``` >>> from transformers import AutoModelForCausalLM >>> model = AutoModelForCausalLM.from_pretrained("distilgpt2") >>> model.generation_config GenerationConfig { "_from_model_config": true, "bos_token_id": 50256, "eos_token_id": 50256, "transformers_version": "4.26.0.dev0" }``` Printing out the `model.generation_config` reveals only the values that are different from the default generation configuration, and does not list any of the default values. The default generation configuration limits the size of the output combined with the input prompt to a maximum of 20 tokens to avoid running into resource limitations. The default decoding strategy is greedy search, which is the simplest decoding strategy that picks a token with the highest probability as the next token. For many tasks and small output sizes this works well. However, when used to generate longer outputs, greedy search can start producing highly repetitive results. ## [](#customize-text-generation)Customize text generation You can override any `generation_config` by passing the parameters and their values directly to the `generate` method: ``` >>> my_model.generate(**inputs, num_beams=4, do_sample=True)``` Even if the default decoding strategy mostly works for your task, you can still tweak a few things. Some of the commonly adjusted parameters include: - `max_new_tokens`: the maximum number of tokens to generate. In other words, the size of the output sequence, not including the tokens in the prompt. - `num_beams`: by specifying a number of beams higher than 1, you are effectively switching from greedy search to beam search. This strategy evaluates several hypotheses at each time step and eventually chooses the hypothesis that has the overall highest probability for the entire sequence. This has the advantage of identifying high-probability sequences that start with a lower probability initial tokens and would’ve been ignored by the greedy search. - `do_sample`: if set to `True`, this parameter enables decoding strategies such as multinomial sampling, beam-search multinomial sampling, Top-K sampling and Top-p sampling. All these strategies select the next token from the probability distribution over the entire vocabulary with various strategy-specific adjustments. - `num_return_sequences`: the number of sequence candidates to return for each input. This options is only available for the decoding strategies that support multiple sequence candidates, e.g. variations of beam search and sampling. Decoding strategies like greedy search and contrastive search return a single output sequence. ## [](#save-a-custom-decoding-strategy-with-your-model)Save a custom decoding strategy with your model If you would like to share your fine-tuned model with a specific generation configuration, you can: - Create a [GenerationConfig](/docs/transformers/v4.30.0/en/main_classes/text_generation#transformers.GenerationConfig) class instance - Specify the decoding strategy parameters - Save your generation configuration with [GenerationConfig.save\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/text_generation#transformers.GenerationConfig.save_pretrained), making sure to leave its `config_file_name` argument empty - Set `push_to_hub` to `True` to upload your config to the model’s repo ``` >>> from transformers import AutoModelForCausalLM, GenerationConfig >>> model = AutoModelForCausalLM.from_pretrained("my_account/my_model") >>> generation_config = GenerationConfig( ... max_new_tokens=50, do_sample=True, top_k=50, eos_token_id=model.config.eos_token_id ... ) >>> generation_config.save_pretrained("my_account/my_model", push_to_hub=True)``` You can also store several generation configurations in a single directory, making use of the `config_file_name` argument in [GenerationConfig.save\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/text_generation#transformers.GenerationConfig.save_pretrained). You can later instantiate them with [GenerationConfig.from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/text_generation#transformers.GenerationConfig.from_pretrained). This is useful if you want to store several generation configurations for a single model (e.g. one for creative text generation with sampling, and one for summarization with beam search). You must have the right Hub permissions to add configuration files to a model. ``` >>> from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, GenerationConfig >>> tokenizer = AutoTokenizer.from_pretrained("t5-small") >>> model = AutoModelForSeq2SeqLM.from_pretrained("t5-small") >>> translation_generation_config = GenerationConfig( ... num_beams=4, ... early_stopping=True, ... decoder_start_token_id=0, ... eos_token_id=model.config.eos_token_id, ... pad_token=model.config.pad_token_id, ... ) >>> translation_generation_config.save_pretrained("t5-small", "translation_generation_config.json", push_to_hub=True) >>> >>> generation_config = GenerationConfig.from_pretrained("t5-small", "translation_generation_config.json") >>> inputs = tokenizer("translate English to French: Configuration files are easy to use!", return_tensors="pt") >>> outputs = model.generate(**inputs, generation_config=generation_config) >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) ['Les fichiers de configuration sont faciles à utiliser !']``` ## [](#streaming)Streaming The `generate()` supports streaming, through its `streamer` input. The `streamer` input is compatible any instance from a class that has the following methods: `put()` and `end()`. Internally, `put()` is used to push new tokens and `end()` is used to flag the end of text generation. The API for the streamer classes is still under development and may change in the future. In practice, you can craft your own streaming class for all sorts of purposes! We also have basic streaming classes ready for you to use. For example, you can use the [TextStreamer](/docs/transformers/v4.30.0/en/internal/generation_utils#transformers.TextStreamer) class to stream the output of `generate()` into your screen, one word at a time: ``` >>> from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer >>> tok = AutoTokenizer.from_pretrained("gpt2") >>> model = AutoModelForCausalLM.from_pretrained("gpt2") >>> inputs = tok(["An increasing sequence: one,"], return_tensors="pt") >>> streamer = TextStreamer(tok) >>> >>> _ = model.generate(**inputs, streamer=streamer, max_new_tokens=20) An increasing sequence: one, two, three, four, five, six, seven, eight, nine, ten, eleven,``` ## [](#decoding-strategies)Decoding strategies Certain combinations of the `generate()` parameters, and ultimately `generation_config`, can be used to enable specific decoding strategies. If you are new to this concept, we recommend reading [this blog post that illustrates how common decoding strategies work](https://huggingface.co/blog/how-to-generate). Here, we’ll show some of the parameters that control the decoding strategies and illustrate how you can use them. ### [](#greedy-search)Greedy Search `generate` uses greedy search decoding by default so you don’t have to pass any parameters to enable it. This means the parameters `num_beams` is set to 1 and `do_sample=False`. ``` >>> from transformers import AutoModelForCausalLM, AutoTokenizer >>> prompt = "I look forward to" >>> checkpoint = "distilgpt2" >>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) >>> inputs = tokenizer(prompt, return_tensors="pt") >>> model = AutoModelForCausalLM.from_pretrained(checkpoint) >>> outputs = model.generate(**inputs) >>> tokenizer.batch_decode(outputs, skip_special_tokens=True) ['I look forward to seeing you all again!\n\n\n\n\n\n\n\n\n\n\n']``` ### [](#contrastive-search)Contrastive search The contrastive search decoding strategy was proposed in the 2022 paper [A Contrastive Framework for Neural Text Generation](https://arxiv.org/abs/2202.06417). It demonstrates superior results for generating non-repetitive yet coherent long outputs. To learn how contrastive search works, check out [this blog post](https://huggingface.co/blog/introducing-csearch). The two main parameters that enable and control the behavior of contrastive search are `penalty_alpha` and `top_k`: ``` >>> from transformers import AutoTokenizer, AutoModelForCausalLM >>> checkpoint = "gpt2-large" >>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) >>> model = AutoModelForCausalLM.from_pretrained(checkpoint) >>> prompt = "Hugging Face Company is" >>> inputs = tokenizer(prompt, return_tensors="pt") >>> outputs = model.generate(**inputs, penalty_alpha=0.6, top_k=4, max_new_tokens=100) >>> tokenizer.batch_decode(outputs, skip_special_tokens=True) ['Hugging Face Company is a family owned and operated business. \ We pride ourselves on being the best in the business and our customer service is second to none.\ \n\nIf you have any questions about our products or services, feel free to contact us at any time.\ We look forward to hearing from you!']``` ### [](#multinomial-sampling)Multinomial sampling As opposed to greedy search that always chooses a token with the highest probability as the next token, multinomial sampling (also called ancestral sampling) randomly selects the next token based on the probability distribution over the entire vocabulary given by the model. Every token with a non-zero probability has a chance of being selected, thus reducing the risk of repetition. To enable multinomial sampling set `do_sample=True` and `num_beams=1`. ``` >>> from transformers import AutoTokenizer, AutoModelForCausalLM >>> checkpoint = "gpt2-large" >>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) >>> model = AutoModelForCausalLM.from_pretrained(checkpoint) >>> prompt = "Today was an amazing day because" >>> inputs = tokenizer(prompt, return_tensors="pt") >>> outputs = model.generate(**inputs, do_sample=True, num_beams=1, max_new_tokens=100) >>> tokenizer.batch_decode(outputs, skip_special_tokens=True) ['Today was an amazing day because we are now in the final stages of our trip to New York City which was very tough. \ It is a difficult schedule and a challenging part of the year but still worth it. I have been taking things easier and \ I feel stronger and more motivated to be out there on their tour. Hopefully, that experience is going to help them with \ their upcoming events which are currently scheduled in Australia.\n\nWe love that they are here. They want to make a \ name for themselves and become famous for what they']``` ### [](#beamsearch-decoding)Beam-search decoding Unlike greedy search, beam-search decoding keeps several hypotheses at each time step and eventually chooses the hypothesis that has the overall highest probability for the entire sequence. This has the advantage of identifying high-probability sequences that start with lower probability initial tokens and would’ve been ignored by the greedy search. To enable this decoding strategy, specify the `num_beams` (aka number of hypotheses to keep track of) that is greater than 1. ``` >>> from transformers import AutoModelForCausalLM, AutoTokenizer >>> prompt = "It is astonishing how one can" >>> checkpoint = "gpt2-medium" >>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) >>> inputs = tokenizer(prompt, return_tensors="pt") >>> model = AutoModelForCausalLM.from_pretrained(checkpoint) >>> outputs = model.generate(**inputs, num_beams=5, max_new_tokens=50) >>> tokenizer.batch_decode(outputs, skip_special_tokens=True) ['It is astonishing how one can have such a profound impact on the lives of so many people in such a short period of \ time."\n\nHe added: "I am very proud of the work I have been able to do in the last few years.\n\n"I have']``` ### [](#beamsearch-multinomial-sampling)Beam-search multinomial sampling As the name implies, this decoding strategy combines beam search with multinomial sampling. You need to specify the `num_beams` greater than 1, and set `do_sample=True` to use this decoding strategy. ``` >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM >>> prompt = "translate English to German: The house is wonderful." >>> checkpoint = "t5-small" >>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) >>> inputs = tokenizer(prompt, return_tensors="pt") >>> model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint) >>> outputs = model.generate(**inputs, num_beams=5, do_sample=True) >>> tokenizer.decode(outputs[0], skip_special_tokens=True) 'Das Haus ist wunderbar.'``` ### [](#diverse-beam-search-decoding)Diverse beam search decoding The diverse beam search decoding strategy is an extension of the beam search strategy that allows for generating a more diverse set of beam sequences to choose from. To learn how it works, refer to [Diverse Beam Search: Decoding Diverse Solutions from Neural Sequence Models](https://arxiv.org/pdf/1610.02424.pdf). This approach has two main parameters: `num_beams` and `num_beam_groups`. The groups are selected to ensure they are distinct enough compared to the others, and regular beam search is used within each group. ``` >>> from transformers import AutoTokenizer, AutoModelForSeq2SeqLM >>> checkpoint = "google/pegasus-xsum" >>> prompt = "The Permaculture Design Principles are a set of universal design principles \ >>> that can be applied to any location, climate and culture, and they allow us to design \ >>> the most efficient and sustainable human habitation and food production systems. \ >>> Permaculture is a design system that encompasses a wide variety of disciplines, such \ >>> as ecology, landscape design, environmental science and energy conservation, and the \ >>> Permaculture design principles are drawn from these various disciplines. Each individual \ >>> design principle itself embodies a complete conceptual framework based on sound \ >>> scientific principles. When we bring all these separate principles together, we can \ >>> create a design system that both looks at whole systems, the parts that these systems \ >>> consist of, and how those parts interact with each other to create a complex, dynamic, \ >>> living system. Each design principle serves as a tool that allows us to integrate all \ >>> the separate parts of a design, referred to as elements, into a functional, synergistic, \ >>> whole system, where the elements harmoniously interact and work together in the most \ >>> efficient way possible." >>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) >>> inputs = tokenizer(prompt, return_tensors="pt") >>> model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint) >>> outputs = model.generate(**inputs, num_beams=5, num_beam_groups=5, max_new_tokens=30) >>> tokenizer.decode(outputs[0], skip_special_tokens=True) 'The Design Principles are a set of universal design principles that can be applied to any location, climate and culture, and they allow us to design the most efficient and sustainable human habitation and food production systems.'``` This guide illustrates the main parameters that enable various decoding strategies. More advanced parameters exist for the `generate` method, which gives you even further control over the `generate` method’s behavior. For the complete list of the available parameters, refer to the [API documentation](./main_classes/text_generation.mdx). ### [](#assisted-decoding)Assisted Decoding Assisted decoding is a modification of the decoding strategies above that uses an assistant model with the same tokenizer (ideally a much smaller model) to greedily generate a few candidate tokens. The main model then validates the candidate tokens in a single forward pass, which speeds up the decoding process. Currently, only greedy search and sampling are supported with assisted decoding, and doesn’t support batched inputs. To learn more about assisted decoding, check [this blog post](https://huggingface.co/blog/assisted-generation). To enable assisted decoding, set the `assistant_model` argument with a model. ``` >>> from transformers import AutoModelForCausalLM, AutoTokenizer >>> prompt = "Alice and Bob" >>> checkpoint = "EleutherAI/pythia-1.4b-deduped" >>> assistant_checkpoint = "EleutherAI/pythia-160m-deduped" >>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) >>> inputs = tokenizer(prompt, return_tensors="pt") >>> model = AutoModelForCausalLM.from_pretrained(checkpoint) >>> assistant_model = AutoModelForCausalLM.from_pretrained(assistant_checkpoint) >>> outputs = model.generate(**inputs, assistant_model=assistant_model) >>> tokenizer.batch_decode(outputs, skip_special_tokens=True) ['Alice and Bob are sitting in a bar. Alice is drinking a beer and Bob is drinking a']``` When using assisted decoding with sampling methods, you can use the `temperarure` argument to control the randomness just like in multinomial sampling. However, in assisted decoding, reducing the temperature will help improving latency. ``` >>> from transformers import AutoModelForCausalLM, AutoTokenizer >>> prompt = "Alice and Bob" >>> checkpoint = "EleutherAI/pythia-1.4b-deduped" >>> assistant_checkpoint = "EleutherAI/pythia-160m-deduped" >>> tokenizer = AutoTokenizer.from_pretrained(checkpoint) >>> inputs = tokenizer(prompt, return_tensors="pt") >>> model = AutoModelForCausalLM.from_pretrained(checkpoint) >>> assistant_model = AutoModelForCausalLM.from_pretrained(assistant_checkpoint) >>> outputs = model.generate(**inputs, assistant_model=assistant_model, do_sample=True, temperature=0.5) >>> tokenizer.batch_decode(outputs, skip_special_tokens=True) ["Alice and Bob are sitting on the sofa. Alice says, 'I'm going to my room"]```
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Text generation strategies"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/generation_strategies"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Text generation strategies</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;text-generation-strategies&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;default-text-generation-configuration&quot;,&quot;title&quot;:&quot;Default text generation configuration&quot;},{&quot;local&quot;:&quot;customize-text-generation&quot;,&quot;title&quot;:&quot;Customize text generation&quot;},{&quot;local&quot;:&quot;save-a-custom-decoding-strategy-with-your-model&quot;,&quot;title&quot;:&quot;Save a custom decoding strategy with your model&quot;},{&quot;local&quot;:&quot;streaming&quot;,&quot;title&quot;:&quot;Streaming&quot;},{&quot;local&quot;:&quot;decoding-strategies&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;greedy-search&quot;,&quot;title&quot;:&quot;Greedy Search&quot;},{&quot;local&quot;:&quot;contrastive-search&quot;,&quot;title&quot;:&quot;Contrastive search&quot;},{&quot;local&quot;:&quot;multinomial-sampling&quot;,&quot;title&quot;:&quot;Multinomial sampling&quot;},{&quot;local&quot;:&quot;beamsearch-decoding&quot;,&quot;title&quot;:&quot;Beam-search decoding&quot;},{&quot;local&quot;:&quot;beamsearch-multinomial-sampling&quot;,&quot;title&quot;:&quot;Beam-search multinomial sampling&quot;},{&quot;local&quot;:&quot;diverse-beam-search-decoding&quot;,&quot;title&quot;:&quot;Diverse beam search decoding&quot;},{&quot;local&quot;:&quot;assisted-decoding&quot;,&quot;title&quot;:&quot;Assisted Decoding&quot;}],&quot;title&quot;:&quot;Decoding strategies&quot;}],&quot;title&quot;:&quot;Text generation strategies&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;generation_strategies&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Text generation strategies&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Text generation strategies</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="text-generation-strategies" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#text-generation-strategies"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Text generation strategies</span></h1> <p>Text generation is essential to many NLP tasks, such as open-ended text generation, summarization, translation, and more. It also plays a role in a variety of mixed-modality applications that have text as an output like speech-to-text and vision-to-text. Some of the models that can generate text include GPT2, XLNet, OpenAI GPT, CTRL, TransformerXL, XLM, Bart, T5, GIT, Whisper.</p> <p>Check out a few examples that use <a href="/docs/transformers/v4.30.0/en/main_classes/text_generation#transformers.GenerationMixin.generate">generate()</a> method to produce text outputs for different tasks:</p> <ul><li><a href="./tasks/summarization#inference">Text summarization</a></li> <li><a href="./model_doc/git#transformers.GitForCausalLM.forward.example">Image captioning</a></li> <li><a href="./model_doc/whisper#transformers.WhisperForConditionalGeneration.forward.example">Audio transcription</a></li></ul> <p>Note that the inputs to the generate method depend on the model’s modality. They are returned by the model’s preprocessor class, such as AutoTokenizer or AutoProcessor. If a model’s preprocessor creates more than one kind of input, pass all the inputs to generate(). You can learn more about the individual model’s preprocessor in the corresponding model’s documentation.</p> <p>The process of selecting output tokens to generate text is known as decoding, and you can customize the decoding strategy that the <code>generate()</code> method will use. Modifying a decoding strategy does not change the values of any trainable parameters. However, it can have a noticeable impact on the quality of the generated output. It can help reduce repetition in the text and make it more coherent.</p> <p>This guide describes:</p> <ul><li>default generation configuration</li> <li>common decoding strategies and their main parameters</li> <li>saving and sharing custom generation configurations with your fine-tuned model on 🤗 Hub</li></ul> <h2 class="relative group"><a id="default-text-generation-configuration" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#default-text-generation-configuration"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Default text generation configuration</span></h2> <p>A decoding strategy for a model is defined in its generation configuration. When using pre-trained models for inference within a <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a>, the models call the <code>PreTrainedModel.generate()</code> method that applies a default generation configuration under the hood. The default configuration is also used when no custom configuration has been saved with the model.</p> <p>When you load a model explicitly, you can inspect the generation configuration that comes with it through <code>model.generation_config</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">"distilgpt2"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model.generation_config GenerationConfig { <span class="hljs-string">"_from_model_config"</span>: true, <span class="hljs-string">"bos_token_id"</span>: <span class="hljs-number">50256</span>, <span class="hljs-string">"eos_token_id"</span>: <span class="hljs-number">50256</span>, <span class="hljs-string">"transformers_version"</span>: <span class="hljs-string">"4.26.0.dev0"</span> }</pre></div> <p>Printing out the <code>model.generation_config</code> reveals only the values that are different from the default generation configuration, and does not list any of the default values.</p> <p>The default generation configuration limits the size of the output combined with the input prompt to a maximum of 20 tokens to avoid running into resource limitations. The default decoding strategy is greedy search, which is the simplest decoding strategy that picks a token with the highest probability as the next token. For many tasks and small output sizes this works well. However, when used to generate longer outputs, greedy search can start producing highly repetitive results.</p> <h2 class="relative group"><a id="customize-text-generation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#customize-text-generation"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Customize text generation</span></h2> <p>You can override any <code>generation_config</code> by passing the parameters and their values directly to the <code>generate</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>my_model.generate(**inputs, num_beams=<span class="hljs-number">4</span>, do_sample=<span class="hljs-literal">True</span>)</pre></div> <p>Even if the default decoding strategy mostly works for your task, you can still tweak a few things. Some of the commonly adjusted parameters include:</p> <ul><li><code>max_new_tokens</code>: the maximum number of tokens to generate. In other words, the size of the output sequence, not including the tokens in the prompt.</li> <li><code>num_beams</code>: by specifying a number of beams higher than 1, you are effectively switching from greedy search to beam search. This strategy evaluates several hypotheses at each time step and eventually chooses the hypothesis that has the overall highest probability for the entire sequence. This has the advantage of identifying high-probability sequences that start with a lower probability initial tokens and would’ve been ignored by the greedy search.</li> <li><code>do_sample</code>: if set to <code>True</code>, this parameter enables decoding strategies such as multinomial sampling, beam-search multinomial sampling, Top-K sampling and Top-p sampling. All these strategies select the next token from the probability distribution over the entire vocabulary with various strategy-specific adjustments.</li> <li><code>num_return_sequences</code>: the number of sequence candidates to return for each input. This options is only available for the decoding strategies that support multiple sequence candidates, e.g. variations of beam search and sampling. Decoding strategies like greedy search and contrastive search return a single output sequence.</li></ul> <h2 class="relative group"><a id="save-a-custom-decoding-strategy-with-your-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#save-a-custom-decoding-strategy-with-your-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Save a custom decoding strategy with your model</span></h2> <p>If you would like to share your fine-tuned model with a specific generation configuration, you can:</p> <ul><li>Create a <a href="/docs/transformers/v4.30.0/en/main_classes/text_generation#transformers.GenerationConfig">GenerationConfig</a> class instance</li> <li>Specify the decoding strategy parameters</li> <li>Save your generation configuration with <a href="/docs/transformers/v4.30.0/en/main_classes/text_generation#transformers.GenerationConfig.save_pretrained">GenerationConfig.save_pretrained()</a>, making sure to leave its <code>config_file_name</code> argument empty</li> <li>Set <code>push_to_hub</code> to <code>True</code> to upload your config to the model’s repo</li></ul> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, GenerationConfig <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">"my_account/my_model"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>generation_config = GenerationConfig( <span class="hljs-meta">... </span> max_new_tokens=<span class="hljs-number">50</span>, do_sample=<span class="hljs-literal">True</span>, top_k=<span class="hljs-number">50</span>, eos_token_id=model.config.eos_token_id <span class="hljs-meta">... </span>) <span class="hljs-meta">&gt;&gt;&gt; </span>generation_config.save_pretrained(<span class="hljs-string">"my_account/my_model"</span>, push_to_hub=<span class="hljs-literal">True</span>)</pre></div> <p>You can also store several generation configurations in a single directory, making use of the <code>config_file_name</code> argument in <a href="/docs/transformers/v4.30.0/en/main_classes/text_generation#transformers.GenerationConfig.save_pretrained">GenerationConfig.save_pretrained()</a>. You can later instantiate them with <a href="/docs/transformers/v4.30.0/en/main_classes/text_generation#transformers.GenerationConfig.from_pretrained">GenerationConfig.from_pretrained()</a>. This is useful if you want to store several generation configurations for a single model (e.g. one for creative text generation with sampling, and one for summarization with beam search). You must have the right Hub permissions to add configuration files to a model.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSeq2SeqLM, AutoTokenizer, GenerationConfig <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"t5-small"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSeq2SeqLM.from_pretrained(<span class="hljs-string">"t5-small"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>translation_generation_config = GenerationConfig( <span class="hljs-meta">... </span> num_beams=<span class="hljs-number">4</span>, <span class="hljs-meta">... </span> early_stopping=<span class="hljs-literal">True</span>, <span class="hljs-meta">... </span> decoder_start_token_id=<span class="hljs-number">0</span>, <span class="hljs-meta">... </span> eos_token_id=model.config.eos_token_id, <span class="hljs-meta">... </span> pad_token=model.config.pad_token_id, <span class="hljs-meta">... </span>) <span class="hljs-meta">&gt;&gt;&gt; </span>translation_generation_config.save_pretrained(<span class="hljs-string">"t5-small"</span>, <span class="hljs-string">"translation_generation_config.json"</span>, push_to_hub=<span class="hljs-literal">True</span>) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># You could then use the named generation config file to parameterize generation</span> <span class="hljs-meta">&gt;&gt;&gt; </span>generation_config = GenerationConfig.from_pretrained(<span class="hljs-string">"t5-small"</span>, <span class="hljs-string">"translation_generation_config.json"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(<span class="hljs-string">"translate English to French: Configuration files are easy to use!"</span>, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model.generate(**inputs, generation_config=generation_config) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(tokenizer.batch_decode(outputs, skip_special_tokens=<span class="hljs-literal">True</span>)) [<span class="hljs-string">'Les fichiers de configuration sont faciles à utiliser !'</span>]</pre></div> <h2 class="relative group"><a id="streaming" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#streaming"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Streaming</span></h2> <p>The <code>generate()</code> supports streaming, through its <code>streamer</code> input. The <code>streamer</code> input is compatible any instance from a class that has the following methods: <code>put()</code> and <code>end()</code>. Internally, <code>put()</code> is used to push new tokens and <code>end()</code> is used to flag the end of text generation.</p> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>The API for the streamer classes is still under development and may change in the future.</p></div> <p>In practice, you can craft your own streaming class for all sorts of purposes! We also have basic streaming classes ready for you to use. For example, you can use the <a href="/docs/transformers/v4.30.0/en/internal/generation_utils#transformers.TextStreamer">TextStreamer</a> class to stream the output of <code>generate()</code> into your screen, one word at a time:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, AutoTokenizer, TextStreamer <span class="hljs-meta">&gt;&gt;&gt; </span>tok = AutoTokenizer.from_pretrained(<span class="hljs-string">"gpt2"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">"gpt2"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tok([<span class="hljs-string">"An increasing sequence: one,"</span>], return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>streamer = TextStreamer(tok) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Despite returning the usual output, the streamer will also print the generated text to stdout.</span> <span class="hljs-meta">&gt;&gt;&gt; </span>_ = model.generate(**inputs, streamer=streamer, max_new_tokens=<span class="hljs-number">20</span>) An increasing sequence: one, two, three, four, five, six, seven, eight, nine, ten, eleven,</pre></div> <h2 class="relative group"><a id="decoding-strategies" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#decoding-strategies"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Decoding strategies</span></h2> <p>Certain combinations of the <code>generate()</code> parameters, and ultimately <code>generation_config</code>, can be used to enable specific decoding strategies. If you are new to this concept, we recommend reading <a href="https://huggingface.co/blog/how-to-generate" rel="nofollow">this blog post that illustrates how common decoding strategies work</a>.</p> <p>Here, we’ll show some of the parameters that control the decoding strategies and illustrate how you can use them.</p> <h3 class="relative group"><a id="greedy-search" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#greedy-search"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Greedy Search</span></h3> <p><code>generate</code> uses greedy search decoding by default so you don’t have to pass any parameters to enable it. This means the parameters <code>num_beams</code> is set to 1 and <code>do_sample=False</code>.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>prompt = <span class="hljs-string">"I look forward to"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>checkpoint = <span class="hljs-string">"distilgpt2"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(prompt, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForCausalLM.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model.generate(**inputs) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.batch_decode(outputs, skip_special_tokens=<span class="hljs-literal">True</span>) [<span class="hljs-string">'I look forward to seeing you all again!\n\n\n\n\n\n\n\n\n\n\n'</span>]</pre></div> <h3 class="relative group"><a id="contrastive-search" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#contrastive-search"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Contrastive search</span></h3> <p>The contrastive search decoding strategy was proposed in the 2022 paper <a href="https://arxiv.org/abs/2202.06417" rel="nofollow">A Contrastive Framework for Neural Text Generation</a>. It demonstrates superior results for generating non-repetitive yet coherent long outputs. To learn how contrastive search works, check out <a href="https://huggingface.co/blog/introducing-csearch" rel="nofollow">this blog post</a>. The two main parameters that enable and control the behavior of contrastive search are <code>penalty_alpha</code> and <code>top_k</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForCausalLM <span class="hljs-meta">&gt;&gt;&gt; </span>checkpoint = <span class="hljs-string">"gpt2-large"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForCausalLM.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>prompt = <span class="hljs-string">"Hugging Face Company is"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(prompt, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model.generate(**inputs, penalty_alpha=<span class="hljs-number">0.6</span>, top_k=<span class="hljs-number">4</span>, max_new_tokens=<span class="hljs-number">100</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.batch_decode(outputs, skip_special_tokens=<span class="hljs-literal">True</span>) [<span class="hljs-string">'Hugging Face Company is a family owned and operated business. \ We pride ourselves on being the best in the business and our customer service is second to none.\ \n\nIf you have any questions about our products or services, feel free to contact us at any time.\ We look forward to hearing from you!'</span>]</pre></div> <h3 class="relative group"><a id="multinomial-sampling" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#multinomial-sampling"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Multinomial sampling</span></h3> <p>As opposed to greedy search that always chooses a token with the highest probability as the next token, multinomial sampling (also called ancestral sampling) randomly selects the next token based on the probability distribution over the entire vocabulary given by the model. Every token with a non-zero probability has a chance of being selected, thus reducing the risk of repetition.</p> <p>To enable multinomial sampling set <code>do_sample=True</code> and <code>num_beams=1</code>.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForCausalLM <span class="hljs-meta">&gt;&gt;&gt; </span>checkpoint = <span class="hljs-string">"gpt2-large"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForCausalLM.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>prompt = <span class="hljs-string">"Today was an amazing day because"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(prompt, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model.generate(**inputs, do_sample=<span class="hljs-literal">True</span>, num_beams=<span class="hljs-number">1</span>, max_new_tokens=<span class="hljs-number">100</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.batch_decode(outputs, skip_special_tokens=<span class="hljs-literal">True</span>) [<span class="hljs-string">'Today was an amazing day because we are now in the final stages of our trip to New York City which was very tough. \ It is a difficult schedule and a challenging part of the year but still worth it. I have been taking things easier and \ I feel stronger and more motivated to be out there on their tour. Hopefully, that experience is going to help them with \ their upcoming events which are currently scheduled in Australia.\n\nWe love that they are here. They want to make a \ name for themselves and become famous for what they'</span>]</pre></div> <h3 class="relative group"><a id="beamsearch-decoding" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#beamsearch-decoding"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Beam-search decoding</span></h3> <p>Unlike greedy search, beam-search decoding keeps several hypotheses at each time step and eventually chooses the hypothesis that has the overall highest probability for the entire sequence. This has the advantage of identifying high-probability sequences that start with lower probability initial tokens and would’ve been ignored by the greedy search.</p> <p>To enable this decoding strategy, specify the <code>num_beams</code> (aka number of hypotheses to keep track of) that is greater than 1.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>prompt = <span class="hljs-string">"It is astonishing how one can"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>checkpoint = <span class="hljs-string">"gpt2-medium"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(prompt, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForCausalLM.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model.generate(**inputs, num_beams=<span class="hljs-number">5</span>, max_new_tokens=<span class="hljs-number">50</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.batch_decode(outputs, skip_special_tokens=<span class="hljs-literal">True</span>) [<span class="hljs-string">'It is astonishing how one can have such a profound impact on the lives of so many people in such a short period of \ time."\n\nHe added: "I am very proud of the work I have been able to do in the last few years.\n\n"I have'</span>]</pre></div> <h3 class="relative group"><a id="beamsearch-multinomial-sampling" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#beamsearch-multinomial-sampling"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Beam-search multinomial sampling</span></h3> <p>As the name implies, this decoding strategy combines beam search with multinomial sampling. You need to specify the <code>num_beams</code> greater than 1, and set <code>do_sample=True</code> to use this decoding strategy.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForSeq2SeqLM <span class="hljs-meta">&gt;&gt;&gt; </span>prompt = <span class="hljs-string">"translate English to German: The house is wonderful."</span> <span class="hljs-meta">&gt;&gt;&gt; </span>checkpoint = <span class="hljs-string">"t5-small"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(prompt, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model.generate(**inputs, num_beams=<span class="hljs-number">5</span>, do_sample=<span class="hljs-literal">True</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.decode(outputs[<span class="hljs-number">0</span>], skip_special_tokens=<span class="hljs-literal">True</span>) <span class="hljs-string">'Das Haus ist wunderbar.'</span></pre></div> <h3 class="relative group"><a id="diverse-beam-search-decoding" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#diverse-beam-search-decoding"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Diverse beam search decoding</span></h3> <p>The diverse beam search decoding strategy is an extension of the beam search strategy that allows for generating a more diverse set of beam sequences to choose from. To learn how it works, refer to <a href="https://arxiv.org/pdf/1610.02424.pdf" rel="nofollow">Diverse Beam Search: Decoding Diverse Solutions from Neural Sequence Models</a>. This approach has two main parameters: <code>num_beams</code> and <code>num_beam_groups</code>. The groups are selected to ensure they are distinct enough compared to the others, and regular beam search is used within each group.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForSeq2SeqLM <span class="hljs-meta">&gt;&gt;&gt; </span>checkpoint = <span class="hljs-string">"google/pegasus-xsum"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>prompt = <span class="hljs-string">"The Permaculture Design Principles are a set of universal design principles \ &gt;&gt;&gt; that can be applied to any location, climate and culture, and they allow us to design \ &gt;&gt;&gt; the most efficient and sustainable human habitation and food production systems. \ &gt;&gt;&gt; Permaculture is a design system that encompasses a wide variety of disciplines, such \ &gt;&gt;&gt; as ecology, landscape design, environmental science and energy conservation, and the \ &gt;&gt;&gt; Permaculture design principles are drawn from these various disciplines. Each individual \ &gt;&gt;&gt; design principle itself embodies a complete conceptual framework based on sound \ &gt;&gt;&gt; scientific principles. When we bring all these separate principles together, we can \ &gt;&gt;&gt; create a design system that both looks at whole systems, the parts that these systems \ &gt;&gt;&gt; consist of, and how those parts interact with each other to create a complex, dynamic, \ &gt;&gt;&gt; living system. Each design principle serves as a tool that allows us to integrate all \ &gt;&gt;&gt; the separate parts of a design, referred to as elements, into a functional, synergistic, \ &gt;&gt;&gt; whole system, where the elements harmoniously interact and work together in the most \ &gt;&gt;&gt; efficient way possible."</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(prompt, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model.generate(**inputs, num_beams=<span class="hljs-number">5</span>, num_beam_groups=<span class="hljs-number">5</span>, max_new_tokens=<span class="hljs-number">30</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.decode(outputs[<span class="hljs-number">0</span>], skip_special_tokens=<span class="hljs-literal">True</span>) <span class="hljs-string">'The Design Principles are a set of universal design principles that can be applied to any location, climate and culture, and they allow us to design the most efficient and sustainable human habitation and food production systems.'</span></pre></div> <p>This guide illustrates the main parameters that enable various decoding strategies. More advanced parameters exist for the <code>generate</code> method, which gives you even further control over the <code>generate</code> method’s behavior. For the complete list of the available parameters, refer to the <a href="./main_classes/text_generation.mdx">API documentation</a>.</p> <h3 class="relative group"><a id="assisted-decoding" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#assisted-decoding"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Assisted Decoding</span></h3> <p>Assisted decoding is a modification of the decoding strategies above that uses an assistant model with the same tokenizer (ideally a much smaller model) to greedily generate a few candidate tokens. The main model then validates the candidate tokens in a single forward pass, which speeds up the decoding process. Currently, only greedy search and sampling are supported with assisted decoding, and doesn’t support batched inputs. To learn more about assisted decoding, check <a href="https://huggingface.co/blog/assisted-generation" rel="nofollow">this blog post</a>.</p> <p>To enable assisted decoding, set the <code>assistant_model</code> argument with a model.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>prompt = <span class="hljs-string">"Alice and Bob"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>checkpoint = <span class="hljs-string">"EleutherAI/pythia-1.4b-deduped"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>assistant_checkpoint = <span class="hljs-string">"EleutherAI/pythia-160m-deduped"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(prompt, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForCausalLM.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>assistant_model = AutoModelForCausalLM.from_pretrained(assistant_checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model.generate(**inputs, assistant_model=assistant_model) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.batch_decode(outputs, skip_special_tokens=<span class="hljs-literal">True</span>) [<span class="hljs-string">'Alice and Bob are sitting in a bar. Alice is drinking a beer and Bob is drinking a'</span>]</pre></div> <p>When using assisted decoding with sampling methods, you can use the <code>temperarure</code> argument to control the randomness just like in multinomial sampling. However, in assisted decoding, reducing the temperature will help improving latency.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>prompt = <span class="hljs-string">"Alice and Bob"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>checkpoint = <span class="hljs-string">"EleutherAI/pythia-1.4b-deduped"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>assistant_checkpoint = <span class="hljs-string">"EleutherAI/pythia-160m-deduped"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(prompt, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForCausalLM.from_pretrained(checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>assistant_model = AutoModelForCausalLM.from_pretrained(assistant_checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model.generate(**inputs, assistant_model=assistant_model, do_sample=<span class="hljs-literal">True</span>, temperature=<span class="hljs-number">0.5</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.batch_decode(outputs, skip_special_tokens=<span class="hljs-literal">True</span>) [<span class="hljs-string">"Alice and Bob are sitting on the sofa. Alice says, 'I'm going to my room"</span>]</pre></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/multilingual" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Run inference with multilingual models</a> <a href="/docs/transformers/create_a_model" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Use model-specific APIs<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Text generation strategies&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;text-generation-strategies&quot;,&quot;url&quot;:&quot;#text-generation-strategies&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Default text generation configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;default-text-generation-configuration&quot;,&quot;url&quot;:&quot;#default-text-generation-configuration&quot;},{&quot;title&quot;:&quot;Customize text generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;customize-text-generation&quot;,&quot;url&quot;:&quot;#customize-text-generation&quot;},{&quot;title&quot;:&quot;Save a custom decoding strategy with your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;save-a-custom-decoding-strategy-with-your-model&quot;,&quot;url&quot;:&quot;#save-a-custom-decoding-strategy-with-your-model&quot;},{&quot;title&quot;:&quot;Streaming&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;streaming&quot;,&quot;url&quot;:&quot;#streaming&quot;},{&quot;title&quot;:&quot;Decoding strategies&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;decoding-strategies&quot;,&quot;url&quot;:&quot;#decoding-strategies&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Greedy Search&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;greedy-search&quot;,&quot;url&quot;:&quot;#greedy-search&quot;},{&quot;title&quot;:&quot;Contrastive search&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contrastive-search&quot;,&quot;url&quot;:&quot;#contrastive-search&quot;},{&quot;title&quot;:&quot;Multinomial sampling&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multinomial-sampling&quot;,&quot;url&quot;:&quot;#multinomial-sampling&quot;},{&quot;title&quot;:&quot;Beam-search decoding&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;beamsearch-decoding&quot;,&quot;url&quot;:&quot;#beamsearch-decoding&quot;},{&quot;title&quot;:&quot;Beam-search multinomial sampling&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;beamsearch-multinomial-sampling&quot;,&quot;url&quot;:&quot;#beamsearch-multinomial-sampling&quot;},{&quot;title&quot;:&quot;Diverse beam search decoding&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;diverse-beam-search-decoding&quot;,&quot;url&quot;:&quot;#diverse-beam-search-decoding&quot;},{&quot;title&quot;:&quot;Assisted Decoding&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;assisted-decoding&quot;,&quot;url&quot;:&quot;#assisted-decoding&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#text-generation-strategies" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-text-generation-strategies"><wbr>Text generation strategies</a> <a href="#default-text-generation-configuration" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-default-text-generation-configuration"><wbr>Default text generation configuration</a> <a href="#customize-text-generation" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-customize-text-generation"><wbr>Customize text generation</a> <a href="#save-a-custom-decoding-strategy-with-your-model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-save-a-custom-decoding-strategy-with-your-model"><wbr>Save a custom decoding strategy with your model</a> <a href="#streaming" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-streaming"><wbr>Streaming</a> <a href="#decoding-strategies" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-decoding-strategies"><wbr>Decoding strategies</a> <a href="#greedy-search" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-greedy-search"><wbr>Greedy <wbr>Search</a> <a href="#contrastive-search" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-contrastive-search"><wbr>Contrastive search</a> <a href="#multinomial-sampling" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-multinomial-sampling"><wbr>Multinomial sampling</a> <a href="#beamsearch-decoding" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-beamsearch-decoding"><wbr>Beam-search decoding</a> <a href="#beamsearch-multinomial-sampling" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-beamsearch-multinomial-sampling"><wbr>Beam-search multinomial sampling</a> <a href="#diverse-beam-search-decoding" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-diverse-beam-search-decoding"><wbr>Diverse beam search decoding</a> <a href="#assisted-decoding" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-assisted-decoding"><wbr>Assisted <wbr>Decoding</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/generation_strategies" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/generation_strategies"); } </script> <iframe name="__privateStripeMetricsController0710" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fgeneration_strategies&amp;title=Text%20generation%20strategies&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:10.444Z
Create a custom architecture
https://huggingface.co/docs/transformers/create_a_model
An [`AutoClass`](model_doc/auto) automatically infers the model architecture and downloads pretrained configuration and weights. Generally, we recommend using an `AutoClass` to produce checkpoint-agnostic code. But users who want more control over specific model parameters can create a custom 🤗 Transformers model from just a few base classes. This could be particularly useful for anyone who is interested in studying, training or experimenting with a 🤗 Transformers model. In this guide, dive deeper into creating a custom model without an `AutoClass`. Learn how to: - Load and customize a model configuration. - Create a model architecture. - Create a slow and fast tokenizer for text. - Create an image processor for vision tasks. - Create a feature extractor for audio tasks. - Create a processor for multimodal tasks. ## [](#configuration)Configuration A [configuration](main_classes/configuration) refers to a model’s specific attributes. Each model configuration has different attributes; for instance, all NLP models have the `hidden_size`, `num_attention_heads`, `num_hidden_layers` and `vocab_size` attributes in common. These attributes specify the number of attention heads or hidden layers to construct a model with. Get a closer look at [DistilBERT](model_doc/distilbert) by accessing [DistilBertConfig](/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertConfig) to inspect it’s attributes: ``` >>> from transformers import DistilBertConfig >>> config = DistilBertConfig() >>> print(config) DistilBertConfig { "activation": "gelu", "attention_dropout": 0.1, "dim": 768, "dropout": 0.1, "hidden_dim": 3072, "initializer_range": 0.02, "max_position_embeddings": 512, "model_type": "distilbert", "n_heads": 12, "n_layers": 6, "pad_token_id": 0, "qa_dropout": 0.1, "seq_classif_dropout": 0.2, "sinusoidal_pos_embds": false, "transformers_version": "4.16.2", "vocab_size": 30522 }``` [DistilBertConfig](/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertConfig) displays all the default attributes used to build a base [DistilBertModel](/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertModel). All attributes are customizable, creating space for experimentation. For example, you can customize a default model to: - Try a different activation function with the `activation` parameter. - Use a higher dropout ratio for the attention probabilities with the `attention_dropout` parameter. ``` >>> my_config = DistilBertConfig(activation="relu", attention_dropout=0.4) >>> print(my_config) DistilBertConfig { "activation": "relu", "attention_dropout": 0.4, "dim": 768, "dropout": 0.1, "hidden_dim": 3072, "initializer_range": 0.02, "max_position_embeddings": 512, "model_type": "distilbert", "n_heads": 12, "n_layers": 6, "pad_token_id": 0, "qa_dropout": 0.1, "seq_classif_dropout": 0.2, "sinusoidal_pos_embds": false, "transformers_version": "4.16.2", "vocab_size": 30522 }``` Pretrained model attributes can be modified in the [from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/configuration#transformers.PretrainedConfig.from_pretrained) function: ``` >>> my_config = DistilBertConfig.from_pretrained("distilbert-base-uncased", activation="relu", attention_dropout=0.4)``` Once you are satisfied with your model configuration, you can save it with [save\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/configuration#transformers.PretrainedConfig.save_pretrained). Your configuration file is stored as a JSON file in the specified save directory: ``` >>> my_config.save_pretrained(save_directory="./your_model_save_path")``` To reuse the configuration file, load it with [from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/configuration#transformers.PretrainedConfig.from_pretrained): ``` >>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/config.json")``` You can also save your configuration file as a dictionary or even just the difference between your custom configuration attributes and the default configuration attributes! See the [configuration](main_classes/configuration) documentation for more details. ## [](#model)Model The next step is to create a [model](main_classes/models). The model - also loosely referred to as the architecture - defines what each layer is doing and what operations are happening. Attributes like `num_hidden_layers` from the configuration are used to define the architecture. Every model shares the base class [PreTrainedModel](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel) and a few common methods like resizing input embeddings and pruning self-attention heads. In addition, all models are also either a [`torch.nn.Module`](https://pytorch.org/docs/stable/generated/torch.nn.Module.html), [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) or [`flax.linen.Module`](https://flax.readthedocs.io/en/latest/flax.linen.html#module) subclass. This means models are compatible with each of their respective framework’s usage. Load your custom configuration attributes into the model: ``` >>> from transformers import DistilBertModel >>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/config.json") >>> model = DistilBertModel(my_config)``` This creates a model with random values instead of pretrained weights. You won’t be able to use this model for anything useful yet until you train it. Training is a costly and time-consuming process. It is generally better to use a pretrained model to obtain better results faster, while using only a fraction of the resources required for training. Create a pretrained model with [from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained): ``` >>> model = DistilBertModel.from_pretrained("distilbert-base-uncased")``` When you load pretrained weights, the default model configuration is automatically loaded if the model is provided by 🤗 Transformers. However, you can still replace - some or all of - the default model configuration attributes with your own if you’d like: ``` >>> model = DistilBertModel.from_pretrained("distilbert-base-uncased", config=my_config)``` Load your custom configuration attributes into the model: ``` >>> from transformers import TFDistilBertModel >>> my_config = DistilBertConfig.from_pretrained("./your_model_save_path/my_config.json") >>> tf_model = TFDistilBertModel(my_config)``` This creates a model with random values instead of pretrained weights. You won’t be able to use this model for anything useful yet until you train it. Training is a costly and time-consuming process. It is generally better to use a pretrained model to obtain better results faster, while using only a fraction of the resources required for training. Create a pretrained model with [from\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.from_pretrained): ``` >>> tf_model = TFDistilBertModel.from_pretrained("distilbert-base-uncased")``` When you load pretrained weights, the default model configuration is automatically loaded if the model is provided by 🤗 Transformers. However, you can still replace - some or all of - the default model configuration attributes with your own if you’d like: ``` >>> tf_model = TFDistilBertModel.from_pretrained("distilbert-base-uncased", config=my_config)``` ### [](#model-heads)Model heads At this point, you have a base DistilBERT model which outputs the _hidden states_. The hidden states are passed as inputs to a model head to produce the final output. 🤗 Transformers provides a different model head for each task as long as a model supports the task (i.e., you can’t use DistilBERT for a sequence-to-sequence task like translation). For example, [DistilBertForSequenceClassification](/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertForSequenceClassification) is a base DistilBERT model with a sequence classification head. The sequence classification head is a linear layer on top of the pooled outputs. ``` >>> from transformers import DistilBertForSequenceClassification >>> model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased")``` Easily reuse this checkpoint for another task by switching to a different model head. For a question answering task, you would use the [DistilBertForQuestionAnswering](/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertForQuestionAnswering) model head. The question answering head is similar to the sequence classification head except it is a linear layer on top of the hidden states output. ``` >>> from transformers import DistilBertForQuestionAnswering >>> model = DistilBertForQuestionAnswering.from_pretrained("distilbert-base-uncased")``` For example, [TFDistilBertForSequenceClassification](/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.TFDistilBertForSequenceClassification) is a base DistilBERT model with a sequence classification head. The sequence classification head is a linear layer on top of the pooled outputs. ``` >>> from transformers import TFDistilBertForSequenceClassification >>> tf_model = TFDistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased")``` Easily reuse this checkpoint for another task by switching to a different model head. For a question answering task, you would use the [TFDistilBertForQuestionAnswering](/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.TFDistilBertForQuestionAnswering) model head. The question answering head is similar to the sequence classification head except it is a linear layer on top of the hidden states output. ``` >>> from transformers import TFDistilBertForQuestionAnswering >>> tf_model = TFDistilBertForQuestionAnswering.from_pretrained("distilbert-base-uncased")``` ## [](#tokenizer)Tokenizer The last base class you need before using a model for textual data is a [tokenizer](main_classes/tokenizer) to convert raw text to tensors. There are two types of tokenizers you can use with 🤗 Transformers: - [PreTrainedTokenizer](/docs/transformers/v4.30.0/en/main_classes/tokenizer#transformers.PreTrainedTokenizer): a Python implementation of a tokenizer. - [PreTrainedTokenizerFast](/docs/transformers/v4.30.0/en/main_classes/tokenizer#transformers.PreTrainedTokenizerFast): a tokenizer from our Rust-based [🤗 Tokenizer](https://huggingface.co/docs/tokenizers/python/latest/) library. This tokenizer type is significantly faster - especially during batch tokenization - due to it’s Rust implementation. The fast tokenizer also offers additional methods like _offset mapping_ which maps tokens to their original words or characters. Both tokenizers support common methods such as encoding and decoding, adding new tokens, and managing special tokens. Not every model supports a fast tokenizer. Take a look at this [table](index#supported-frameworks) to check if a model has fast tokenizer support. If you trained your own tokenizer, you can create one from your _vocabulary_ file: ``` >>> from transformers import DistilBertTokenizer >>> my_tokenizer = DistilBertTokenizer(vocab_file="my_vocab_file.txt", do_lower_case=False, padding_side="left")``` It is important to remember the vocabulary from a custom tokenizer will be different from the vocabulary generated by a pretrained model’s tokenizer. You need to use a pretrained model’s vocabulary if you are using a pretrained model, otherwise the inputs won’t make sense. Create a tokenizer with a pretrained model’s vocabulary with the [DistilBertTokenizer](/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertTokenizer) class: ``` >>> from transformers import DistilBertTokenizer >>> slow_tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased")``` Create a fast tokenizer with the [DistilBertTokenizerFast](/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertTokenizerFast) class: ``` >>> from transformers import DistilBertTokenizerFast >>> fast_tokenizer = DistilBertTokenizerFast.from_pretrained("distilbert-base-uncased")``` By default, [AutoTokenizer](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer) will try to load a fast tokenizer. You can disable this behavior by setting `use_fast=False` in `from_pretrained`. ## [](#image-processor)Image Processor An image processor processes vision inputs. It inherits from the base [ImageProcessingMixin](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin) class. To use, create an image processor associated with the model you’re using. For example, create a default [ViTImageProcessor](/docs/transformers/v4.30.0/en/model_doc/vit#transformers.ViTImageProcessor) if you are using [ViT](model_doc/vit) for image classification: ``` >>> from transformers import ViTImageProcessor >>> vit_extractor = ViTImageProcessor() >>> print(vit_extractor) ViTImageProcessor { "do_normalize": true, "do_resize": true, "feature_extractor_type": "ViTImageProcessor", "image_mean": [ 0.5, 0.5, 0.5 ], "image_std": [ 0.5, 0.5, 0.5 ], "resample": 2, "size": 224 }``` If you aren’t looking for any customization, just use the `from_pretrained` method to load a model’s default image processor parameters. Modify any of the [ViTImageProcessor](/docs/transformers/v4.30.0/en/model_doc/vit#transformers.ViTImageProcessor) parameters to create your custom image processor: ``` >>> from transformers import ViTImageProcessor >>> my_vit_extractor = ViTImageProcessor(resample="PIL.Image.BOX", do_normalize=False, image_mean=[0.3, 0.3, 0.3]) >>> print(my_vit_extractor) ViTImageProcessor { "do_normalize": false, "do_resize": true, "feature_extractor_type": "ViTImageProcessor", "image_mean": [ 0.3, 0.3, 0.3 ], "image_std": [ 0.5, 0.5, 0.5 ], "resample": "PIL.Image.BOX", "size": 224 }``` ## [](#feature-extractor)Feature Extractor A feature extractor processes audio inputs. It inherits from the base [FeatureExtractionMixin](/docs/transformers/v4.30.0/en/main_classes/feature_extractor#transformers.FeatureExtractionMixin) class, and may also inherit from the [SequenceFeatureExtractor](/docs/transformers/v4.30.0/en/main_classes/feature_extractor#transformers.SequenceFeatureExtractor) class for processing audio inputs. To use, create a feature extractor associated with the model you’re using. For example, create a default [Wav2Vec2FeatureExtractor](/docs/transformers/v4.30.0/en/model_doc/wav2vec2#transformers.Wav2Vec2FeatureExtractor) if you are using [Wav2Vec2](model_doc/wav2vec2) for audio classification: ``` >>> from transformers import Wav2Vec2FeatureExtractor >>> w2v2_extractor = Wav2Vec2FeatureExtractor() >>> print(w2v2_extractor) Wav2Vec2FeatureExtractor { "do_normalize": true, "feature_extractor_type": "Wav2Vec2FeatureExtractor", "feature_size": 1, "padding_side": "right", "padding_value": 0.0, "return_attention_mask": false, "sampling_rate": 16000 }``` If you aren’t looking for any customization, just use the `from_pretrained` method to load a model’s default feature extractor parameters. Modify any of the [Wav2Vec2FeatureExtractor](/docs/transformers/v4.30.0/en/model_doc/wav2vec2#transformers.Wav2Vec2FeatureExtractor) parameters to create your custom feature extractor: ``` >>> from transformers import Wav2Vec2FeatureExtractor >>> w2v2_extractor = Wav2Vec2FeatureExtractor(sampling_rate=8000, do_normalize=False) >>> print(w2v2_extractor) Wav2Vec2FeatureExtractor { "do_normalize": false, "feature_extractor_type": "Wav2Vec2FeatureExtractor", "feature_size": 1, "padding_side": "right", "padding_value": 0.0, "return_attention_mask": false, "sampling_rate": 8000 }``` ## [](#processor)Processor For models that support multimodal tasks, 🤗 Transformers offers a processor class that conveniently wraps processing classes such as a feature extractor and a tokenizer into a single object. For example, let’s use the [Wav2Vec2Processor](/docs/transformers/v4.30.0/en/model_doc/wav2vec2#transformers.Wav2Vec2Processor) for an automatic speech recognition task (ASR). ASR transcribes audio to text, so you will need a feature extractor and a tokenizer. Create a feature extractor to handle the audio inputs: ``` >>> from transformers import Wav2Vec2FeatureExtractor >>> feature_extractor = Wav2Vec2FeatureExtractor(padding_value=1.0, do_normalize=True)``` Create a tokenizer to handle the text inputs: ``` >>> from transformers import Wav2Vec2CTCTokenizer >>> tokenizer = Wav2Vec2CTCTokenizer(vocab_file="my_vocab_file.txt")``` Combine the feature extractor and tokenizer in [Wav2Vec2Processor](/docs/transformers/v4.30.0/en/model_doc/wav2vec2#transformers.Wav2Vec2Processor): ``` >>> from transformers import Wav2Vec2Processor >>> processor = Wav2Vec2Processor(feature_extractor=feature_extractor, tokenizer=tokenizer)``` With two basic classes - configuration and model - and an additional preprocessing class (tokenizer, image processor, feature extractor, or processor), you can create any of the models supported by 🤗 Transformers. Each of these base classes are configurable, allowing you to use the specific attributes you want. You can easily setup a model for training or modify an existing pretrained model to fine-tune.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Create a custom architecture"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/create_a_model"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Create a custom architecture</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;create-a-custom-architecture&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;configuration&quot;,&quot;title&quot;:&quot;Configuration&quot;},{&quot;local&quot;:&quot;model&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;model-heads&quot;,&quot;title&quot;:&quot;Model heads&quot;}],&quot;title&quot;:&quot;Model&quot;},{&quot;local&quot;:&quot;tokenizer&quot;,&quot;title&quot;:&quot;Tokenizer&quot;},{&quot;local&quot;:&quot;image-processor&quot;,&quot;title&quot;:&quot;Image Processor&quot;},{&quot;local&quot;:&quot;feature-extractor&quot;,&quot;title&quot;:&quot;Feature Extractor&quot;},{&quot;local&quot;:&quot;processor&quot;,&quot;title&quot;:&quot;Processor&quot;}],&quot;title&quot;:&quot;Create a custom architecture&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;create_a_model&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Create a custom architecture&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Create a custom architecture</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="create-a-custom-architecture" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#create-a-custom-architecture"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Create a custom architecture</span></h1> <p>An <a href="model_doc/auto"><code>AutoClass</code></a> automatically infers the model architecture and downloads pretrained configuration and weights. Generally, we recommend using an <code>AutoClass</code> to produce checkpoint-agnostic code. But users who want more control over specific model parameters can create a custom 🤗 Transformers model from just a few base classes. This could be particularly useful for anyone who is interested in studying, training or experimenting with a 🤗 Transformers model. In this guide, dive deeper into creating a custom model without an <code>AutoClass</code>. Learn how to:</p> <ul><li>Load and customize a model configuration.</li> <li>Create a model architecture.</li> <li>Create a slow and fast tokenizer for text.</li> <li>Create an image processor for vision tasks.</li> <li>Create a feature extractor for audio tasks.</li> <li>Create a processor for multimodal tasks.</li></ul> <h2 class="relative group"><a id="configuration" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#configuration"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Configuration</span></h2> <p>A <a href="main_classes/configuration">configuration</a> refers to a model’s specific attributes. Each model configuration has different attributes; for instance, all NLP models have the <code>hidden_size</code>, <code>num_attention_heads</code>, <code>num_hidden_layers</code> and <code>vocab_size</code> attributes in common. These attributes specify the number of attention heads or hidden layers to construct a model with.</p> <p>Get a closer look at <a href="model_doc/distilbert">DistilBERT</a> by accessing <a href="/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertConfig">DistilBertConfig</a> to inspect it’s attributes:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertConfig <span class="hljs-meta">&gt;&gt;&gt; </span>config = DistilBertConfig() <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(config) DistilBertConfig { <span class="hljs-string">"activation"</span>: <span class="hljs-string">"gelu"</span>, <span class="hljs-string">"attention_dropout"</span>: <span class="hljs-number">0.1</span>, <span class="hljs-string">"dim"</span>: <span class="hljs-number">768</span>, <span class="hljs-string">"dropout"</span>: <span class="hljs-number">0.1</span>, <span class="hljs-string">"hidden_dim"</span>: <span class="hljs-number">3072</span>, <span class="hljs-string">"initializer_range"</span>: <span class="hljs-number">0.02</span>, <span class="hljs-string">"max_position_embeddings"</span>: <span class="hljs-number">512</span>, <span class="hljs-string">"model_type"</span>: <span class="hljs-string">"distilbert"</span>, <span class="hljs-string">"n_heads"</span>: <span class="hljs-number">12</span>, <span class="hljs-string">"n_layers"</span>: <span class="hljs-number">6</span>, <span class="hljs-string">"pad_token_id"</span>: <span class="hljs-number">0</span>, <span class="hljs-string">"qa_dropout"</span>: <span class="hljs-number">0.1</span>, <span class="hljs-string">"seq_classif_dropout"</span>: <span class="hljs-number">0.2</span>, <span class="hljs-string">"sinusoidal_pos_embds"</span>: false, <span class="hljs-string">"transformers_version"</span>: <span class="hljs-string">"4.16.2"</span>, <span class="hljs-string">"vocab_size"</span>: <span class="hljs-number">30522</span> }</pre></div> <p><a href="/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertConfig">DistilBertConfig</a> displays all the default attributes used to build a base <a href="/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertModel">DistilBertModel</a>. All attributes are customizable, creating space for experimentation. For example, you can customize a default model to:</p> <ul><li>Try a different activation function with the <code>activation</code> parameter.</li> <li>Use a higher dropout ratio for the attention probabilities with the <code>attention_dropout</code> parameter.</li></ul> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>my_config = DistilBertConfig(activation=<span class="hljs-string">"relu"</span>, attention_dropout=<span class="hljs-number">0.4</span>) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(my_config) DistilBertConfig { <span class="hljs-string">"activation"</span>: <span class="hljs-string">"relu"</span>, <span class="hljs-string">"attention_dropout"</span>: <span class="hljs-number">0.4</span>, <span class="hljs-string">"dim"</span>: <span class="hljs-number">768</span>, <span class="hljs-string">"dropout"</span>: <span class="hljs-number">0.1</span>, <span class="hljs-string">"hidden_dim"</span>: <span class="hljs-number">3072</span>, <span class="hljs-string">"initializer_range"</span>: <span class="hljs-number">0.02</span>, <span class="hljs-string">"max_position_embeddings"</span>: <span class="hljs-number">512</span>, <span class="hljs-string">"model_type"</span>: <span class="hljs-string">"distilbert"</span>, <span class="hljs-string">"n_heads"</span>: <span class="hljs-number">12</span>, <span class="hljs-string">"n_layers"</span>: <span class="hljs-number">6</span>, <span class="hljs-string">"pad_token_id"</span>: <span class="hljs-number">0</span>, <span class="hljs-string">"qa_dropout"</span>: <span class="hljs-number">0.1</span>, <span class="hljs-string">"seq_classif_dropout"</span>: <span class="hljs-number">0.2</span>, <span class="hljs-string">"sinusoidal_pos_embds"</span>: false, <span class="hljs-string">"transformers_version"</span>: <span class="hljs-string">"4.16.2"</span>, <span class="hljs-string">"vocab_size"</span>: <span class="hljs-number">30522</span> }</pre></div> <p>Pretrained model attributes can be modified in the <a href="/docs/transformers/v4.30.0/en/main_classes/configuration#transformers.PretrainedConfig.from_pretrained">from_pretrained()</a> function:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>my_config = DistilBertConfig.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>, activation=<span class="hljs-string">"relu"</span>, attention_dropout=<span class="hljs-number">0.4</span>)</pre></div> <p>Once you are satisfied with your model configuration, you can save it with <a href="/docs/transformers/v4.30.0/en/main_classes/configuration#transformers.PretrainedConfig.save_pretrained">save_pretrained()</a>. Your configuration file is stored as a JSON file in the specified save directory:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>my_config.save_pretrained(save_directory=<span class="hljs-string">"./your_model_save_path"</span>)</pre></div> <p>To reuse the configuration file, load it with <a href="/docs/transformers/v4.30.0/en/main_classes/configuration#transformers.PretrainedConfig.from_pretrained">from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>my_config = DistilBertConfig.from_pretrained(<span class="hljs-string">"./your_model_save_path/config.json"</span>)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>You can also save your configuration file as a dictionary or even just the difference between your custom configuration attributes and the default configuration attributes! See the <a href="main_classes/configuration">configuration</a> documentation for more details.</p></div> <h2 class="relative group"><a id="model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Model</span></h2> <p>The next step is to create a <a href="main_classes/models">model</a>. The model - also loosely referred to as the architecture - defines what each layer is doing and what operations are happening. Attributes like <code>num_hidden_layers</code> from the configuration are used to define the architecture. Every model shares the base class <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel">PreTrainedModel</a> and a few common methods like resizing input embeddings and pruning self-attention heads. In addition, all models are also either a <a href="https://pytorch.org/docs/stable/generated/torch.nn.Module.html" rel="nofollow"><code>torch.nn.Module</code></a>, <a href="https://www.tensorflow.org/api_docs/python/tf/keras/Model" rel="nofollow"><code>tf.keras.Model</code></a> or <a href="https://flax.readthedocs.io/en/latest/flax.linen.html#module" rel="nofollow"><code>flax.linen.Module</code></a> subclass. This means models are compatible with each of their respective framework’s usage.</p> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><p>Load your custom configuration attributes into the model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertModel <span class="hljs-meta">&gt;&gt;&gt; </span>my_config = DistilBertConfig.from_pretrained(<span class="hljs-string">"./your_model_save_path/config.json"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = DistilBertModel(my_config)</pre></div> <p>This creates a model with random values instead of pretrained weights. You won’t be able to use this model for anything useful yet until you train it. Training is a costly and time-consuming process. It is generally better to use a pretrained model to obtain better results faster, while using only a fraction of the resources required for training.</p> <p>Create a pretrained model with <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.from_pretrained">from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>model = DistilBertModel.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)</pre></div> <p>When you load pretrained weights, the default model configuration is automatically loaded if the model is provided by 🤗 Transformers. However, you can still replace - some or all of - the default model configuration attributes with your own if you’d like:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>model = DistilBertModel.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>, config=my_config)</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><p>Load your custom configuration attributes into the model:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFDistilBertModel <span class="hljs-meta">&gt;&gt;&gt; </span>my_config = DistilBertConfig.from_pretrained(<span class="hljs-string">"./your_model_save_path/my_config.json"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>tf_model = TFDistilBertModel(my_config)</pre></div> <p>This creates a model with random values instead of pretrained weights. You won’t be able to use this model for anything useful yet until you train it. Training is a costly and time-consuming process. It is generally better to use a pretrained model to obtain better results faster, while using only a fraction of the resources required for training.</p> <p>Create a pretrained model with <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.TFPreTrainedModel.from_pretrained">from_pretrained()</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tf_model = TFDistilBertModel.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)</pre></div> <p>When you load pretrained weights, the default model configuration is automatically loaded if the model is provided by 🤗 Transformers. However, you can still replace - some or all of - the default model configuration attributes with your own if you’d like:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>tf_model = TFDistilBertModel.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>, config=my_config)</pre></div></div></div> </div> <h3 class="relative group"><a id="model-heads" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#model-heads"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Model heads</span></h3> <p>At this point, you have a base DistilBERT model which outputs the <em>hidden states</em>. The hidden states are passed as inputs to a model head to produce the final output. 🤗 Transformers provides a different model head for each task as long as a model supports the task (i.e., you can’t use DistilBERT for a sequence-to-sequence task like translation).</p> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><p>For example, <a href="/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertForSequenceClassification">DistilBertForSequenceClassification</a> is a base DistilBERT model with a sequence classification head. The sequence classification head is a linear layer on top of the pooled outputs.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span>model = DistilBertForSequenceClassification.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)</pre></div> <p>Easily reuse this checkpoint for another task by switching to a different model head. For a question answering task, you would use the <a href="/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertForQuestionAnswering">DistilBertForQuestionAnswering</a> model head. The question answering head is similar to the sequence classification head except it is a linear layer on top of the hidden states output.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertForQuestionAnswering <span class="hljs-meta">&gt;&gt;&gt; </span>model = DistilBertForQuestionAnswering.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><p>For example, <a href="/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.TFDistilBertForSequenceClassification">TFDistilBertForSequenceClassification</a> is a base DistilBERT model with a sequence classification head. The sequence classification head is a linear layer on top of the pooled outputs.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFDistilBertForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span>tf_model = TFDistilBertForSequenceClassification.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)</pre></div> <p>Easily reuse this checkpoint for another task by switching to a different model head. For a question answering task, you would use the <a href="/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.TFDistilBertForQuestionAnswering">TFDistilBertForQuestionAnswering</a> model head. The question answering head is similar to the sequence classification head except it is a linear layer on top of the hidden states output.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFDistilBertForQuestionAnswering <span class="hljs-meta">&gt;&gt;&gt; </span>tf_model = TFDistilBertForQuestionAnswering.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)</pre></div></div></div> </div> <h2 class="relative group"><a id="tokenizer" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tokenizer"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Tokenizer</span></h2> <p>The last base class you need before using a model for textual data is a <a href="main_classes/tokenizer">tokenizer</a> to convert raw text to tensors. There are two types of tokenizers you can use with 🤗 Transformers:</p> <ul><li><a href="/docs/transformers/v4.30.0/en/main_classes/tokenizer#transformers.PreTrainedTokenizer">PreTrainedTokenizer</a>: a Python implementation of a tokenizer.</li> <li><a href="/docs/transformers/v4.30.0/en/main_classes/tokenizer#transformers.PreTrainedTokenizerFast">PreTrainedTokenizerFast</a>: a tokenizer from our Rust-based <a href="https://huggingface.co/docs/tokenizers/python/latest/" rel="nofollow">🤗 Tokenizer</a> library. This tokenizer type is significantly faster - especially during batch tokenization - due to it’s Rust implementation. The fast tokenizer also offers additional methods like <em>offset mapping</em> which maps tokens to their original words or characters.</li></ul> <p>Both tokenizers support common methods such as encoding and decoding, adding new tokens, and managing special tokens.</p> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>Not every model supports a fast tokenizer. Take a look at this <a href="index#supported-frameworks">table</a> to check if a model has fast tokenizer support.</p></div> <p>If you trained your own tokenizer, you can create one from your <em>vocabulary</em> file:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>my_tokenizer = DistilBertTokenizer(vocab_file=<span class="hljs-string">"my_vocab_file.txt"</span>, do_lower_case=<span class="hljs-literal">False</span>, padding_side=<span class="hljs-string">"left"</span>)</pre></div> <p>It is important to remember the vocabulary from a custom tokenizer will be different from the vocabulary generated by a pretrained model’s tokenizer. You need to use a pretrained model’s vocabulary if you are using a pretrained model, otherwise the inputs won’t make sense. Create a tokenizer with a pretrained model’s vocabulary with the <a href="/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertTokenizer">DistilBertTokenizer</a> class:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>slow_tokenizer = DistilBertTokenizer.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)</pre></div> <p>Create a fast tokenizer with the <a href="/docs/transformers/v4.30.0/en/model_doc/distilbert#transformers.DistilBertTokenizerFast">DistilBertTokenizerFast</a> class:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DistilBertTokenizerFast <span class="hljs-meta">&gt;&gt;&gt; </span>fast_tokenizer = DistilBertTokenizerFast.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>)</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>By default, <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoTokenizer">AutoTokenizer</a> will try to load a fast tokenizer. You can disable this behavior by setting <code>use_fast=False</code> in <code>from_pretrained</code>.</p></div> <h2 class="relative group"><a id="image-processor" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#image-processor"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Image Processor</span></h2> <p>An image processor processes vision inputs. It inherits from the base <a href="/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin">ImageProcessingMixin</a> class.</p> <p>To use, create an image processor associated with the model you’re using. For example, create a default <a href="/docs/transformers/v4.30.0/en/model_doc/vit#transformers.ViTImageProcessor">ViTImageProcessor</a> if you are using <a href="model_doc/vit">ViT</a> for image classification:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> ViTImageProcessor <span class="hljs-meta">&gt;&gt;&gt; </span>vit_extractor = ViTImageProcessor() <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(vit_extractor) ViTImageProcessor { <span class="hljs-string">"do_normalize"</span>: true, <span class="hljs-string">"do_resize"</span>: true, <span class="hljs-string">"feature_extractor_type"</span>: <span class="hljs-string">"ViTImageProcessor"</span>, <span class="hljs-string">"image_mean"</span>: [ <span class="hljs-number">0.5</span>, <span class="hljs-number">0.5</span>, <span class="hljs-number">0.5</span> ], <span class="hljs-string">"image_std"</span>: [ <span class="hljs-number">0.5</span>, <span class="hljs-number">0.5</span>, <span class="hljs-number">0.5</span> ], <span class="hljs-string">"resample"</span>: <span class="hljs-number">2</span>, <span class="hljs-string">"size"</span>: <span class="hljs-number">224</span> }</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>If you aren’t looking for any customization, just use the <code>from_pretrained</code> method to load a model’s default image processor parameters.</p></div> <p>Modify any of the <a href="/docs/transformers/v4.30.0/en/model_doc/vit#transformers.ViTImageProcessor">ViTImageProcessor</a> parameters to create your custom image processor:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> ViTImageProcessor <span class="hljs-meta">&gt;&gt;&gt; </span>my_vit_extractor = ViTImageProcessor(resample=<span class="hljs-string">"PIL.Image.BOX"</span>, do_normalize=<span class="hljs-literal">False</span>, image_mean=[<span class="hljs-number">0.3</span>, <span class="hljs-number">0.3</span>, <span class="hljs-number">0.3</span>]) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(my_vit_extractor) ViTImageProcessor { <span class="hljs-string">"do_normalize"</span>: false, <span class="hljs-string">"do_resize"</span>: true, <span class="hljs-string">"feature_extractor_type"</span>: <span class="hljs-string">"ViTImageProcessor"</span>, <span class="hljs-string">"image_mean"</span>: [ <span class="hljs-number">0.3</span>, <span class="hljs-number">0.3</span>, <span class="hljs-number">0.3</span> ], <span class="hljs-string">"image_std"</span>: [ <span class="hljs-number">0.5</span>, <span class="hljs-number">0.5</span>, <span class="hljs-number">0.5</span> ], <span class="hljs-string">"resample"</span>: <span class="hljs-string">"PIL.Image.BOX"</span>, <span class="hljs-string">"size"</span>: <span class="hljs-number">224</span> }</pre></div> <h2 class="relative group"><a id="feature-extractor" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#feature-extractor"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Feature Extractor</span></h2> <p>A feature extractor processes audio inputs. It inherits from the base <a href="/docs/transformers/v4.30.0/en/main_classes/feature_extractor#transformers.FeatureExtractionMixin">FeatureExtractionMixin</a> class, and may also inherit from the <a href="/docs/transformers/v4.30.0/en/main_classes/feature_extractor#transformers.SequenceFeatureExtractor">SequenceFeatureExtractor</a> class for processing audio inputs.</p> <p>To use, create a feature extractor associated with the model you’re using. For example, create a default <a href="/docs/transformers/v4.30.0/en/model_doc/wav2vec2#transformers.Wav2Vec2FeatureExtractor">Wav2Vec2FeatureExtractor</a> if you are using <a href="model_doc/wav2vec2">Wav2Vec2</a> for audio classification:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Wav2Vec2FeatureExtractor <span class="hljs-meta">&gt;&gt;&gt; </span>w2v2_extractor = Wav2Vec2FeatureExtractor() <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(w2v2_extractor) Wav2Vec2FeatureExtractor { <span class="hljs-string">"do_normalize"</span>: true, <span class="hljs-string">"feature_extractor_type"</span>: <span class="hljs-string">"Wav2Vec2FeatureExtractor"</span>, <span class="hljs-string">"feature_size"</span>: <span class="hljs-number">1</span>, <span class="hljs-string">"padding_side"</span>: <span class="hljs-string">"right"</span>, <span class="hljs-string">"padding_value"</span>: <span class="hljs-number">0.0</span>, <span class="hljs-string">"return_attention_mask"</span>: false, <span class="hljs-string">"sampling_rate"</span>: <span class="hljs-number">16000</span> }</pre></div> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>If you aren’t looking for any customization, just use the <code>from_pretrained</code> method to load a model’s default feature extractor parameters.</p></div> <p>Modify any of the <a href="/docs/transformers/v4.30.0/en/model_doc/wav2vec2#transformers.Wav2Vec2FeatureExtractor">Wav2Vec2FeatureExtractor</a> parameters to create your custom feature extractor:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Wav2Vec2FeatureExtractor <span class="hljs-meta">&gt;&gt;&gt; </span>w2v2_extractor = Wav2Vec2FeatureExtractor(sampling_rate=<span class="hljs-number">8000</span>, do_normalize=<span class="hljs-literal">False</span>) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(w2v2_extractor) Wav2Vec2FeatureExtractor { <span class="hljs-string">"do_normalize"</span>: false, <span class="hljs-string">"feature_extractor_type"</span>: <span class="hljs-string">"Wav2Vec2FeatureExtractor"</span>, <span class="hljs-string">"feature_size"</span>: <span class="hljs-number">1</span>, <span class="hljs-string">"padding_side"</span>: <span class="hljs-string">"right"</span>, <span class="hljs-string">"padding_value"</span>: <span class="hljs-number">0.0</span>, <span class="hljs-string">"return_attention_mask"</span>: false, <span class="hljs-string">"sampling_rate"</span>: <span class="hljs-number">8000</span> }</pre></div> <h2 class="relative group"><a id="processor" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#processor"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Processor</span></h2> <p>For models that support multimodal tasks, 🤗 Transformers offers a processor class that conveniently wraps processing classes such as a feature extractor and a tokenizer into a single object. For example, let’s use the <a href="/docs/transformers/v4.30.0/en/model_doc/wav2vec2#transformers.Wav2Vec2Processor">Wav2Vec2Processor</a> for an automatic speech recognition task (ASR). ASR transcribes audio to text, so you will need a feature extractor and a tokenizer.</p> <p>Create a feature extractor to handle the audio inputs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Wav2Vec2FeatureExtractor <span class="hljs-meta">&gt;&gt;&gt; </span>feature_extractor = Wav2Vec2FeatureExtractor(padding_value=<span class="hljs-number">1.0</span>, do_normalize=<span class="hljs-literal">True</span>)</pre></div> <p>Create a tokenizer to handle the text inputs:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Wav2Vec2CTCTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = Wav2Vec2CTCTokenizer(vocab_file=<span class="hljs-string">"my_vocab_file.txt"</span>)</pre></div> <p>Combine the feature extractor and tokenizer in <a href="/docs/transformers/v4.30.0/en/model_doc/wav2vec2#transformers.Wav2Vec2Processor">Wav2Vec2Processor</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Wav2Vec2Processor <span class="hljs-meta">&gt;&gt;&gt; </span>processor = Wav2Vec2Processor(feature_extractor=feature_extractor, tokenizer=tokenizer)</pre></div> <p>With two basic classes - configuration and model - and an additional preprocessing class (tokenizer, image processor, feature extractor, or processor), you can create any of the models supported by 🤗 Transformers. Each of these base classes are configurable, allowing you to use the specific attributes you want. You can easily setup a model for training or modify an existing pretrained model to fine-tune.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/generation_strategies" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Customize text generation strategy</a> <a href="/docs/transformers/custom_models" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Share a custom model<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Create a custom architecture&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create-a-custom-architecture&quot;,&quot;url&quot;:&quot;#create-a-custom-architecture&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;configuration&quot;,&quot;url&quot;:&quot;#configuration&quot;},{&quot;title&quot;:&quot;Model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model&quot;,&quot;url&quot;:&quot;#model&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Model heads&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model-heads&quot;,&quot;url&quot;:&quot;#model-heads&quot;}]},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer&quot;,&quot;url&quot;:&quot;#tokenizer&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;image-processor&quot;,&quot;url&quot;:&quot;#image-processor&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;feature-extractor&quot;,&quot;url&quot;:&quot;#feature-extractor&quot;},{&quot;title&quot;:&quot;Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;processor&quot;,&quot;url&quot;:&quot;#processor&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#create-a-custom-architecture" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-create-a-custom-architecture"><wbr>Create a custom architecture</a> <a href="#configuration" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-configuration"><wbr>Configuration</a> <a href="#model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-model"><wbr>Model</a> <a href="#model-heads" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-model-heads"><wbr>Model heads</a> <a href="#tokenizer" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tokenizer"><wbr>Tokenizer</a> <a href="#image-processor" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-image-processor"><wbr>Image <wbr>Processor</a> <a href="#feature-extractor" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-feature-extractor"><wbr>Feature <wbr>Extractor</a> <a href="#processor" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-processor"><wbr>Processor</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/create_a_model" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/create_a_model"); } </script> <iframe name="__privateStripeMetricsController9620" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fcreate_a_model&amp;title=Create%20a%20custom%20architecture&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:11.143Z
Sharing custom models
https://huggingface.co/docs/transformers/custom_models
The 🤗 Transformers library is designed to be easily extensible. Every model is fully coded in a given subfolder of the repository with no abstraction, so you can easily copy a modeling file and tweak it to your needs. If you are writing a brand new model, it might be easier to start from scratch. In this tutorial, we will show you how to write a custom model and its configuration so it can be used inside Transformers, and how you can share it with the community (with the code it relies on) so that anyone can use it, even if it’s not present in the 🤗 Transformers library. We will illustrate all of this on a ResNet model, by wrapping the ResNet class of the [timm library](https://github.com/rwightman/pytorch-image-models) into a [PreTrainedModel](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel). ## [](#writing-a-custom-configuration)Writing a custom configuration Before we dive into the model, let’s first write its configuration. The configuration of a model is an object that will contain all the necessary information to build the model. As we will see in the next section, the model can only take a `config` to be initialized, so we really need that object to be as complete as possible. In our example, we will take a couple of arguments of the ResNet class that we might want to tweak. Different configurations will then give us the different types of ResNets that are possible. We then just store those arguments, after checking the validity of a few of them. ``` from transformers import PretrainedConfig from typing import List class ResnetConfig(PretrainedConfig): model_type = "resnet" def __init__( self, block_type="bottleneck", layers: List[int] = [3, 4, 6, 3], num_classes: int = 1000, input_channels: int = 3, cardinality: int = 1, base_width: int = 64, stem_width: int = 64, stem_type: str = "", avg_down: bool = False, **kwargs, ): if block_type not in ["basic", "bottleneck"]: raise ValueError(f"`block_type` must be 'basic' or bottleneck', got {block_type}.") if stem_type not in ["", "deep", "deep-tiered"]: raise ValueError(f"`stem_type` must be '', 'deep' or 'deep-tiered', got {stem_type}.") self.block_type = block_type self.layers = layers self.num_classes = num_classes self.input_channels = input_channels self.cardinality = cardinality self.base_width = base_width self.stem_width = stem_width self.stem_type = stem_type self.avg_down = avg_down super().__init__(**kwargs)``` The three important things to remember when writing you own configuration are the following: - you have to inherit from `PretrainedConfig`, - the `__init__` of your `PretrainedConfig` must accept any kwargs, - those `kwargs` need to be passed to the superclass `__init__`. The inheritance is to make sure you get all the functionality from the 🤗 Transformers library, while the two other constraints come from the fact a `PretrainedConfig` has more fields than the ones you are setting. When reloading a config with the `from_pretrained` method, those fields need to be accepted by your config and then sent to the superclass. Defining a `model_type` for your configuration (here `model_type="resnet"`) is not mandatory, unless you want to register your model with the auto classes (see last section). With this done, you can easily create and save your configuration like you would do with any other model config of the library. Here is how we can create a resnet50d config and save it: ``` resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True) resnet50d_config.save_pretrained("custom-resnet")``` This will save a file named `config.json` inside the folder `custom-resnet`. You can then reload your config with the `from_pretrained` method: ``` resnet50d_config = ResnetConfig.from_pretrained("custom-resnet")``` You can also use any other method of the [PretrainedConfig](/docs/transformers/v4.30.0/en/main_classes/configuration#transformers.PretrainedConfig) class, like [push\_to\_hub()](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin.push_to_hub) to directly upload your config to the Hub. ## [](#writing-a-custom-model)Writing a custom model Now that we have our ResNet configuration, we can go on writing the model. We will actually write two: one that extracts the hidden features from a batch of images (like [BertModel](/docs/transformers/v4.30.0/en/model_doc/bert#transformers.BertModel)) and one that is suitable for image classification (like [BertForSequenceClassification](/docs/transformers/v4.30.0/en/model_doc/bert#transformers.BertForSequenceClassification)). As we mentioned before, we’ll only write a loose wrapper of the model to keep it simple for this example. The only thing we need to do before writing this class is a map between the block types and actual block classes. Then the model is defined from the configuration by passing everything to the `ResNet` class: ``` from transformers import PreTrainedModel from timm.models.resnet import BasicBlock, Bottleneck, ResNet from .configuration_resnet import ResnetConfig BLOCK_MAPPING = {"basic": BasicBlock, "bottleneck": Bottleneck} class ResnetModel(PreTrainedModel): config_class = ResnetConfig def __init__(self, config): super().__init__(config) block_layer = BLOCK_MAPPING[config.block_type] self.model = ResNet( block_layer, config.layers, num_classes=config.num_classes, in_chans=config.input_channels, cardinality=config.cardinality, base_width=config.base_width, stem_width=config.stem_width, stem_type=config.stem_type, avg_down=config.avg_down, ) def forward(self, tensor): return self.model.forward_features(tensor)``` For the model that will classify images, we just change the forward method: ``` import torch class ResnetModelForImageClassification(PreTrainedModel): config_class = ResnetConfig def __init__(self, config): super().__init__(config) block_layer = BLOCK_MAPPING[config.block_type] self.model = ResNet( block_layer, config.layers, num_classes=config.num_classes, in_chans=config.input_channels, cardinality=config.cardinality, base_width=config.base_width, stem_width=config.stem_width, stem_type=config.stem_type, avg_down=config.avg_down, ) def forward(self, tensor, labels=None): logits = self.model(tensor) if labels is not None: loss = torch.nn.cross_entropy(logits, labels) return {"loss": loss, "logits": logits} return {"logits": logits}``` In both cases, notice how we inherit from `PreTrainedModel` and call the superclass initialization with the `config` (a bit like when you write a regular `torch.nn.Module`). The line that sets the `config_class` is not mandatory, unless you want to register your model with the auto classes (see last section). If your model is very similar to a model inside the library, you can re-use the same configuration as this model. You can have your model return anything you want, but returning a dictionary like we did for `ResnetModelForImageClassification`, with the loss included when labels are passed, will make your model directly usable inside the [Trainer](/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer) class. Using another output format is fine as long as you are planning on using your own training loop or another library for training. Now that we have our model class, let’s create one: ``` resnet50d = ResnetModelForImageClassification(resnet50d_config)``` Again, you can use any of the methods of [PreTrainedModel](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel), like [save\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.save_pretrained) or [push\_to\_hub()](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin.push_to_hub). We will use the second in the next section, and see how to push the model weights with the code of our model. But first, let’s load some pretrained weights inside our model. In your own use case, you will probably be training your custom model on your own data. To go fast for this tutorial, we will use the pretrained version of the resnet50d. Since our model is just a wrapper around it, it’s going to be easy to transfer those weights: ``` import timm pretrained_model = timm.create_model("resnet50d", pretrained=True) resnet50d.model.load_state_dict(pretrained_model.state_dict())``` Now let’s see how to make sure that when we do [save\_pretrained()](/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.save_pretrained) or [push\_to\_hub()](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin.push_to_hub), the code of the model is saved. ## [](#sending-the-code-to-the-hub)Sending the code to the Hub This API is experimental and may have some slight breaking changes in the next releases. First, make sure your model is fully defined in a `.py` file. It can rely on relative imports to some other files as long as all the files are in the same directory (we don’t support submodules for this feature yet). For our example, we’ll define a `modeling_resnet.py` file and a `configuration_resnet.py` file in a folder of the current working directory named `resnet_model`. The configuration file contains the code for `ResnetConfig` and the modeling file contains the code of `ResnetModel` and `ResnetModelForImageClassification`. ``` . └── resnet_model ├── __init__.py ├── configuration_resnet.py └── modeling_resnet.py``` The `__init__.py` can be empty, it’s just there so that Python detects `resnet_model` can be use as a module. If copying a modeling files from the library, you will need to replace all the relative imports at the top of the file to import from the `transformers` package. Note that you can re-use (or subclass) an existing configuration/model. To share your model with the community, follow those steps: first import the ResNet model and config from the newly created files: ``` from resnet_model.configuration_resnet import ResnetConfig from resnet_model.modeling_resnet import ResnetModel, ResnetModelForImageClassification``` Then you have to tell the library you want to copy the code files of those objects when using the `save_pretrained` method and properly register them with a given Auto class (especially for models), just run: ``` ResnetConfig.register_for_auto_class() ResnetModel.register_for_auto_class("AutoModel") ResnetModelForImageClassification.register_for_auto_class("AutoModelForImageClassification")``` Note that there is no need to specify an auto class for the configuration (there is only one auto class for them, [AutoConfig](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoConfig)) but it’s different for models. Your custom model could be suitable for many different tasks, so you have to specify which one of the auto classes is the correct one for your model. Next, let’s create the config and models as we did before: ``` resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True) resnet50d = ResnetModelForImageClassification(resnet50d_config) pretrained_model = timm.create_model("resnet50d", pretrained=True) resnet50d.model.load_state_dict(pretrained_model.state_dict())``` Now to send the model to the Hub, make sure you are logged in. Either run in your terminal: or from a notebook: ``` from huggingface_hub import notebook_login notebook_login()``` You can then push to your own namespace (or an organization you are a member of) like this: ``` resnet50d.push_to_hub("custom-resnet50d")``` On top of the modeling weights and the configuration in json format, this also copied the modeling and configuration `.py` files in the folder `custom-resnet50d` and uploaded the result to the Hub. You can check the result in this [model repo](https://huggingface.co/sgugger/custom-resnet50d). See the [sharing tutorial](model_sharing) for more information on the push to Hub method. ## [](#using-a-model-with-custom-code)Using a model with custom code You can use any configuration, model or tokenizer with custom code files in its repository with the auto-classes and the `from_pretrained` method. All files and code uploaded to the Hub are scanned for malware (refer to the [Hub security](https://huggingface.co/docs/hub/security#malware-scanning) documentation for more information), but you should still review the model code and author to avoid executing malicious code on your machine. Set `trust_remote_code=True` to use a model with custom code: ``` from transformers import AutoModelForImageClassification model = AutoModelForImageClassification.from_pretrained("sgugger/custom-resnet50d", trust_remote_code=True)``` It is also strongly encouraged to pass a commit hash as a `revision` to make sure the author of the models did not update the code with some malicious new lines (unless you fully trust the authors of the models). ``` commit_hash = "ed94a7c6247d8aedce4647f00f20de6875b5b292" model = AutoModelForImageClassification.from_pretrained( "sgugger/custom-resnet50d", trust_remote_code=True, revision=commit_hash )``` Note that when browsing the commit history of the model repo on the Hub, there is a button to easily copy the commit hash of any commit. ## [](#registering-a-model-with-custom-code-to-the-auto-classes)Registering a model with custom code to the auto classes If you are writing a library that extends 🤗 Transformers, you may want to extend the auto classes to include your own model. This is different from pushing the code to the Hub in the sense that users will need to import your library to get the custom models (contrarily to automatically downloading the model code from the Hub). As long as your config has a `model_type` attribute that is different from existing model types, and that your model classes have the right `config_class` attributes, you can just add them to the auto classes likes this: ``` from transformers import AutoConfig, AutoModel, AutoModelForImageClassification AutoConfig.register("resnet", ResnetConfig) AutoModel.register(ResnetConfig, ResnetModel) AutoModelForImageClassification.register(ResnetConfig, ResnetModelForImageClassification)``` Note that the first argument used when registering your custom config to [AutoConfig](/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoConfig) needs to match the `model_type` of your custom config, and the first argument used when registering your custom models to any auto model class needs to match the `config_class` of those models.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Sharing custom models"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/custom_models"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Sharing custom models</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;sharing-custom-models&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;writing-a-custom-configuration&quot;,&quot;title&quot;:&quot;Writing a custom configuration&quot;},{&quot;local&quot;:&quot;writing-a-custom-model&quot;,&quot;title&quot;:&quot;Writing a custom model&quot;},{&quot;local&quot;:&quot;sending-the-code-to-the-hub&quot;,&quot;title&quot;:&quot;Sending the code to the Hub&quot;},{&quot;local&quot;:&quot;using-a-model-with-custom-code&quot;,&quot;title&quot;:&quot;Using a model with custom code&quot;},{&quot;local&quot;:&quot;registering-a-model-with-custom-code-to-the-auto-classes&quot;,&quot;title&quot;:&quot;Registering a model with custom code to the auto classes&quot;}],&quot;title&quot;:&quot;Sharing custom models&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;custom_models&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Sharing custom models&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Sharing custom models</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="sharing-custom-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#sharing-custom-models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Sharing custom models</span></h1> <p>The 🤗 Transformers library is designed to be easily extensible. Every model is fully coded in a given subfolder of the repository with no abstraction, so you can easily copy a modeling file and tweak it to your needs.</p> <p>If you are writing a brand new model, it might be easier to start from scratch. In this tutorial, we will show you how to write a custom model and its configuration so it can be used inside Transformers, and how you can share it with the community (with the code it relies on) so that anyone can use it, even if it’s not present in the 🤗 Transformers library.</p> <p>We will illustrate all of this on a ResNet model, by wrapping the ResNet class of the <a href="https://github.com/rwightman/pytorch-image-models" rel="nofollow">timm library</a> into a <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel">PreTrainedModel</a>.</p> <h2 class="relative group"><a id="writing-a-custom-configuration" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#writing-a-custom-configuration"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Writing a custom configuration</span></h2> <p>Before we dive into the model, let’s first write its configuration. The configuration of a model is an object that will contain all the necessary information to build the model. As we will see in the next section, the model can only take a <code>config</code> to be initialized, so we really need that object to be as complete as possible.</p> <p>In our example, we will take a couple of arguments of the ResNet class that we might want to tweak. Different configurations will then give us the different types of ResNets that are possible. We then just store those arguments, after checking the validity of a few of them.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PretrainedConfig <span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> <span class="hljs-type">List</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ResnetConfig</span>(<span class="hljs-title class_ inherited__">PretrainedConfig</span>): model_type = <span class="hljs-string">"resnet"</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params"> self, block_type=<span class="hljs-string">"bottleneck"</span>, layers: <span class="hljs-type">List</span>[<span class="hljs-built_in">int</span>] = [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">3</span>], num_classes: <span class="hljs-built_in">int</span> = <span class="hljs-number">1000</span>, input_channels: <span class="hljs-built_in">int</span> = <span class="hljs-number">3</span>, cardinality: <span class="hljs-built_in">int</span> = <span class="hljs-number">1</span>, base_width: <span class="hljs-built_in">int</span> = <span class="hljs-number">64</span>, stem_width: <span class="hljs-built_in">int</span> = <span class="hljs-number">64</span>, stem_type: <span class="hljs-built_in">str</span> = <span class="hljs-string">""</span>, avg_down: <span class="hljs-built_in">bool</span> = <span class="hljs-literal">False</span>, **kwargs, </span>): <span class="hljs-keyword">if</span> block_type <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> [<span class="hljs-string">"basic"</span>, <span class="hljs-string">"bottleneck"</span>]: <span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">f"`block_type` must be 'basic' or bottleneck', got <span class="hljs-subst">{block_type}</span>."</span>) <span class="hljs-keyword">if</span> stem_type <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> [<span class="hljs-string">""</span>, <span class="hljs-string">"deep"</span>, <span class="hljs-string">"deep-tiered"</span>]: <span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">f"`stem_type` must be '', 'deep' or 'deep-tiered', got <span class="hljs-subst">{stem_type}</span>."</span>) self.block_type = block_type self.layers = layers self.num_classes = num_classes self.input_channels = input_channels self.cardinality = cardinality self.base_width = base_width self.stem_width = stem_width self.stem_type = stem_type self.avg_down = avg_down <span class="hljs-built_in">super</span>().__init__(**kwargs)</pre></div> <p>The three important things to remember when writing you own configuration are the following:</p> <ul><li>you have to inherit from <code>PretrainedConfig</code>,</li> <li>the <code>__init__</code> of your <code>PretrainedConfig</code> must accept any kwargs,</li> <li>those <code>kwargs</code> need to be passed to the superclass <code>__init__</code>.</li></ul> <p>The inheritance is to make sure you get all the functionality from the 🤗 Transformers library, while the two other constraints come from the fact a <code>PretrainedConfig</code> has more fields than the ones you are setting. When reloading a config with the <code>from_pretrained</code> method, those fields need to be accepted by your config and then sent to the superclass.</p> <p>Defining a <code>model_type</code> for your configuration (here <code>model_type="resnet"</code>) is not mandatory, unless you want to register your model with the auto classes (see last section).</p> <p>With this done, you can easily create and save your configuration like you would do with any other model config of the library. Here is how we can create a resnet50d config and save it:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>resnet50d_config = ResnetConfig(block_type=<span class="hljs-string">"bottleneck"</span>, stem_width=<span class="hljs-number">32</span>, stem_type=<span class="hljs-string">"deep"</span>, avg_down=<span class="hljs-literal">True</span>) resnet50d_config.save_pretrained(<span class="hljs-string">"custom-resnet"</span>)</pre></div> <p>This will save a file named <code>config.json</code> inside the folder <code>custom-resnet</code>. You can then reload your config with the <code>from_pretrained</code> method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>resnet50d_config = ResnetConfig.from_pretrained(<span class="hljs-string">"custom-resnet"</span>)</pre></div> <p>You can also use any other method of the <a href="/docs/transformers/v4.30.0/en/main_classes/configuration#transformers.PretrainedConfig">PretrainedConfig</a> class, like <a href="/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin.push_to_hub">push_to_hub()</a> to directly upload your config to the Hub.</p> <h2 class="relative group"><a id="writing-a-custom-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#writing-a-custom-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Writing a custom model</span></h2> <p>Now that we have our ResNet configuration, we can go on writing the model. We will actually write two: one that extracts the hidden features from a batch of images (like <a href="/docs/transformers/v4.30.0/en/model_doc/bert#transformers.BertModel">BertModel</a>) and one that is suitable for image classification (like <a href="/docs/transformers/v4.30.0/en/model_doc/bert#transformers.BertForSequenceClassification">BertForSequenceClassification</a>).</p> <p>As we mentioned before, we’ll only write a loose wrapper of the model to keep it simple for this example. The only thing we need to do before writing this class is a map between the block types and actual block classes. Then the model is defined from the configuration by passing everything to the <code>ResNet</code> class:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PreTrainedModel <span class="hljs-keyword">from</span> timm.models.resnet <span class="hljs-keyword">import</span> BasicBlock, Bottleneck, ResNet <span class="hljs-keyword">from</span> .configuration_resnet <span class="hljs-keyword">import</span> ResnetConfig BLOCK_MAPPING = {<span class="hljs-string">"basic"</span>: BasicBlock, <span class="hljs-string">"bottleneck"</span>: Bottleneck} <span class="hljs-keyword">class</span> <span class="hljs-title class_">ResnetModel</span>(<span class="hljs-title class_ inherited__">PreTrainedModel</span>): config_class = ResnetConfig <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config</span>): <span class="hljs-built_in">super</span>().__init__(config) block_layer = BLOCK_MAPPING[config.block_type] self.model = ResNet( block_layer, config.layers, num_classes=config.num_classes, in_chans=config.input_channels, cardinality=config.cardinality, base_width=config.base_width, stem_width=config.stem_width, stem_type=config.stem_type, avg_down=config.avg_down, ) <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, tensor</span>): <span class="hljs-keyword">return</span> self.model.forward_features(tensor)</pre></div> <p>For the model that will classify images, we just change the forward method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> torch <span class="hljs-keyword">class</span> <span class="hljs-title class_">ResnetModelForImageClassification</span>(<span class="hljs-title class_ inherited__">PreTrainedModel</span>): config_class = ResnetConfig <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config</span>): <span class="hljs-built_in">super</span>().__init__(config) block_layer = BLOCK_MAPPING[config.block_type] self.model = ResNet( block_layer, config.layers, num_classes=config.num_classes, in_chans=config.input_channels, cardinality=config.cardinality, base_width=config.base_width, stem_width=config.stem_width, stem_type=config.stem_type, avg_down=config.avg_down, ) <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, tensor, labels=<span class="hljs-literal">None</span></span>): logits = self.model(tensor) <span class="hljs-keyword">if</span> labels <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>: loss = torch.nn.cross_entropy(logits, labels) <span class="hljs-keyword">return</span> {<span class="hljs-string">"loss"</span>: loss, <span class="hljs-string">"logits"</span>: logits} <span class="hljs-keyword">return</span> {<span class="hljs-string">"logits"</span>: logits}</pre></div> <p>In both cases, notice how we inherit from <code>PreTrainedModel</code> and call the superclass initialization with the <code>config</code> (a bit like when you write a regular <code>torch.nn.Module</code>). The line that sets the <code>config_class</code> is not mandatory, unless you want to register your model with the auto classes (see last section).</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>If your model is very similar to a model inside the library, you can re-use the same configuration as this model.</p></div> <p>You can have your model return anything you want, but returning a dictionary like we did for <code>ResnetModelForImageClassification</code>, with the loss included when labels are passed, will make your model directly usable inside the <a href="/docs/transformers/v4.30.0/en/main_classes/trainer#transformers.Trainer">Trainer</a> class. Using another output format is fine as long as you are planning on using your own training loop or another library for training.</p> <p>Now that we have our model class, let’s create one:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>resnet50d = ResnetModelForImageClassification(resnet50d_config)</pre></div> <p>Again, you can use any of the methods of <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel">PreTrainedModel</a>, like <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.save_pretrained">save_pretrained()</a> or <a href="/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin.push_to_hub">push_to_hub()</a>. We will use the second in the next section, and see how to push the model weights with the code of our model. But first, let’s load some pretrained weights inside our model.</p> <p>In your own use case, you will probably be training your custom model on your own data. To go fast for this tutorial, we will use the pretrained version of the resnet50d. Since our model is just a wrapper around it, it’s going to be easy to transfer those weights:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">import</span> timm pretrained_model = timm.create_model(<span class="hljs-string">"resnet50d"</span>, pretrained=<span class="hljs-literal">True</span>) resnet50d.model.load_state_dict(pretrained_model.state_dict())</pre></div> <p>Now let’s see how to make sure that when we do <a href="/docs/transformers/v4.30.0/en/main_classes/model#transformers.PreTrainedModel.save_pretrained">save_pretrained()</a> or <a href="/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin.push_to_hub">push_to_hub()</a>, the code of the model is saved.</p> <h2 class="relative group"><a id="sending-the-code-to-the-hub" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#sending-the-code-to-the-hub"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Sending the code to the Hub</span></h2> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>This API is experimental and may have some slight breaking changes in the next releases.</p></div> <p>First, make sure your model is fully defined in a <code>.py</code> file. It can rely on relative imports to some other files as long as all the files are in the same directory (we don’t support submodules for this feature yet). For our example, we’ll define a <code>modeling_resnet.py</code> file and a <code>configuration_resnet.py</code> file in a folder of the current working directory named <code>resnet_model</code>. The configuration file contains the code for <code>ResnetConfig</code> and the modeling file contains the code of <code>ResnetModel</code> and <code>ResnetModelForImageClassification</code>.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>. └── resnet_model ├── __init__.<span class="hljs-keyword">py</span> ├── configuration_resnet.<span class="hljs-keyword">py</span> └── modeling_resnet.<span class="hljs-keyword">py</span></pre></div> <p>The <code>__init__.py</code> can be empty, it’s just there so that Python detects <code>resnet_model</code> can be use as a module.</p> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>If copying a modeling files from the library, you will need to replace all the relative imports at the top of the file to import from the <code>transformers</code> package.</p></div> <p>Note that you can re-use (or subclass) an existing configuration/model.</p> <p>To share your model with the community, follow those steps: first import the ResNet model and config from the newly created files:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> resnet_model.configuration_resnet <span class="hljs-keyword">import</span> ResnetConfig <span class="hljs-keyword">from</span> resnet_model.modeling_resnet <span class="hljs-keyword">import</span> ResnetModel, ResnetModelForImageClassification</pre></div> <p>Then you have to tell the library you want to copy the code files of those objects when using the <code>save_pretrained</code> method and properly register them with a given Auto class (especially for models), just run:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>ResnetConfig.register_for_auto_class() ResnetModel.register_for_auto_class(<span class="hljs-string">"AutoModel"</span>) ResnetModelForImageClassification.register_for_auto_class(<span class="hljs-string">"AutoModelForImageClassification"</span>)</pre></div> <p>Note that there is no need to specify an auto class for the configuration (there is only one auto class for them, <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoConfig">AutoConfig</a>) but it’s different for models. Your custom model could be suitable for many different tasks, so you have to specify which one of the auto classes is the correct one for your model.</p> <p>Next, let’s create the config and models as we did before:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>resnet50d_config = ResnetConfig(block_type=<span class="hljs-string">"bottleneck"</span>, stem_width=<span class="hljs-number">32</span>, stem_type=<span class="hljs-string">"deep"</span>, avg_down=<span class="hljs-literal">True</span>) resnet50d = ResnetModelForImageClassification(resnet50d_config) pretrained_model = timm.create_model(<span class="hljs-string">"resnet50d"</span>, pretrained=<span class="hljs-literal">True</span>) resnet50d.model.load_state_dict(pretrained_model.state_dict())</pre></div> <p>Now to send the model to the Hub, make sure you are logged in. Either run in your terminal:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>huggingface-cli login</pre></div> <p>or from a notebook:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login notebook_login()</pre></div> <p>You can then push to your own namespace (or an organization you are a member of) like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>resnet50d.push_to_hub(<span class="hljs-string">"custom-resnet50d"</span>)</pre></div> <p>On top of the modeling weights and the configuration in json format, this also copied the modeling and configuration <code>.py</code> files in the folder <code>custom-resnet50d</code> and uploaded the result to the Hub. You can check the result in this <a href="https://huggingface.co/sgugger/custom-resnet50d" rel="nofollow">model repo</a>.</p> <p>See the <a href="model_sharing">sharing tutorial</a> for more information on the push to Hub method.</p> <h2 class="relative group"><a id="using-a-model-with-custom-code" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-a-model-with-custom-code"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using a model with custom code</span></h2> <p>You can use any configuration, model or tokenizer with custom code files in its repository with the auto-classes and the <code>from_pretrained</code> method. All files and code uploaded to the Hub are scanned for malware (refer to the <a href="https://huggingface.co/docs/hub/security#malware-scanning" rel="nofollow">Hub security</a> documentation for more information), but you should still review the model code and author to avoid executing malicious code on your machine. Set <code>trust_remote_code=True</code> to use a model with custom code:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForImageClassification model = AutoModelForImageClassification.from_pretrained(<span class="hljs-string">"sgugger/custom-resnet50d"</span>, trust_remote_code=<span class="hljs-literal">True</span>)</pre></div> <p>It is also strongly encouraged to pass a commit hash as a <code>revision</code> to make sure the author of the models did not update the code with some malicious new lines (unless you fully trust the authors of the models).</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>commit_hash = <span class="hljs-string">"ed94a7c6247d8aedce4647f00f20de6875b5b292"</span> model = AutoModelForImageClassification.from_pretrained( <span class="hljs-string">"sgugger/custom-resnet50d"</span>, trust_remote_code=<span class="hljs-literal">True</span>, revision=commit_hash )</pre></div> <p>Note that when browsing the commit history of the model repo on the Hub, there is a button to easily copy the commit hash of any commit.</p> <h2 class="relative group"><a id="registering-a-model-with-custom-code-to-the-auto-classes" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#registering-a-model-with-custom-code-to-the-auto-classes"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Registering a model with custom code to the auto classes</span></h2> <p>If you are writing a library that extends 🤗 Transformers, you may want to extend the auto classes to include your own model. This is different from pushing the code to the Hub in the sense that users will need to import your library to get the custom models (contrarily to automatically downloading the model code from the Hub).</p> <p>As long as your config has a <code>model_type</code> attribute that is different from existing model types, and that your model classes have the right <code>config_class</code> attributes, you can just add them to the auto classes likes this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoConfig, AutoModel, AutoModelForImageClassification AutoConfig.register(<span class="hljs-string">"resnet"</span>, ResnetConfig) AutoModel.register(ResnetConfig, ResnetModel) AutoModelForImageClassification.register(ResnetConfig, ResnetModelForImageClassification)</pre></div> <p>Note that the first argument used when registering your custom config to <a href="/docs/transformers/v4.30.0/en/model_doc/auto#transformers.AutoConfig">AutoConfig</a> needs to match the <code>model_type</code> of your custom config, and the first argument used when registering your custom models to any auto model class needs to match the <code>config_class</code> of those models.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/create_a_model" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Use model-specific APIs</a> <a href="/docs/transformers/sagemaker" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Run training on Amazon SageMaker<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Sharing custom models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sharing-custom-models&quot;,&quot;url&quot;:&quot;#sharing-custom-models&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Writing a custom configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;writing-a-custom-configuration&quot;,&quot;url&quot;:&quot;#writing-a-custom-configuration&quot;},{&quot;title&quot;:&quot;Writing a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;writing-a-custom-model&quot;,&quot;url&quot;:&quot;#writing-a-custom-model&quot;},{&quot;title&quot;:&quot;Sending the code to the Hub&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sending-the-code-to-the-hub&quot;,&quot;url&quot;:&quot;#sending-the-code-to-the-hub&quot;},{&quot;title&quot;:&quot;Using a model with custom code&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;using-a-model-with-custom-code&quot;,&quot;url&quot;:&quot;#using-a-model-with-custom-code&quot;},{&quot;title&quot;:&quot;Registering a model with custom code to the auto classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;registering-a-model-with-custom-code-to-the-auto-classes&quot;,&quot;url&quot;:&quot;#registering-a-model-with-custom-code-to-the-auto-classes&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#sharing-custom-models" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-sharing-custom-models"><wbr>Sharing custom models</a> <a href="#writing-a-custom-configuration" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-writing-a-custom-configuration"><wbr>Writing a custom configuration</a> <a href="#writing-a-custom-model" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-writing-a-custom-model"><wbr>Writing a custom model</a> <a href="#sending-the-code-to-the-hub" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-sending-the-code-to-the-hub"><wbr>Sending the code to the <wbr>Hub</a> <a href="#using-a-model-with-custom-code" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-a-model-with-custom-code"><wbr>Using a model with custom code</a> <a href="#registering-a-model-with-custom-code-to-the-auto-classes" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-registering-a-model-with-custom-code-to-the-auto-classes"><wbr>Registering a model with custom code to the auto classes</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/custom_models" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/custom_models"); } </script> <iframe name="__privateStripeMetricsController8400" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fcustom_models&amp;title=Sharing%20custom%20models&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:11.269Z
Run training on Amazon SageMaker
https://huggingface.co/docs/transformers/sagemaker
Transformers documentation Run training on Amazon SageMaker [105,251](https://github.com/huggingface/transformers) Get started [🤗 Transformers](/docs/transformers/index) [Quick tour](/docs/transformers/quicktour) [Installation](/docs/transformers/installation) Tutorials [Run inference with pipelines](/docs/transformers/pipeline_tutorial) [Write portable code with AutoClass](/docs/transformers/autoclass_tutorial) [Preprocess data](/docs/transformers/preprocessing) [Fine-tune a pretrained model](/docs/transformers/training) [Train with a script](/docs/transformers/run_scripts) [Set up distributed training with 🤗 Accelerate](/docs/transformers/accelerate) [Share your model](/docs/transformers/model_sharing) [Agents](/docs/transformers/transformers_agents) Task Guides Natural Language Processing Audio Computer Vision Multimodal Developer guides [Use fast tokenizers from 🤗 Tokenizers](/docs/transformers/fast_tokenizers) [Run inference with multilingual models](/docs/transformers/multilingual) [Customize text generation strategy](/docs/transformers/generation_strategies) [Use model-specific APIs](/docs/transformers/create_a_model) [Share a custom model](/docs/transformers/custom_models) [Run training on Amazon SageMaker](/docs/transformers/sagemaker) [Export to ONNX](/docs/transformers/serialization) [Export to TFLite](/docs/transformers/tflite) [Export to TorchScript](/docs/transformers/torchscript) [Benchmarks](/docs/transformers/benchmarks) [Notebooks with examples](/docs/transformers/notebooks) [Community resources](/docs/transformers/community) [Custom Tools and Prompts](/docs/transformers/custom_tools) [Troubleshoot](/docs/transformers/troubleshooting) Performance and scalability [Overview](/docs/transformers/performance) [Training on one GPU](/docs/transformers/perf_train_gpu_one) [Training on many GPUs](/docs/transformers/perf_train_gpu_many) [Training on CPU](/docs/transformers/perf_train_cpu) [Training on many CPUs](/docs/transformers/perf_train_cpu_many) [Training on TPUs](/docs/transformers/perf_train_tpu) [Training on TPU with TensorFlow](/docs/transformers/perf_train_tpu_tf) [Training on Specialized Hardware](/docs/transformers/perf_train_special) [Inference on CPU](/docs/transformers/perf_infer_cpu) [Inference on one GPU](/docs/transformers/perf_infer_gpu_one) [Inference on many GPUs](/docs/transformers/perf_infer_gpu_many) [Inference on Specialized Hardware](/docs/transformers/perf_infer_special) [Custom hardware for training](/docs/transformers/perf_hardware) [Instantiating a big model](/docs/transformers/big_models) [Debugging](/docs/transformers/debugging) [Hyperparameter Search using Trainer API](/docs/transformers/hpo_train) [XLA Integration for TensorFlow Models](/docs/transformers/tf_xla) Contribute [How to contribute to transformers?](/docs/transformers/contributing) [How to add a model to 🤗 Transformers?](/docs/transformers/add_new_model) [How to convert a 🤗 Transformers model to TensorFlow?](/docs/transformers/add_tensorflow_model) [How to add a pipeline to 🤗 Transformers?](/docs/transformers/add_new_pipeline) [Testing](/docs/transformers/testing) [Checks on a Pull Request](/docs/transformers/pr_checks) Conceptual guides [Philosophy](/docs/transformers/philosophy) [Glossary](/docs/transformers/glossary) [What 🤗 Transformers can do](/docs/transformers/task_summary) [How 🤗 Transformers solve tasks](/docs/transformers/tasks_explained) [The Transformer model family](/docs/transformers/model_summary) [Summary of the tokenizers](/docs/transformers/tokenizer_summary) [Attention mechanisms](/docs/transformers/attention) [Padding and truncation](/docs/transformers/pad_truncation) [BERTology](/docs/transformers/bertology) [Perplexity of fixed-length models](/docs/transformers/perplexity) [Pipelines for webserver inference](/docs/transformers/pipeline_webserver) API Main Classes [Agents and Tools](/docs/transformers/main_classes/agent) [Auto Classes](/docs/transformers/model_doc/auto) [Callbacks](/docs/transformers/main_classes/callback) [Configuration](/docs/transformers/main_classes/configuration) [Data Collator](/docs/transformers/main_classes/data_collator) [Keras callbacks](/docs/transformers/main_classes/keras_callbacks) [Logging](/docs/transformers/main_classes/logging) [Models](/docs/transformers/main_classes/model) [Text Generation](/docs/transformers/main_classes/text_generation) [ONNX](/docs/transformers/main_classes/onnx) [Optimization](/docs/transformers/main_classes/optimizer_schedules) [Model outputs](/docs/transformers/main_classes/output) [Pipelines](/docs/transformers/main_classes/pipelines) [Processors](/docs/transformers/main_classes/processors) [Quantization](/docs/transformers/main_classes/quantization) [Tokenizer](/docs/transformers/main_classes/tokenizer) [Trainer](/docs/transformers/main_classes/trainer) [DeepSpeed Integration](/docs/transformers/main_classes/deepspeed) [Feature Extractor](/docs/transformers/main_classes/feature_extractor) [Image Processor](/docs/transformers/main_classes/image_processor) Models Text models Vision models Audio models Multimodal models Reinforcement learning models Time series models Graph models Internal Helpers [Custom Layers and Utilities](/docs/transformers/internal/modeling_utils) [Utilities for pipelines](/docs/transformers/internal/pipelines_utils) [Utilities for Tokenizers](/docs/transformers/internal/tokenization_utils) [Utilities for Trainer](/docs/transformers/internal/trainer_utils) [Utilities for Generation](/docs/transformers/internal/generation_utils) [Utilities for Image Processors](/docs/transformers/internal/image_processing_utils) [Utilities for Audio processing](/docs/transformers/internal/audio_utils) [General Utilities](/docs/transformers/internal/file_utils) [Utilities for Time Series](/docs/transformers/internal/time_series_utils) ![Hugging Face's logo](/front/assets/huggingface_logo-noborder.svg) 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 to get started The documentation has been moved to [hf.co/docs/sagemaker](https://huggingface.co/docs/sagemaker). This page will be removed in `transformers` 5.0. ### [](#table-of-content)Table of Content - [Train Hugging Face models on Amazon SageMaker with the SageMaker Python SDK](https://huggingface.co/docs/sagemaker/train) - [Deploy Hugging Face models to Amazon SageMaker with the SageMaker Python SDK](https://huggingface.co/docs/sagemaker/inference) - [Frequently Asked Questions](https://huggingface.co/docs/sagemaker/faq) [←Share a custom model](/docs/transformers/custom_models) [Export to ONNX→](/docs/transformers/serialization) [Run training on Amazon SageMaker](#run-training-on-amazon-sagemaker) [Table of Content](#table-of-content)
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Run training on Amazon SageMaker"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/sagemaker"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Run training on Amazon SageMaker</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;run-training-on-amazon-sagemaker&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;table-of-content&quot;,&quot;title&quot;:&quot;Table of Content&quot;}],&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;sagemaker&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Run training on Amazon SageMaker</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="run-training-on-amazon-sagemaker" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#run-training-on-amazon-sagemaker"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Run training on Amazon SageMaker</span></h1> <p>The documentation has been moved to <a href="https://huggingface.co/docs/sagemaker" rel="nofollow">hf.co/docs/sagemaker</a>. This page will be removed in <code>transformers</code> 5.0.</p> <h3 class="relative group"><a id="table-of-content" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#table-of-content"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Table of Content</span></h3> <ul><li><a href="https://huggingface.co/docs/sagemaker/train" rel="nofollow">Train Hugging Face models on Amazon SageMaker with the SageMaker Python SDK</a></li> <li><a href="https://huggingface.co/docs/sagemaker/inference" rel="nofollow">Deploy Hugging Face models to Amazon SageMaker with the SageMaker Python SDK</a></li> <li><a href="https://huggingface.co/docs/sagemaker/faq" rel="nofollow">Frequently Asked Questions</a></li></ul> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/custom_models" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Share a custom model</a> <a href="/docs/transformers/serialization" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Export to ONNX<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run-training-on-amazon-sagemaker&quot;,&quot;url&quot;:&quot;#run-training-on-amazon-sagemaker&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Table of Content&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;table-of-content&quot;,&quot;url&quot;:&quot;#table-of-content&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#run-training-on-amazon-sagemaker" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-run-training-on-amazon-sagemaker"><wbr>Run training on <wbr>Amazon <wbr>Sage<wbr>Maker</a> <a href="#table-of-content" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-table-of-content"><wbr>Table of <wbr>Content</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/sagemaker" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/sagemaker"); } </script> <iframe name="__privateStripeMetricsController4990" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fsagemaker&amp;title=Run%20training%20on%20Amazon%20SageMaker&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:12.402Z
Export to ONNX
https://huggingface.co/docs/transformers/serialization
Deploying 🤗 Transformers models in production environments often requires, or can benefit from exporting the models into a serialized format that can be loaded and executed on specialized runtimes and hardware. 🤗 Optimum is an extension of Transformers that enables exporting models from PyTorch or TensorFlow to serialized formats such as ONNX and TFLite through its `exporters` module. 🤗 Optimum also provides a set of performance optimization tools to train and run models on targeted hardware with maximum efficiency. This guide demonstrates how you can export 🤗 Transformers models to ONNX with 🤗 Optimum, for the guide on exporting models to TFLite, please refer to the [Export to TFLite page](tflite). ## [](#export-to-onnx)Export to ONNX [ONNX (Open Neural Network eXchange)](http://onnx.ai/) is an open standard that defines a common set of operators and a common file format to represent deep learning models in a wide variety of frameworks, including PyTorch and TensorFlow. When a model is exported to the ONNX format, these operators are used to construct a computational graph (often called an _intermediate representation_) which represents the flow of data through the neural network. By exposing a graph with standardized operators and data types, ONNX makes it easy to switch between frameworks. For example, a model trained in PyTorch can be exported to ONNX format and then imported in TensorFlow (and vice versa). Once exported to ONNX format, a model can be: - optimized for inference via techniques such as [graph optimization](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/optimization) and [quantization](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/quantization). - run with ONNX Runtime via [`ORTModelForXXX` classes](https://huggingface.co/docs/optimum/onnxruntime/package_reference/modeling_ort), which follow the same `AutoModel` API as the one you are used to in 🤗 Transformers. - run with [optimized inference pipelines](https://huggingface.co/docs/optimum/main/en/onnxruntime/usage_guides/pipelines), which has the same API as the [pipeline()](/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline) function in 🤗 Transformers. 🤗 Optimum provides support for the ONNX export by leveraging configuration objects. These configuration objects come ready-made for a number of model architectures, and are designed to be easily extendable to other architectures. For the list of ready-made configurations, please refer to [🤗 Optimum documentation](https://huggingface.co/docs/optimum/exporters/onnx/overview). There are two ways to export a 🤗 Transformers model to ONNX, here we show both: - export with 🤗 Optimum via CLI. - export with 🤗 Optimum with `optimum.onnxruntime`. ### [](#exporting-a-transformers-model-to-onnx-with-cli)Exporting a 🤗 Transformers model to ONNX with CLI To export a 🤗 Transformers model to ONNX, first install an extra dependency: ``` pip install optimum[exporters]``` To check out all available arguments, refer to the [🤗 Optimum docs](https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/export_a_model#exporting-a-model-to-onnx-using-the-cli), or view help in command line: ``` optimum-cli export onnx --help``` To export a model’s checkpoint from the 🤗 Hub, for example, `distilbert-base-uncased-distilled-squad`, run the following command: ``` optimum-cli export onnx --model distilbert-base-uncased-distilled-squad distilbert_base_uncased_squad_onnx/``` You should see the logs indicating progress and showing where the resulting `model.onnx` is saved, like this: ``` Validating ONNX model distilbert_base_uncased_squad_onnx/model.onnx... -[✓] ONNX model output names match reference model (start_logits, end_logits) - Validating ONNX Model output "start_logits": -[✓] (2, 16) matches (2, 16) -[✓] all values close (atol: 0.0001) - Validating ONNX Model output "end_logits": -[✓] (2, 16) matches (2, 16) -[✓] all values close (atol: 0.0001) The ONNX export succeeded and the exported model was saved at: distilbert_base_uncased_squad_onnx``` The example above illustrates exporting a checkpoint from 🤗 Hub. When exporting a local model, first make sure that you saved both the model’s weights and tokenizer files in the same directory (`local_path`). When using CLI, pass the `local_path` to the `model` argument instead of the checkpoint name on 🤗 Hub and provide the `--task` argument. You can review the list of supported tasks in the [🤗 Optimum documentation](https://huggingface.co/docs/optimum/exporters/task_manager). If `task` argument is not provided, it will default to the model architecture without any task specific head. ``` optimum-cli export onnx --model local_path --task question-answering distilbert_base_uncased_squad_onnx/``` The resulting `model.onnx` file can then be run on one of the [many accelerators](https://onnx.ai/supported-tools.html#deployModel) that support the ONNX standard. For example, we can load and run the model with [ONNX Runtime](https://onnxruntime.ai/) as follows: ``` >>> from transformers import AutoTokenizer >>> from optimum.onnxruntime import ORTModelForQuestionAnswering >>> tokenizer = AutoTokenizer.from_pretrained("distilbert_base_uncased_squad_onnx") >>> model = ORTModelForQuestionAnswering.from_pretrained("distilbert_base_uncased_squad_onnx") >>> inputs = tokenizer("What am I using?", "Using DistilBERT with ONNX Runtime!", return_tensors="pt") >>> outputs = model(**inputs)``` The process is identical for TensorFlow checkpoints on the Hub. For instance, here’s how you would export a pure TensorFlow checkpoint from the [Keras organization](https://huggingface.co/keras-io): ``` optimum-cli export onnx --model keras-io/transformers-qa distilbert_base_cased_squad_onnx/``` ### [](#exporting-a-transformers-model-to-onnx-with-optimumonnxruntime)Exporting a 🤗 Transformers model to ONNX with `optimum.onnxruntime` Alternative to CLI, you can export a 🤗 Transformers model to ONNX programmatically like so: ``` >>> from optimum.onnxruntime import ORTModelForSequenceClassification >>> from transformers import AutoTokenizer >>> model_checkpoint = "distilbert_base_uncased_squad" >>> save_directory = "onnx/" >>> >>> ort_model = ORTModelForSequenceClassification.from_pretrained(model_checkpoint, export=True) >>> tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) >>> >>> ort_model.save_pretrained(save_directory) >>> tokenizer.save_pretrained(save_directory)``` ### [](#exporting-a-model-for-an-unsupported-architecture)Exporting a model for an unsupported architecture If you wish to contribute by adding support for a model that cannot be currently exported, you should first check if it is supported in [`optimum.exporters.onnx`](https://huggingface.co/docs/optimum/exporters/onnx/overview), and if it is not, [contribute to 🤗 Optimum](https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/contribute) directly. ### [](#exporting-a-model-with-transformersonnx)Exporting a model with `transformers.onnx` `tranformers.onnx` is no longer maintained, please export models with 🤗 Optimum as described above. This section will be removed in the future versions. To export a 🤗 Transformers model to ONNX with `tranformers.onnx`, install extra dependencies: ``` pip install transformers[onnx]``` Use `transformers.onnx` package as a Python module to export a checkpoint using a ready-made configuration: ``` python -m transformers.onnx --model=distilbert-base-uncased onnx/``` This exports an ONNX graph of the checkpoint defined by the `--model` argument. Pass any checkpoint on the 🤗 Hub or one that’s stored locally. The resulting `model.onnx` file can then be run on one of the many accelerators that support the ONNX standard. For example, load and run the model with ONNX Runtime as follows: ``` >>> from transformers import AutoTokenizer >>> from onnxruntime import InferenceSession >>> tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased") >>> session = InferenceSession("onnx/model.onnx") >>> >>> inputs = tokenizer("Using DistilBERT with ONNX Runtime!", return_tensors="np") >>> outputs = session.run(output_names=["last_hidden_state"], input_feed=dict(inputs))``` The required output names (like `["last_hidden_state"]`) can be obtained by taking a look at the ONNX configuration of each model. For example, for DistilBERT we have: ``` >>> from transformers.models.distilbert import DistilBertConfig, DistilBertOnnxConfig >>> config = DistilBertConfig() >>> onnx_config = DistilBertOnnxConfig(config) >>> print(list(onnx_config.outputs.keys())) ["last_hidden_state"]``` The process is identical for TensorFlow checkpoints on the Hub. For example, export a pure TensorFlow checkpoint like so: ``` python -m transformers.onnx --model=keras-io/transformers-qa onnx/``` To export a model that’s stored locally, save the model’s weights and tokenizer files in the same directory (e.g. `local-pt-checkpoint`), then export it to ONNX by pointing the `--model` argument of the `transformers.onnx` package to the desired directory: ``` python -m transformers.onnx --model=local-pt-checkpoint onnx/```
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Export to ONNX"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/serialization"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Export to ONNX</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;export-to-onnx&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;export-to-onnx&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;exporting-a-transformers-model-to-onnx-with-cli&quot;,&quot;title&quot;:&quot;Exporting a 🤗 Transformers model to ONNX with CLI&quot;},{&quot;local&quot;:&quot;exporting-a-transformers-model-to-onnx-with-optimumonnxruntime&quot;,&quot;title&quot;:&quot;Exporting a 🤗 Transformers model to ONNX with `optimum.onnxruntime`&quot;},{&quot;local&quot;:&quot;exporting-a-model-for-an-unsupported-architecture&quot;,&quot;title&quot;:&quot;Exporting a model for an unsupported architecture&quot;},{&quot;local&quot;:&quot;exporting-a-model-with-transformersonnx&quot;,&quot;title&quot;:&quot;Exporting a model with `transformers.onnx`&quot;}],&quot;title&quot;:&quot;Export to ONNX &quot;}],&quot;title&quot;:&quot;Export to ONNX&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;serialization&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Export to ONNX&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Export to ONNX</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="export-to-onnx" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#export-to-onnx"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Export to ONNX</span></h1> <p>Deploying 🤗 Transformers models in production environments often requires, or can benefit from exporting the models into a serialized format that can be loaded and executed on specialized runtimes and hardware.</p> <p>🤗 Optimum is an extension of Transformers that enables exporting models from PyTorch or TensorFlow to serialized formats such as ONNX and TFLite through its <code>exporters</code> module. 🤗 Optimum also provides a set of performance optimization tools to train and run models on targeted hardware with maximum efficiency.</p> <p>This guide demonstrates how you can export 🤗 Transformers models to ONNX with 🤗 Optimum, for the guide on exporting models to TFLite, please refer to the <a href="tflite">Export to TFLite page</a>.</p> <h2 class="relative group"><a id="export-to-onnx" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#export-to-onnx"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Export to ONNX</span></h2> <p><a href="http://onnx.ai" rel="nofollow">ONNX (Open Neural Network eXchange)</a> is an open standard that defines a common set of operators and a common file format to represent deep learning models in a wide variety of frameworks, including PyTorch and TensorFlow. When a model is exported to the ONNX format, these operators are used to construct a computational graph (often called an <em>intermediate representation</em>) which represents the flow of data through the neural network.</p> <p>By exposing a graph with standardized operators and data types, ONNX makes it easy to switch between frameworks. For example, a model trained in PyTorch can be exported to ONNX format and then imported in TensorFlow (and vice versa).</p> <p>Once exported to ONNX format, a model can be:</p> <ul><li>optimized for inference via techniques such as <a href="https://huggingface.co/docs/optimum/onnxruntime/usage_guides/optimization" rel="nofollow">graph optimization</a> and <a href="https://huggingface.co/docs/optimum/onnxruntime/usage_guides/quantization" rel="nofollow">quantization</a>.</li> <li>run with ONNX Runtime via <a href="https://huggingface.co/docs/optimum/onnxruntime/package_reference/modeling_ort" rel="nofollow"><code>ORTModelForXXX</code> classes</a>, which follow the same <code>AutoModel</code> API as the one you are used to in 🤗 Transformers.</li> <li>run with <a href="https://huggingface.co/docs/optimum/main/en/onnxruntime/usage_guides/pipelines" rel="nofollow">optimized inference pipelines</a>, which has the same API as the <a href="/docs/transformers/v4.30.0/en/main_classes/pipelines#transformers.pipeline">pipeline()</a> function in 🤗 Transformers.</li></ul> <p>🤗 Optimum provides support for the ONNX export by leveraging configuration objects. These configuration objects come ready-made for a number of model architectures, and are designed to be easily extendable to other architectures.</p> <p>For the list of ready-made configurations, please refer to <a href="https://huggingface.co/docs/optimum/exporters/onnx/overview" rel="nofollow">🤗 Optimum documentation</a>.</p> <p>There are two ways to export a 🤗 Transformers model to ONNX, here we show both:</p> <ul><li>export with 🤗 Optimum via CLI.</li> <li>export with 🤗 Optimum with <code>optimum.onnxruntime</code>.</li></ul> <h3 class="relative group"><a id="exporting-a-transformers-model-to-onnx-with-cli" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#exporting-a-transformers-model-to-onnx-with-cli"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Exporting a 🤗 Transformers model to ONNX with CLI</span></h3> <p>To export a 🤗 Transformers model to ONNX, first install an extra dependency:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install optimum[exporters]</pre></div> <p>To check out all available arguments, refer to the <a href="https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/export_a_model#exporting-a-model-to-onnx-using-the-cli" rel="nofollow">🤗 Optimum docs</a>, or view help in command line:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>optimum-cli <span class="hljs-built_in">export</span> onnx --<span class="hljs-built_in">help</span></pre></div> <p>To export a model’s checkpoint from the 🤗 Hub, for example, <code>distilbert-base-uncased-distilled-squad</code>, run the following command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>optimum-cli <span class="hljs-built_in">export</span> onnx --model distilbert-base-uncased-distilled-squad distilbert_base_uncased_squad_onnx/</pre></div> <p>You should see the logs indicating progress and showing where the resulting <code>model.onnx</code> is saved, like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Validating ONNX model distilbert_base_uncased_squad_onnx/model.onnx... -[✓] ONNX model output names match reference model (start_logits, end_logits) - Validating ONNX Model output <span class="hljs-string">"start_logits"</span>: -[✓] (2, 16) matches (2, 16) -[✓] all values close (atol: 0.0001) - Validating ONNX Model output <span class="hljs-string">"end_logits"</span>: -[✓] (2, 16) matches (2, 16) -[✓] all values close (atol: 0.0001) The ONNX <span class="hljs-built_in">export</span> succeeded and the exported model was saved at: distilbert_base_uncased_squad_onnx</pre></div> <p>The example above illustrates exporting a checkpoint from 🤗 Hub. When exporting a local model, first make sure that you saved both the model’s weights and tokenizer files in the same directory (<code>local_path</code>). When using CLI, pass the <code>local_path</code> to the <code>model</code> argument instead of the checkpoint name on 🤗 Hub and provide the <code>--task</code> argument. You can review the list of supported tasks in the <a href="https://huggingface.co/docs/optimum/exporters/task_manager" rel="nofollow">🤗 Optimum documentation</a>. If <code>task</code> argument is not provided, it will default to the model architecture without any task specific head.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>optimum-cli <span class="hljs-built_in">export</span> onnx --model local_path --task question-answering distilbert_base_uncased_squad_onnx/</pre></div> <p>The resulting <code>model.onnx</code> file can then be run on one of the <a href="https://onnx.ai/supported-tools.html#deployModel" rel="nofollow">many accelerators</a> that support the ONNX standard. For example, we can load and run the model with <a href="https://onnxruntime.ai/" rel="nofollow">ONNX Runtime</a> as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> optimum.onnxruntime <span class="hljs-keyword">import</span> ORTModelForQuestionAnswering <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"distilbert_base_uncased_squad_onnx"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>model = ORTModelForQuestionAnswering.from_pretrained(<span class="hljs-string">"distilbert_base_uncased_squad_onnx"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(<span class="hljs-string">"What am I using?"</span>, <span class="hljs-string">"Using DistilBERT with ONNX Runtime!"</span>, return_tensors=<span class="hljs-string">"pt"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>outputs = model(**inputs)</pre></div> <p>The process is identical for TensorFlow checkpoints on the Hub. For instance, here’s how you would export a pure TensorFlow checkpoint from the <a href="https://huggingface.co/keras-io" rel="nofollow">Keras organization</a>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>optimum-cli <span class="hljs-built_in">export</span> onnx --model keras-io/transformers-qa distilbert_base_cased_squad_onnx/</pre></div> <h3 class="relative group"><a id="exporting-a-transformers-model-to-onnx-with-optimumonnxruntime" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#exporting-a-transformers-model-to-onnx-with-optimumonnxruntime"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Exporting a 🤗 Transformers model to ONNX with <code>optimum.onnxruntime</code></span></h3> <p>Alternative to CLI, you can export a 🤗 Transformers model to ONNX programmatically like so:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> optimum.onnxruntime <span class="hljs-keyword">import</span> ORTModelForSequenceClassification <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span>model_checkpoint = <span class="hljs-string">"distilbert_base_uncased_squad"</span> <span class="hljs-meta">&gt;&gt;&gt; </span>save_directory = <span class="hljs-string">"onnx/"</span> <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Load a model from transformers and export it to ONNX</span> <span class="hljs-meta">&gt;&gt;&gt; </span>ort_model = ORTModelForSequenceClassification.from_pretrained(model_checkpoint, export=<span class="hljs-literal">True</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Save the onnx model and tokenizer</span> <span class="hljs-meta">&gt;&gt;&gt; </span>ort_model.save_pretrained(save_directory) <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.save_pretrained(save_directory)</pre></div> <h3 class="relative group"><a id="exporting-a-model-for-an-unsupported-architecture" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#exporting-a-model-for-an-unsupported-architecture"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Exporting a model for an unsupported architecture</span></h3> <p>If you wish to contribute by adding support for a model that cannot be currently exported, you should first check if it is supported in <a href="https://huggingface.co/docs/optimum/exporters/onnx/overview" rel="nofollow"><code>optimum.exporters.onnx</code></a>, and if it is not, <a href="https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/contribute" rel="nofollow">contribute to 🤗 Optimum</a> directly.</p> <h3 class="relative group"><a id="exporting-a-model-with-transformersonnx" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#exporting-a-model-with-transformersonnx"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Exporting a model with <code>transformers.onnx</code></span></h3> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p><code>tranformers.onnx</code> is no longer maintained, please export models with 🤗 Optimum as described above. This section will be removed in the future versions.</p></div> <p>To export a 🤗 Transformers model to ONNX with <code>tranformers.onnx</code>, install extra dependencies:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install transformers[onnx]</pre></div> <p>Use <code>transformers.onnx</code> package as a Python module to export a checkpoint using a ready-made configuration:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python -m transformers.onnx --model=distilbert-base-uncased onnx/</pre></div> <p>This exports an ONNX graph of the checkpoint defined by the <code>--model</code> argument. Pass any checkpoint on the 🤗 Hub or one that’s stored locally. The resulting <code>model.onnx</code> file can then be run on one of the many accelerators that support the ONNX standard. For example, load and run the model with ONNX Runtime as follows:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> onnxruntime <span class="hljs-keyword">import</span> InferenceSession <span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"distilbert-base-uncased"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>session = InferenceSession(<span class="hljs-string">"onnx/model.onnx"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># ONNX Runtime expects NumPy arrays as input</span> <span class="hljs-meta">&gt;&gt;&gt; </span>inputs = tokenizer(<span class="hljs-string">"Using DistilBERT with ONNX Runtime!"</span>, return_tensors=<span class="hljs-string">"np"</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>outputs = session.run(output_names=[<span class="hljs-string">"last_hidden_state"</span>], input_feed=<span class="hljs-built_in">dict</span>(inputs))</pre></div> <p>The required output names (like <code>["last_hidden_state"]</code>) can be obtained by taking a look at the ONNX configuration of each model. For example, for DistilBERT we have:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers.models.distilbert <span class="hljs-keyword">import</span> DistilBertConfig, DistilBertOnnxConfig <span class="hljs-meta">&gt;&gt;&gt; </span>config = DistilBertConfig() <span class="hljs-meta">&gt;&gt;&gt; </span>onnx_config = DistilBertOnnxConfig(config) <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(onnx_config.outputs.keys())) [<span class="hljs-string">"last_hidden_state"</span>]</pre></div> <p>The process is identical for TensorFlow checkpoints on the Hub. For example, export a pure TensorFlow checkpoint like so:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python -m transformers.onnx --model=keras-io/transformers-qa onnx/</pre></div> <p>To export a model that’s stored locally, save the model’s weights and tokenizer files in the same directory (e.g. <code>local-pt-checkpoint</code>), then export it to ONNX by pointing the <code>--model</code> argument of the <code>transformers.onnx</code> package to the desired directory:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python -m transformers.onnx --model=local-pt-checkpoint onnx/</pre></div> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/sagemaker" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Run training on Amazon SageMaker</a> <a href="/docs/transformers/tflite" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Export to TFLite<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;export-to-onnx&quot;,&quot;url&quot;:&quot;#export-to-onnx&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Export to ONNX &quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;export-to-onnx&quot;,&quot;url&quot;:&quot;#export-to-onnx&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Exporting a 🤗 Transformers model to ONNX with CLI&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;exporting-a-transformers-model-to-onnx-with-cli&quot;,&quot;url&quot;:&quot;#exporting-a-transformers-model-to-onnx-with-cli&quot;},{&quot;title&quot;:&quot;Exporting a 🤗 Transformers model to ONNX with `optimum.onnxruntime`&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;exporting-a-transformers-model-to-onnx-with-optimumonnxruntime&quot;,&quot;url&quot;:&quot;#exporting-a-transformers-model-to-onnx-with-optimumonnxruntime&quot;},{&quot;title&quot;:&quot;Exporting a model for an unsupported architecture&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;exporting-a-model-for-an-unsupported-architecture&quot;,&quot;url&quot;:&quot;#exporting-a-model-for-an-unsupported-architecture&quot;},{&quot;title&quot;:&quot;Exporting a model with `transformers.onnx`&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;exporting-a-model-with-transformersonnx&quot;,&quot;url&quot;:&quot;#exporting-a-model-with-transformersonnx&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#export-to-onnx" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-export-to-onnx"><wbr>Export to ONNX</a> <a href="#export-to-onnx" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-export-to-onnx"><wbr>Export to ONN<wbr>X </a> <a href="#exporting-a-transformers-model-to-onnx-with-cli" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-exporting-a-transformers-model-to-onnx-with-cli"><wbr>Exporting a 🤗 <wbr>Transformers model to ONN<wbr>X with CLI</a> <a href="#exporting-a-transformers-model-to-onnx-with-optimumonnxruntime" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-exporting-a-transformers-model-to-onnx-with-optimumonnxruntime"><wbr>Exporting a 🤗 <wbr>Transformers model to ONN<wbr>X with `optimum.onnxruntime`</a> <a href="#exporting-a-model-for-an-unsupported-architecture" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-exporting-a-model-for-an-unsupported-architecture"><wbr>Exporting a model for an unsupported architecture</a> <a href="#exporting-a-model-with-transformersonnx" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-exporting-a-model-with-transformersonnx"><wbr>Exporting a model with `transformers.onnx`</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/serialization" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/serialization"); } </script> <iframe name="__privateStripeMetricsController0200" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fserialization&amp;title=Export%20to%20ONNX&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:12.492Z
Export to TFLite
https://huggingface.co/docs/transformers/tflite
[TensorFlow Lite](https://www.tensorflow.org/lite/guide) is a lightweight framework for deploying machine learning models on resource-constrained devices, such as mobile phones, embedded systems, and Internet of Things (IoT) devices. TFLite is designed to optimize and run models efficiently on these devices with limited computational power, memory, and power consumption. A TensorFlow Lite model is represented in a special efficient portable format identified by the `.tflite` file extension. 🤗 Optimum offers functionality to export 🤗 Transformers models to TFLite through the `exporters.tflite` module. For the list of supported model architectures, please refer to [🤗 Optimum documentation](https://huggingface.co/docs/optimum/exporters/tflite/overview). To export a model to TFLite, install the required dependencies: ``` pip install optimum[exporters-tf]``` To check out all available arguments, refer to the [🤗 Optimum docs](https://huggingface.co/docs/optimum/main/en/exporters/tflite/usage_guides/export_a_model), or view help in command line: ``` optimum-cli export tflite --help``` To export a model’s checkpoint from the 🤗 Hub, for example, `bert-base-uncased`, run the following command: ``` optimum-cli export tflite --model bert-base-uncased --sequence_length 128 bert_tflite/``` You should see the logs indicating progress and showing where the resulting `model.tflite` is saved, like this: ``` Validating TFLite model... -[✓] TFLite model output names match reference model (logits) - Validating TFLite Model output "logits": -[✓] (1, 128, 30522) matches (1, 128, 30522) -[x] values not close enough, max diff: 5.817413330078125e-05 (atol: 1e-05) The TensorFlow Lite export succeeded with the warning: The maximum absolute difference between the output of the reference model and the TFLite exported model is not within the set tolerance 1e-05: - logits: max diff = 5.817413330078125e-05. The exported model was saved at: bert_tflite``` The example above illustrates exporting a checkpoint from 🤗 Hub. When exporting a local model, first make sure that you saved both the model’s weights and tokenizer files in the same directory (`local_path`). When using CLI, pass the `local_path` to the `model` argument instead of the checkpoint name on 🤗 Hub.
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Export to TFLite"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/tflite"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Export to TFLite</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;export-to-tflite&quot;,&quot;title&quot;:&quot;Export to TFLite&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;tflite&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Export to TFLite&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Export to TFLite</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="export-to-tflite" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#export-to-tflite"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Export to TFLite</span></h1> <p><a href="https://www.tensorflow.org/lite/guide" rel="nofollow">TensorFlow Lite</a> is a lightweight framework for deploying machine learning models on resource-constrained devices, such as mobile phones, embedded systems, and Internet of Things (IoT) devices. TFLite is designed to optimize and run models efficiently on these devices with limited computational power, memory, and power consumption. A TensorFlow Lite model is represented in a special efficient portable format identified by the <code>.tflite</code> file extension.</p> <p>🤗 Optimum offers functionality to export 🤗 Transformers models to TFLite through the <code>exporters.tflite</code> module. For the list of supported model architectures, please refer to <a href="https://huggingface.co/docs/optimum/exporters/tflite/overview" rel="nofollow">🤗 Optimum documentation</a>.</p> <p>To export a model to TFLite, install the required dependencies:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>pip install optimum[exporters-tf]</pre></div> <p>To check out all available arguments, refer to the <a href="https://huggingface.co/docs/optimum/main/en/exporters/tflite/usage_guides/export_a_model" rel="nofollow">🤗 Optimum docs</a>, or view help in command line:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>optimum-cli <span class="hljs-built_in">export</span> tflite --<span class="hljs-built_in">help</span></pre></div> <p>To export a model’s checkpoint from the 🤗 Hub, for example, <code>bert-base-uncased</code>, run the following command:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>optimum-cli <span class="hljs-built_in">export</span> tflite --model bert-base-uncased --sequence_length 128 bert_tflite/</pre></div> <p>You should see the logs indicating progress and showing where the resulting <code>model.tflite</code> is saved, like this:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>Validating TFLite model... -[✓] TFLite model output names match reference model (logits) - Validating TFLite Model output <span class="hljs-string">"logits"</span>: -[✓] (1, 128, 30522) matches (1, 128, 30522) -[x] values not close enough, max diff: 5.817413330078125e-05 (atol: 1e-05) The TensorFlow Lite <span class="hljs-built_in">export</span> succeeded with the warning: The maximum absolute difference between the output of the reference model and the TFLite exported model is not within the <span class="hljs-built_in">set</span> tolerance 1e-05: - logits: max diff = 5.817413330078125e-05. The exported model was saved at: bert_tflite</pre></div> <p>The example above illustrates exporting a checkpoint from 🤗 Hub. When exporting a local model, first make sure that you saved both the model’s weights and tokenizer files in the same directory (<code>local_path</code>). When using CLI, pass the <code>local_path</code> to the <code>model</code> argument instead of the checkpoint name on 🤗 Hub.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/serialization" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Export to ONNX</a> <a href="/docs/transformers/torchscript" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Export to TorchScript<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;export-to-tflite&quot;,&quot;url&quot;:&quot;#export-to-tflite&quot;}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#export-to-tflite" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-export-to-tflite"><wbr>Export to TF<wbr>Lite</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/tflite" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/tflite"); } </script> <iframe name="__privateStripeMetricsController8350" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Ftflite&amp;title=Export%20to%20TFLite&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:12.602Z
Export to TorchScript
https://huggingface.co/docs/transformers/torchscript
This is the very beginning of our experiments with TorchScript and we are still exploring its capabilities with variable-input-size models. It is a focus of interest to us and we will deepen our analysis in upcoming releases, with more code examples, a more flexible implementation, and benchmarks comparing Python-based codes with compiled TorchScript. According to the [TorchScript documentation](https://pytorch.org/docs/stable/jit.html): > TorchScript is a way to create serializable and optimizable models from PyTorch code. There are two PyTorch modules, [JIT and TRACE](https://pytorch.org/docs/stable/jit.html), that allow developers to export their models to be reused in other programs like efficiency-oriented C++ programs. We provide an interface that allows you to export 🤗 Transformers models to TorchScript so they can be reused in a different environment than PyTorch-based Python programs. Here, we explain how to export and use our models using TorchScript. Exporting a model requires two things: - model instantiation with the `torchscript` flag - a forward pass with dummy inputs These necessities imply several things developers should be careful about as detailed below. ## [](#torchscript-flag-and-tied-weights)TorchScript flag and tied weights The `torchscript` flag is necessary because most of the 🤗 Transformers language models have tied weights between their `Embedding` layer and their `Decoding` layer. TorchScript does not allow you to export models that have tied weights, so it is necessary to untie and clone the weights beforehand. Models instantiated with the `torchscript` flag have their `Embedding` layer and `Decoding` layer separated, which means that they should not be trained down the line. Training would desynchronize the two layers, leading to unexpected results. This is not the case for models that do not have a language model head, as those do not have tied weights. These models can be safely exported without the `torchscript` flag. ## [](#dummy-inputs-and-standard-lengths)Dummy inputs and standard lengths The dummy inputs are used for a models forward pass. While the inputs’ values are propagated through the layers, PyTorch keeps track of the different operations executed on each tensor. These recorded operations are then used to create the _trace_ of the model. The trace is created relative to the inputs’ dimensions. It is therefore constrained by the dimensions of the dummy input, and will not work for any other sequence length or batch size. When trying with a different size, the following error is raised: ``` `The expanded size of the tensor (3) must match the existing size (7) at non-singleton dimension 2```` We recommended you trace the model with a dummy input size at least as large as the largest input that will be fed to the model during inference. Padding can help fill the missing values. However, since the model is traced with a larger input size, the dimensions of the matrix will also be large, resulting in more calculations. Be careful of the total number of operations done on each input and follow the performance closely when exporting varying sequence-length models. ## [](#using-torchscript-in-python)Using TorchScript in Python This section demonstrates how to save and load models as well as how to use the trace for inference. ### [](#saving-a-model)Saving a model To export a `BertModel` with TorchScript, instantiate `BertModel` from the `BertConfig` class and then save it to disk under the filename `traced_bert.pt`: ``` from transformers import BertModel, BertTokenizer, BertConfig import torch enc = BertTokenizer.from_pretrained("bert-base-uncased") text = "[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]" tokenized_text = enc.tokenize(text) masked_index = 8 tokenized_text[masked_index] = "[MASK]" indexed_tokens = enc.convert_tokens_to_ids(tokenized_text) segments_ids = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1] tokens_tensor = torch.tensor([indexed_tokens]) segments_tensors = torch.tensor([segments_ids]) dummy_input = [tokens_tensor, segments_tensors] config = BertConfig( vocab_size_or_config_json_file=32000, hidden_size=768, num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072, torchscript=True, ) model = BertModel(config) model.eval() model = BertModel.from_pretrained("bert-base-uncased", torchscript=True) traced_model = torch.jit.trace(model, [tokens_tensor, segments_tensors]) torch.jit.save(traced_model, "traced_bert.pt")``` ### [](#loading-a-model)Loading a model Now you can load the previously saved `BertModel`, `traced_bert.pt`, from disk and use it on the previously initialised `dummy_input`: ``` loaded_model = torch.jit.load("traced_bert.pt") loaded_model.eval() all_encoder_layers, pooled_output = loaded_model(*dummy_input)``` ### [](#using-a-traced-model-for-inference)Using a traced model for inference Use the traced model for inference by using its `__call__` dunder method: ``` traced_model(tokens_tensor, segments_tensors)``` ## [](#deploy-hugging-face-torchscript-models-to-aws-with-the-neuron-sdk)Deploy Hugging Face TorchScript models to AWS with the Neuron SDK AWS introduced the [Amazon EC2 Inf1](https://aws.amazon.com/ec2/instance-types/inf1/) instance family for low cost, high performance machine learning inference in the cloud. The Inf1 instances are powered by the AWS Inferentia chip, a custom-built hardware accelerator, specializing in deep learning inferencing workloads. [AWS Neuron](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/#) is the SDK for Inferentia that supports tracing and optimizing transformers models for deployment on Inf1. The Neuron SDK provides: 1. Easy-to-use API with one line of code change to trace and optimize a TorchScript model for inference in the cloud. 2. Out of the box performance optimizations for [improved cost-performance](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/benchmark/%3E). 3. Support for Hugging Face transformers models built with either [PyTorch](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/pytorch/bert_tutorial/tutorial_pretrained_bert.html) or [TensorFlow](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/tensorflow/huggingface_bert/huggingface_bert.html). ### [](#implications)Implications Transformers models based on the [BERT (Bidirectional Encoder Representations from Transformers)](https://huggingface.co/docs/transformers/main/model_doc/bert) architecture, or its variants such as [distilBERT](https://huggingface.co/docs/transformers/main/model_doc/distilbert) and [roBERTa](https://huggingface.co/docs/transformers/main/model_doc/roberta) run best on Inf1 for non-generative tasks such as extractive question answering, sequence classification, and token classification. However, text generation tasks can still be adapted to run on Inf1 according to this [AWS Neuron MarianMT tutorial](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/pytorch/transformers-marianmt.html). More information about models that can be converted out of the box on Inferentia can be found in the [Model Architecture Fit](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/models/models-inferentia.html#models-inferentia) section of the Neuron documentation. ### [](#dependencies)Dependencies Using AWS Neuron to convert models requires a [Neuron SDK environment](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/neuron-frameworks/pytorch-neuron/index.html#installation-guide) which comes preconfigured on [AWS Deep Learning AMI](https://docs.aws.amazon.com/dlami/latest/devguide/tutorial-inferentia-launching.html). ### [](#converting-a-model-for-aws-neuron)Converting a model for AWS Neuron Convert a model for AWS NEURON using the same code from [Using TorchScript in Python](torchscript#using-torchscript-in-python) to trace a `BertModel`. Import the `torch.neuron` framework extension to access the components of the Neuron SDK through a Python API: ``` from transformers import BertModel, BertTokenizer, BertConfig import torch import torch.neuron``` You only need to modify the following line: ``` - torch.jit.trace(model, [tokens_tensor, segments_tensors]) + torch.neuron.trace(model, [token_tensor, segments_tensors])``` This enables the Neuron SDK to trace the model and optimize it for Inf1 instances. To learn more about AWS Neuron SDK features, tools, example tutorials and latest updates, please see the [AWS NeuronSDK documentation](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/index.html).
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Export to TorchScript"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/torchscript"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Export to TorchScript</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;export-to-torchscript&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;torchscript-flag-and-tied-weights&quot;,&quot;title&quot;:&quot;TorchScript flag and tied weights&quot;},{&quot;local&quot;:&quot;dummy-inputs-and-standard-lengths&quot;,&quot;title&quot;:&quot;Dummy inputs and standard lengths&quot;},{&quot;local&quot;:&quot;using-torchscript-in-python&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;saving-a-model&quot;,&quot;title&quot;:&quot;Saving a model&quot;},{&quot;local&quot;:&quot;loading-a-model&quot;,&quot;title&quot;:&quot;Loading a model&quot;},{&quot;local&quot;:&quot;using-a-traced-model-for-inference&quot;,&quot;title&quot;:&quot;Using a traced model for inference&quot;}],&quot;title&quot;:&quot;Using TorchScript in Python&quot;},{&quot;local&quot;:&quot;deploy-hugging-face-torchscript-models-to-aws-with-the-neuron-sdk&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;implications&quot;,&quot;title&quot;:&quot;Implications&quot;},{&quot;local&quot;:&quot;dependencies&quot;,&quot;title&quot;:&quot;Dependencies&quot;},{&quot;local&quot;:&quot;converting-a-model-for-aws-neuron&quot;,&quot;title&quot;:&quot;Converting a model for AWS Neuron&quot;}],&quot;title&quot;:&quot;Deploy Hugging Face TorchScript models to AWS with the Neuron SDK&quot;}],&quot;title&quot;:&quot;Export to TorchScript&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;torchscript&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Export to TorchScript&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Export to TorchScript</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="export-to-torchscript" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#export-to-torchscript"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Export to TorchScript</span></h1> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>This is the very beginning of our experiments with TorchScript and we are still exploring its capabilities with variable-input-size models. It is a focus of interest to us and we will deepen our analysis in upcoming releases, with more code examples, a more flexible implementation, and benchmarks comparing Python-based codes with compiled TorchScript.</p></div> <p>According to the <a href="https://pytorch.org/docs/stable/jit.html" rel="nofollow">TorchScript documentation</a>:</p> <blockquote><p>TorchScript is a way to create serializable and optimizable models from PyTorch code.</p></blockquote> <p>There are two PyTorch modules, <a href="https://pytorch.org/docs/stable/jit.html" rel="nofollow">JIT and TRACE</a>, that allow developers to export their models to be reused in other programs like efficiency-oriented C++ programs.</p> <p>We provide an interface that allows you to export 🤗 Transformers models to TorchScript so they can be reused in a different environment than PyTorch-based Python programs. Here, we explain how to export and use our models using TorchScript.</p> <p>Exporting a model requires two things:</p> <ul><li>model instantiation with the <code>torchscript</code> flag</li> <li>a forward pass with dummy inputs</li></ul> <p>These necessities imply several things developers should be careful about as detailed below.</p> <h2 class="relative group"><a id="torchscript-flag-and-tied-weights" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#torchscript-flag-and-tied-weights"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>TorchScript flag and tied weights</span></h2> <p>The <code>torchscript</code> flag is necessary because most of the 🤗 Transformers language models have tied weights between their <code>Embedding</code> layer and their <code>Decoding</code> layer. TorchScript does not allow you to export models that have tied weights, so it is necessary to untie and clone the weights beforehand.</p> <p>Models instantiated with the <code>torchscript</code> flag have their <code>Embedding</code> layer and <code>Decoding</code> layer separated, which means that they should not be trained down the line. Training would desynchronize the two layers, leading to unexpected results.</p> <p>This is not the case for models that do not have a language model head, as those do not have tied weights. These models can be safely exported without the <code>torchscript</code> flag.</p> <h2 class="relative group"><a id="dummy-inputs-and-standard-lengths" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#dummy-inputs-and-standard-lengths"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Dummy inputs and standard lengths</span></h2> <p>The dummy inputs are used for a models forward pass. While the inputs’ values are propagated through the layers, PyTorch keeps track of the different operations executed on each tensor. These recorded operations are then used to create the <em>trace</em> of the model.</p> <p>The trace is created relative to the inputs’ dimensions. It is therefore constrained by the dimensions of the dummy input, and will not work for any other sequence length or batch size. When trying with a different size, the following error is raised:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>`The expanded <span class="hljs-built_in">size</span> of the tensor (<span class="hljs-number">3</span>) must match the existing <span class="hljs-built_in">size</span> (<span class="hljs-number">7</span>) at non-singleton <span class="hljs-keyword">dimension</span> <span class="hljs-number">2</span>`</pre></div> <p>We recommended you trace the model with a dummy input size at least as large as the largest input that will be fed to the model during inference. Padding can help fill the missing values. However, since the model is traced with a larger input size, the dimensions of the matrix will also be large, resulting in more calculations.</p> <p>Be careful of the total number of operations done on each input and follow the performance closely when exporting varying sequence-length models.</p> <h2 class="relative group"><a id="using-torchscript-in-python" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-torchscript-in-python"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using TorchScript in Python</span></h2> <p>This section demonstrates how to save and load models as well as how to use the trace for inference.</p> <h3 class="relative group"><a id="saving-a-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#saving-a-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Saving a model</span></h3> <p>To export a <code>BertModel</code> with TorchScript, instantiate <code>BertModel</code> from the <code>BertConfig</code> class and then save it to disk under the filename <code>traced_bert.pt</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> BertModel, BertTokenizer, BertConfig <span class="hljs-keyword">import</span> torch enc = BertTokenizer.from_pretrained(<span class="hljs-string">"bert-base-uncased"</span>) <span class="hljs-comment"># Tokenizing input text</span> text = <span class="hljs-string">"[CLS] Who was Jim Henson ? [SEP] Jim Henson was a puppeteer [SEP]"</span> tokenized_text = enc.tokenize(text) <span class="hljs-comment"># Masking one of the input tokens</span> masked_index = <span class="hljs-number">8</span> tokenized_text[masked_index] = <span class="hljs-string">"[MASK]"</span> indexed_tokens = enc.convert_tokens_to_ids(tokenized_text) segments_ids = [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>] <span class="hljs-comment"># Creating a dummy input</span> tokens_tensor = torch.tensor([indexed_tokens]) segments_tensors = torch.tensor([segments_ids]) dummy_input = [tokens_tensor, segments_tensors] <span class="hljs-comment"># Initializing the model with the torchscript flag</span> <span class="hljs-comment"># Flag set to True even though it is not necessary as this model does not have an LM Head.</span> config = BertConfig( vocab_size_or_config_json_file=<span class="hljs-number">32000</span>, hidden_size=<span class="hljs-number">768</span>, num_hidden_layers=<span class="hljs-number">12</span>, num_attention_heads=<span class="hljs-number">12</span>, intermediate_size=<span class="hljs-number">3072</span>, torchscript=<span class="hljs-literal">True</span>, ) <span class="hljs-comment"># Instantiating the model</span> model = BertModel(config) <span class="hljs-comment"># The model needs to be in evaluation mode</span> model.<span class="hljs-built_in">eval</span>() <span class="hljs-comment"># If you are instantiating the model with *from_pretrained* you can also easily set the TorchScript flag</span> model = BertModel.from_pretrained(<span class="hljs-string">"bert-base-uncased"</span>, torchscript=<span class="hljs-literal">True</span>) <span class="hljs-comment"># Creating the trace</span> traced_model = torch.jit.trace(model, [tokens_tensor, segments_tensors]) torch.jit.save(traced_model, <span class="hljs-string">"traced_bert.pt"</span>)</pre></div> <h3 class="relative group"><a id="loading-a-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-a-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Loading a model</span></h3> <p>Now you can load the previously saved <code>BertModel</code>, <code>traced_bert.pt</code>, from disk and use it on the previously initialised <code>dummy_input</code>:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>loaded_model = torch.jit.load(<span class="hljs-string">"traced_bert.pt"</span>) loaded_model.<span class="hljs-built_in">eval</span>() all_encoder_layers, pooled_output = loaded_model(*dummy_input)</pre></div> <h3 class="relative group"><a id="using-a-traced-model-for-inference" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#using-a-traced-model-for-inference"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Using a traced model for inference</span></h3> <p>Use the traced model for inference by using its <code>__call__</code> dunder method:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>traced_model(tokens_tensor, segments_tensors)</pre></div> <h2 class="relative group"><a id="deploy-hugging-face-torchscript-models-to-aws-with-the-neuron-sdk" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#deploy-hugging-face-torchscript-models-to-aws-with-the-neuron-sdk"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Deploy Hugging Face TorchScript models to AWS with the Neuron SDK</span></h2> <p>AWS introduced the <a href="https://aws.amazon.com/ec2/instance-types/inf1/" rel="nofollow">Amazon EC2 Inf1</a> instance family for low cost, high performance machine learning inference in the cloud. The Inf1 instances are powered by the AWS Inferentia chip, a custom-built hardware accelerator, specializing in deep learning inferencing workloads. <a href="https://awsdocs-neuron.readthedocs-hosted.com/en/latest/#" rel="nofollow">AWS Neuron</a> is the SDK for Inferentia that supports tracing and optimizing transformers models for deployment on Inf1. The Neuron SDK provides:</p> <ol><li>Easy-to-use API with one line of code change to trace and optimize a TorchScript model for inference in the cloud.</li> <li>Out of the box performance optimizations for <a href="https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/benchmark/%3E" rel="nofollow">improved cost-performance</a>.</li> <li>Support for Hugging Face transformers models built with either <a href="https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/pytorch/bert_tutorial/tutorial_pretrained_bert.html" rel="nofollow">PyTorch</a> or <a href="https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/tensorflow/huggingface_bert/huggingface_bert.html" rel="nofollow">TensorFlow</a>.</li></ol> <h3 class="relative group"><a id="implications" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#implications"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Implications</span></h3> <p>Transformers models based on the <a href="https://huggingface.co/docs/transformers/main/model_doc/bert" rel="nofollow">BERT (Bidirectional Encoder Representations from Transformers)</a> architecture, or its variants such as <a href="https://huggingface.co/docs/transformers/main/model_doc/distilbert" rel="nofollow">distilBERT</a> and <a href="https://huggingface.co/docs/transformers/main/model_doc/roberta" rel="nofollow">roBERTa</a> run best on Inf1 for non-generative tasks such as extractive question answering, sequence classification, and token classification. However, text generation tasks can still be adapted to run on Inf1 according to this <a href="https://awsdocs-neuron.readthedocs-hosted.com/en/latest/src/examples/pytorch/transformers-marianmt.html" rel="nofollow">AWS Neuron MarianMT tutorial</a>. More information about models that can be converted out of the box on Inferentia can be found in the <a href="https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/models/models-inferentia.html#models-inferentia" rel="nofollow">Model Architecture Fit</a> section of the Neuron documentation.</p> <h3 class="relative group"><a id="dependencies" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#dependencies"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Dependencies</span></h3> <p>Using AWS Neuron to convert models requires a <a href="https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/neuron-frameworks/pytorch-neuron/index.html#installation-guide" rel="nofollow">Neuron SDK environment</a> which comes preconfigured on <a href="https://docs.aws.amazon.com/dlami/latest/devguide/tutorial-inferentia-launching.html" rel="nofollow">AWS Deep Learning AMI</a>.</p> <h3 class="relative group"><a id="converting-a-model-for-aws-neuron" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#converting-a-model-for-aws-neuron"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Converting a model for AWS Neuron</span></h3> <p>Convert a model for AWS NEURON using the same code from <a href="torchscript#using-torchscript-in-python">Using TorchScript in Python</a> to trace a <code>BertModel</code>. Import the <code>torch.neuron</code> framework extension to access the components of the Neuron SDK through a Python API:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> BertModel, BertTokenizer, BertConfig <span class="hljs-keyword">import</span> torch <span class="hljs-keyword">import</span> torch.neuron</pre></div> <p>You only need to modify the following line:</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-deletion">- torch.jit.trace(model, [tokens_tensor, segments_tensors])</span> <span class="hljs-addition">+ torch.neuron.trace(model, [token_tensor, segments_tensors])</span></pre></div> <p>This enables the Neuron SDK to trace the model and optimize it for Inf1 instances.</p> <p>To learn more about AWS Neuron SDK features, tools, example tutorials and latest updates, please see the <a href="https://awsdocs-neuron.readthedocs-hosted.com/en/latest/index.html" rel="nofollow">AWS NeuronSDK documentation</a>.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/tflite" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Export to TFLite</a> <a href="/docs/transformers/benchmarks" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Benchmarks<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;export-to-torchscript&quot;,&quot;url&quot;:&quot;#export-to-torchscript&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;TorchScript flag and tied weights&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript-flag-and-tied-weights&quot;,&quot;url&quot;:&quot;#torchscript-flag-and-tied-weights&quot;},{&quot;title&quot;:&quot;Dummy inputs and standard lengths&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;dummy-inputs-and-standard-lengths&quot;,&quot;url&quot;:&quot;#dummy-inputs-and-standard-lengths&quot;},{&quot;title&quot;:&quot;Using TorchScript in Python&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;using-torchscript-in-python&quot;,&quot;url&quot;:&quot;#using-torchscript-in-python&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Saving a model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;saving-a-model&quot;,&quot;url&quot;:&quot;#saving-a-model&quot;},{&quot;title&quot;:&quot;Loading a model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;loading-a-model&quot;,&quot;url&quot;:&quot;#loading-a-model&quot;},{&quot;title&quot;:&quot;Using a traced model for inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;using-a-traced-model-for-inference&quot;,&quot;url&quot;:&quot;#using-a-traced-model-for-inference&quot;}]},{&quot;title&quot;:&quot;Deploy Hugging Face TorchScript models to AWS with the Neuron SDK&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;deploy-hugging-face-torchscript-models-to-aws-with-the-neuron-sdk&quot;,&quot;url&quot;:&quot;#deploy-hugging-face-torchscript-models-to-aws-with-the-neuron-sdk&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Implications&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;implications&quot;,&quot;url&quot;:&quot;#implications&quot;},{&quot;title&quot;:&quot;Dependencies&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;dependencies&quot;,&quot;url&quot;:&quot;#dependencies&quot;},{&quot;title&quot;:&quot;Converting a model for AWS Neuron&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;converting-a-model-for-aws-neuron&quot;,&quot;url&quot;:&quot;#converting-a-model-for-aws-neuron&quot;}]}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#export-to-torchscript" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-export-to-torchscript"><wbr>Export to <wbr>Torch<wbr>Script</a> <a href="#torchscript-flag-and-tied-weights" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-torchscript-flag-and-tied-weights"><wbr>Torch<wbr>Script flag and tied weights</a> <a href="#dummy-inputs-and-standard-lengths" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-dummy-inputs-and-standard-lengths"><wbr>Dummy inputs and standard lengths</a> <a href="#using-torchscript-in-python" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-torchscript-in-python"><wbr>Using <wbr>Torch<wbr>Script in <wbr>Python</a> <a href="#saving-a-model" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-saving-a-model"><wbr>Saving a model</a> <a href="#loading-a-model" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-loading-a-model"><wbr>Loading a model</a> <a href="#using-a-traced-model-for-inference" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-using-a-traced-model-for-inference"><wbr>Using a traced model for inference</a> <a href="#deploy-hugging-face-torchscript-models-to-aws-with-the-neuron-sdk" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-deploy-hugging-face-torchscript-models-to-aws-with-the-neuron-sdk"><wbr>Deploy <wbr>Hugging <wbr>Face <wbr>Torch<wbr>Script models to AW<wbr>S with the <wbr>Neuron SDK</a> <a href="#implications" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-implications"><wbr>Implications</a> <a href="#dependencies" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-dependencies"><wbr>Dependencies</a> <a href="#converting-a-model-for-aws-neuron" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-converting-a-model-for-aws-neuron"><wbr>Converting a model for AW<wbr>S <wbr>Neuron</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/torchscript" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/torchscript"); } </script> <iframe name="__privateStripeMetricsController7270" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Ftorchscript&amp;title=Export%20to%20TorchScript&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:13.162Z
🤗 Transformers Notebooks
https://huggingface.co/docs/transformers/notebooks
## [](#transformers-notebooks)🤗 Transformers Notebooks You can find here a list of the official notebooks provided by Hugging Face. Also, we would like to list here interesting content created by the community. If you wrote some notebook(s) leveraging 🤗 Transformers and would like be listed here, please open a Pull Request so it can be included under the Community notebooks. ## [](#hugging-faces-notebooks)Hugging Face's notebooks 🤗 ### [](#documentation-notebooks)Documentation notebooks You can open any page of the documentation as a notebook in Colab (there is a button directly on said pages) but they are also listed here if you need them: | Notebook | Description | | | | --- | --- | --- | --- | | [Quicktour of the library](https://github.com/huggingface/notebooks/blob/main/transformers_doc/en/quicktour.ipynb) | A presentation of the various APIs in Transformers | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/quicktour.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/en/transformers_doc/quicktour.ipynb) | | [Summary of the tasks](https://github.com/huggingface/notebooks/blob/main/transformers_doc/en/task_summary.ipynb) | How to run the models of the Transformers library task by task | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/task_summary.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/task_summary.ipynb) | | [Preprocessing data](https://github.com/huggingface/notebooks/blob/main/transformers_doc/en/preprocessing.ipynb) | How to use a tokenizer to preprocess your data | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/preprocessing.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/preprocessing.ipynb) | | [Fine-tuning a pretrained model](https://github.com/huggingface/notebooks/blob/main/transformers_doc/en/training.ipynb) | How to use the Trainer to fine-tune a pretrained model | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/training.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/training.ipynb) | | [Summary of the tokenizers](https://github.com/huggingface/notebooks/blob/main/transformers_doc/en/tokenizer_summary.ipynb) | The differences between the tokenizers algorithm | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/tokenizer_summary.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/tokenizer_summary.ipynb) | | [Multilingual models](https://github.com/huggingface/notebooks/blob/main/transformers_doc/en/multilingual.ipynb) | How to use the multilingual models of the library | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/multilingual.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/multilingual.ipynb) | ### [](#pytorch-examples)PyTorch Examples #### [](#pytorch-nlp)Natural Language Processing | Notebook | Description | | | | --- | --- | --- | --- | | [Train your tokenizer](https://github.com/huggingface/notebooks/blob/main/examples/tokenizer_training.ipynb) | How to train and use your very own tokenizer | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/tokenizer_training.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/tokenizer_training.ipynb) | | [Train your language model](https://github.com/huggingface/notebooks/blob/main/examples/language_modeling_from_scratch.ipynb) | How to easily start using transformers | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling_from_scratch.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/language_modeling_from_scratch.ipynb) | | [How to fine-tune a model on text classification](https://github.com/huggingface/notebooks/blob/main/examples/text_classification.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on any GLUE task. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb) | | [How to fine-tune a model on language modeling](https://github.com/huggingface/notebooks/blob/main/examples/language_modeling.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on a causal or masked LM task. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb) | | [How to fine-tune a model on token classification](https://github.com/huggingface/notebooks/blob/main/examples/token_classification.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on a token classification task (NER, PoS). | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/token_classification.ipynb) | | [How to fine-tune a model on question answering](https://github.com/huggingface/notebooks/blob/main/examples/question_answering.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on SQUAD. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb) | | [How to fine-tune a model on multiple choice](https://github.com/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on SWAG. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb) | | [How to fine-tune a model on translation](https://github.com/huggingface/notebooks/blob/main/examples/translation.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on WMT. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/translation.ipynb) | | [How to fine-tune a model on summarization](https://github.com/huggingface/notebooks/blob/main/examples/summarization.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on XSUM. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/summarization.ipynb) | | [How to train a language model from scratch](https://github.com/huggingface/blog/blob/main/notebooks/01_how_to_train.ipynb) | Highlight all the steps to effectively train Transformer model on custom data | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/blog/blob/main/notebooks/01_how_to_train.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/blog/blob/main/notebooks/01_how_to_train.ipynb) | | [How to generate text](https://github.com/huggingface/blog/blob/main/notebooks/02_how_to_generate.ipynb) | How to use different decoding methods for language generation with transformers | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/blog/blob/main/notebooks/02_how_to_generate.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/blog/blob/main/notebooks/02_how_to_generate.ipynb) | | [How to generate text (with constraints)](https://github.com/huggingface/blog/blob/main/notebooks/53_constrained_beam_search.ipynb) | How to guide language generation with user-provided constraints | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/blog/blob/main/notebooks/53_constrained_beam_search.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/blog/blob/main/notebooks/53_constrained_beam_search.ipynb) | | [Reformer](https://github.com/huggingface/blog/blob/main/notebooks/03_reformer.ipynb) | How Reformer pushes the limits of language modeling | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/patrickvonplaten/blog/blob/main/notebooks/03_reformer.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/patrickvonplaten/blog/blob/main/notebooks/03_reformer.ipynb) | #### [](#pytorch-cv)Computer Vision | Notebook | Description | | | | --- | --- | --- | --- | | [How to fine-tune a model on image classification (Torchvision)](https://github.com/huggingface/notebooks/blob/main/examples/image_classification.ipynb) | Show how to preprocess the data using Torchvision and fine-tune any pretrained Vision model on Image Classification | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb) | | [How to fine-tune a model on image classification (Albumentations)](https://github.com/huggingface/notebooks/blob/main/examples/image_classification_albumentations.ipynb) | Show how to preprocess the data using Albumentations and fine-tune any pretrained Vision model on Image Classification | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_albumentations.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/image_classification_albumentations.ipynb) | | [How to fine-tune a model on image classification (Kornia)](https://github.com/huggingface/notebooks/blob/main/examples/image_classification_kornia.ipynb) | Show how to preprocess the data using Kornia and fine-tune any pretrained Vision model on Image Classification | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_kornia.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/image_classification_kornia.ipynb) | | [How to perform zero-shot object detection with OWL-ViT](https://github.com/huggingface/notebooks/blob/main/examples/zeroshot_object_detection_with_owlvit.ipynb) | Show how to perform zero-shot object detection on images with text queries | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/zeroshot_object_detection_with_owlvit.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/zeroshot_object_detection_with_owlvit.ipynb) | | [How to fine-tune an image captioning model](https://github.com/huggingface/notebooks/blob/main/examples/image_captioning_blip.ipynb) | Show how to fine-tune BLIP for image captioning on a custom dataset | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_captioning_blip.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/image_captioning_blip.ipynb) | | [How to build an image similarity system with Transformers](https://github.com/huggingface/notebooks/blob/main/examples/image_similarity.ipynb) | Show how to build an image similarity system | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_similarity.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/image_similarity.ipynb) | | [How to fine-tune a SegFormer model on semantic segmentation](https://github.com/huggingface/notebooks/blob/main/examples/semantic_segmentation.ipynb) | Show how to preprocess the data and fine-tune a pretrained SegFormer model on Semantic Segmentation | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/semantic_segmentation.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/semantic_segmentation.ipynb) | | [How to fine-tune a VideoMAE model on video classification](https://github.com/huggingface/notebooks/blob/main/examples/video_classification.ipynb) | Show how to preprocess the data and fine-tune a pretrained VideoMAE model on Video Classification | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/video_classification.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/video_classification.ipynb) | #### [](#pytorch-audio)Audio | Notebook | Description | | | | --- | --- | --- | --- | | [How to fine-tune a speech recognition model in English](https://github.com/huggingface/notebooks/blob/main/examples/speech_recognition.ipynb) | Show how to preprocess the data and fine-tune a pretrained Speech model on TIMIT | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/speech_recognition.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/speech_recognition.ipynb) | | [How to fine-tune a speech recognition model in any language](https://github.com/huggingface/notebooks/blob/main/examples/multi_lingual_speech_recognition.ipynb) | Show how to preprocess the data and fine-tune a multi-lingually pretrained speech model on Common Voice | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multi_lingual_speech_recognition.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/multi_lingual_speech_recognition.ipynb) | | [How to fine-tune a model on audio classification](https://github.com/huggingface/notebooks/blob/main/examples/audio_classification.ipynb) | Show how to preprocess the data and fine-tune a pretrained Speech model on Keyword Spotting | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/audio_classification.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/audio_classification.ipynb) | #### [](#pytorch-other)Other modalities | Notebook | Description | | | | --- | --- | --- | --- | | [How to fine-tune a pre-trained protein model](https://github.com/huggingface/notebooks/blob/main/examples/protein_language_modeling.ipynb) | See how to tokenize proteins and fine-tune a large pre-trained protein “language” model | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/protein_language_modeling.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/protein_language_modeling.ipynb) | | [How to generate protein folds](https://github.com/huggingface/notebooks/blob/main/examples/protein_folding.ipynb) | See how to go from protein sequence to a full protein model and PDB file | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/protein_folding.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/protein_folding.ipynb) | | [Probabilistic Time Series Forecasting](https://github.com/huggingface/notebooks/blob/main/examples/time-series-transformers.ipynb) | See how to train Time Series Transformer on a custom dataset | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/time-series-transformers.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/time-series-transformers.ipynb) | #### [](#pytorch-utility)Utility notebooks | Notebook | Description | | | | --- | --- | --- | --- | | [How to export model to ONNX](https://github.com/huggingface/notebooks/blob/main/examples/onnx-export.ipynb) | Highlight how to export and run inference workloads through ONNX | | | | [How to use Benchmarks](https://github.com/huggingface/notebooks/blob/main/examples/benchmark.ipynb) | How to benchmark models with transformers | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/benchmark.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/benchmark.ipynb) | ### [](#tensorflow-examples)TensorFlow Examples #### [](#tensorflow-nlp)Natural Language Processing | Notebook | Description | | | | --- | --- | --- | --- | | [Train your tokenizer](https://github.com/huggingface/notebooks/blob/main/examples/tokenizer_training.ipynb) | How to train and use your very own tokenizer | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/tokenizer_training.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/tokenizer_training.ipynb) | | [Train your language model](https://github.com/huggingface/notebooks/blob/main/examples/language_modeling_from_scratch-tf.ipynb) | How to easily start using transformers | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling_from_scratch-tf.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/language_modeling_from_scratch-tf.ipynb) | | [How to fine-tune a model on text classification](https://github.com/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on any GLUE task. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb) | | [How to fine-tune a model on language modeling](https://github.com/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on a causal or masked LM task. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb) | | [How to fine-tune a model on token classification](https://github.com/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on a token classification task (NER, PoS). | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb) | | [How to fine-tune a model on question answering](https://github.com/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on SQUAD. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb) | | [How to fine-tune a model on multiple choice](https://github.com/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on SWAG. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb) | | [How to fine-tune a model on translation](https://github.com/huggingface/notebooks/blob/main/examples/translation-tf.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on WMT. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation-tf.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/translation-tf.ipynb) | | [How to fine-tune a model on summarization](https://github.com/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb) | Show how to preprocess the data and fine-tune a pretrained model on XSUM. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb) | #### [](#tensorflow-cv)Computer Vision | Notebook | Description | | | | --- | --- | --- | --- | | [How to fine-tune a model on image classification](https://github.com/huggingface/notebooks/blob/main/examples/image_classification-tf.ipynb) | Show how to preprocess the data and fine-tune any pretrained Vision model on Image Classification | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification-tf.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/image_classification-tf.ipynb) | | [How to fine-tune a SegFormer model on semantic segmentation](https://github.com/huggingface/notebooks/blob/main/examples/semantic_segmentation-tf.ipynb) | Show how to preprocess the data and fine-tune a pretrained SegFormer model on Semantic Segmentation | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/semantic_segmentation-tf.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/semantic_segmentation-tf.ipynb) | #### [](#tensorflow-other)Other modalities | Notebook | Description | | | | --- | --- | --- | --- | | [How to fine-tune a pre-trained protein model](https://github.com/huggingface/notebooks/blob/main/examples/protein_language_modeling-tf.ipynb) | See how to tokenize proteins and fine-tune a large pre-trained protein “language” model | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/protein_language_modeling-tf.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/protein_language_modeling-tf.ipynb) | #### [](#tensorflow-utility)Utility notebooks | Notebook | Description | | | | --- | --- | --- | --- | | [How to train TF/Keras models on TPU](https://github.com/huggingface/notebooks/blob/main/examples/tpu_training-tf.ipynb) | See how to train at high speed on Google’s TPU hardware | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/tpu_training-tf.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/tpu_training-tf.ipynb) | ### [](#optimum-notebooks)Optimum notebooks 🤗 [Optimum](https://github.com/huggingface/optimum) is an extension of 🤗 Transformers, providing a set of performance optimization tools enabling maximum efficiency to train and run models on targeted hardwares. | Notebook | Description | | | | --- | --- | --- | --- | | [How to quantize a model with ONNX Runtime for text classification](https://github.com/huggingface/notebooks/blob/main/examples/text_classification_quantization_ort.ipynb) | Show how to apply static and dynamic quantization on a model using [ONNX Runtime](https://github.com/microsoft/onnxruntime) for any GLUE task. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_quantization_ort.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/text_classification_quantization_ort.ipynb) | | [How to quantize a model with Intel Neural Compressor for text classification](https://github.com/huggingface/notebooks/blob/main/examples/text_classification_quantization_inc.ipynb) | Show how to apply static, dynamic and aware training quantization on a model using [Intel Neural Compressor (INC)](https://github.com/intel/neural-compressor) for any GLUE task. | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_quantization_inc.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/text_classification_quantization_inc.ipynb) | | [How to fine-tune a model on text classification with ONNX Runtime](https://github.com/huggingface/notebooks/blob/main/examples/text_classification_ort.ipynb) | Show how to preprocess the data and fine-tune a model on any GLUE task using [ONNX Runtime](https://github.com/microsoft/onnxruntime). | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_ort.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/text_classification_ort.ipynb) | | [How to fine-tune a model on summarization with ONNX Runtime](https://github.com/huggingface/notebooks/blob/main/examples/summarization_ort.ipynb) | Show how to preprocess the data and fine-tune a model on XSUM using [ONNX Runtime](https://github.com/microsoft/onnxruntime). | [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization_ort.ipynb) | [![Open in AWS Studio](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/summarization_ort.ipynb) | ## [](#community-notebooks)Community notebooks: More notebooks developed by the community are available [here](https://hf.co/docs/transformers/community#community-notebooks).
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="🤗 Transformers Notebooks"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/notebooks"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>🤗 Transformers Notebooks</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;transformers-notebooks&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;hugging-faces-notebooks&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;documentation-notebooks&quot;,&quot;title&quot;:&quot;Documentation notebooks&quot;},{&quot;local&quot;:&quot;pytorch-examples&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;pytorch-nlp&quot;,&quot;title&quot;:&quot;Natural Language Processing&quot;},{&quot;local&quot;:&quot;pytorch-cv&quot;,&quot;title&quot;:&quot;Computer Vision&quot;},{&quot;local&quot;:&quot;pytorch-audio&quot;,&quot;title&quot;:&quot;Audio&quot;},{&quot;local&quot;:&quot;pytorch-other&quot;,&quot;title&quot;:&quot;Other modalities&quot;},{&quot;local&quot;:&quot;pytorch-utility&quot;,&quot;title&quot;:&quot;Utility notebooks&quot;}],&quot;title&quot;:&quot;PyTorch Examples&quot;},{&quot;local&quot;:&quot;tensorflow-examples&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;tensorflow-nlp&quot;,&quot;title&quot;:&quot;Natural Language Processing&quot;},{&quot;local&quot;:&quot;tensorflow-cv&quot;,&quot;title&quot;:&quot;Computer Vision&quot;},{&quot;local&quot;:&quot;tensorflow-other&quot;,&quot;title&quot;:&quot;Other modalities&quot;},{&quot;local&quot;:&quot;tensorflow-utility&quot;,&quot;title&quot;:&quot;Utility notebooks&quot;}],&quot;title&quot;:&quot;TensorFlow Examples&quot;},{&quot;local&quot;:&quot;optimum-notebooks&quot;,&quot;title&quot;:&quot;Optimum notebooks&quot;}],&quot;title&quot;:&quot;Hugging Face's notebooks 🤗&quot;},{&quot;local&quot;:&quot;community-notebooks&quot;,&quot;title&quot;:&quot;Community notebooks:&quot;}],&quot;title&quot;:&quot;🤗 Transformers Notebooks&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;notebooks&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;🤗 Transformers Notebooks&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">🤗 Transformers Notebooks</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="transformers-notebooks" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers-notebooks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>🤗 Transformers Notebooks</span></h1> <p>You can find here a list of the official notebooks provided by Hugging Face.</p> <p>Also, we would like to list here interesting content created by the community. If you wrote some notebook(s) leveraging 🤗 Transformers and would like be listed here, please open a Pull Request so it can be included under the Community notebooks.</p> <h2 class="relative group"><a id="hugging-faces-notebooks" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#hugging-faces-notebooks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Hugging Face's notebooks 🤗</span></h2> <h3 class="relative group"><a id="documentation-notebooks" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#documentation-notebooks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Documentation notebooks</span></h3> <p>You can open any page of the documentation as a notebook in Colab (there is a button directly on said pages) but they are also listed here if you need them:</p> <table><thead><tr><th align="left">Notebook</th> <th align="left">Description</th> <th align="left"></th> <th align="right"></th></tr></thead> <tbody><tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/transformers_doc/en/quicktour.ipynb" rel="nofollow">Quicktour of the library</a></td> <td align="left">A presentation of the various APIs in Transformers</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/quicktour.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/en/transformers_doc/quicktour.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/transformers_doc/en/task_summary.ipynb" rel="nofollow">Summary of the tasks</a></td> <td align="left">How to run the models of the Transformers library task by task</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/task_summary.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/task_summary.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/transformers_doc/en/preprocessing.ipynb" rel="nofollow">Preprocessing data</a></td> <td align="left">How to use a tokenizer to preprocess your data</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/preprocessing.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/preprocessing.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/transformers_doc/en/training.ipynb" rel="nofollow">Fine-tuning a pretrained model</a></td> <td align="left">How to use the Trainer to fine-tune a pretrained model</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/training.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/training.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/transformers_doc/en/tokenizer_summary.ipynb" rel="nofollow">Summary of the tokenizers</a></td> <td align="left">The differences between the tokenizers algorithm</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/tokenizer_summary.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/tokenizer_summary.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/transformers_doc/en/multilingual.ipynb" rel="nofollow">Multilingual models</a></td> <td align="left">How to use the multilingual models of the library</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/multilingual.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/multilingual.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr></tbody></table> <h3 class="relative group"><a id="pytorch-examples" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pytorch-examples"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>PyTorch Examples</span></h3> <h4 class="relative group"><a id="pytorch-nlp" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pytorch-nlp"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Natural Language Processing</span></h4> <table><thead><tr><th align="left">Notebook</th> <th align="left">Description</th> <th align="left"></th> <th align="right"></th></tr></thead> <tbody><tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/tokenizer_training.ipynb" rel="nofollow">Train your tokenizer</a></td> <td align="left">How to train and use your very own tokenizer</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/tokenizer_training.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/tokenizer_training.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/language_modeling_from_scratch.ipynb" rel="nofollow">Train your language model</a></td> <td align="left">How to easily start using transformers</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling_from_scratch.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/language_modeling_from_scratch.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/text_classification.ipynb" rel="nofollow">How to fine-tune a model on text classification</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on any GLUE task.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/language_modeling.ipynb" rel="nofollow">How to fine-tune a model on language modeling</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on a causal or masked LM task.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/language_modeling.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/token_classification.ipynb" rel="nofollow">How to fine-tune a model on token classification</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on a token classification task (NER, PoS).</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/token_classification.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/question_answering.ipynb" rel="nofollow">How to fine-tune a model on question answering</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on SQUAD.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/question_answering.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb" rel="nofollow">How to fine-tune a model on multiple choice</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on SWAG.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/multiple_choice.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/translation.ipynb" rel="nofollow">How to fine-tune a model on translation</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on WMT.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/translation.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/summarization.ipynb" rel="nofollow">How to fine-tune a model on summarization</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on XSUM.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/summarization.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/blog/blob/main/notebooks/01_how_to_train.ipynb" rel="nofollow">How to train a language model from scratch</a></td> <td align="left">Highlight all the steps to effectively train Transformer model on custom data</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/blog/blob/main/notebooks/01_how_to_train.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/blog/blob/main/notebooks/01_how_to_train.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/blog/blob/main/notebooks/02_how_to_generate.ipynb" rel="nofollow">How to generate text</a></td> <td align="left">How to use different decoding methods for language generation with transformers</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/blog/blob/main/notebooks/02_how_to_generate.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/blog/blob/main/notebooks/02_how_to_generate.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/blog/blob/main/notebooks/53_constrained_beam_search.ipynb" rel="nofollow">How to generate text (with constraints)</a></td> <td align="left">How to guide language generation with user-provided constraints</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/blog/blob/main/notebooks/53_constrained_beam_search.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/blog/blob/main/notebooks/53_constrained_beam_search.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/blog/blob/main/notebooks/03_reformer.ipynb" rel="nofollow">Reformer</a></td> <td align="left">How Reformer pushes the limits of language modeling</td> <td align="left"><a href="https://colab.research.google.com/github/patrickvonplaten/blog/blob/main/notebooks/03_reformer.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/patrickvonplaten/blog/blob/main/notebooks/03_reformer.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr></tbody></table> <h4 class="relative group"><a id="pytorch-cv" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pytorch-cv"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Computer Vision</span></h4> <table><thead><tr><th align="left">Notebook</th> <th align="left">Description</th> <th align="left"></th> <th align="right"></th></tr></thead> <tbody><tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/image_classification.ipynb" rel="nofollow">How to fine-tune a model on image classification (Torchvision)</a></td> <td align="left">Show how to preprocess the data using Torchvision and fine-tune any pretrained Vision model on Image Classification</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/image_classification_albumentations.ipynb" rel="nofollow">How to fine-tune a model on image classification (Albumentations)</a></td> <td align="left">Show how to preprocess the data using Albumentations and fine-tune any pretrained Vision model on Image Classification</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_albumentations.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/image_classification_albumentations.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/image_classification_kornia.ipynb" rel="nofollow">How to fine-tune a model on image classification (Kornia)</a></td> <td align="left">Show how to preprocess the data using Kornia and fine-tune any pretrained Vision model on Image Classification</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_kornia.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/image_classification_kornia.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/zeroshot_object_detection_with_owlvit.ipynb" rel="nofollow">How to perform zero-shot object detection with OWL-ViT</a></td> <td align="left">Show how to perform zero-shot object detection on images with text queries</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/zeroshot_object_detection_with_owlvit.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/zeroshot_object_detection_with_owlvit.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/image_captioning_blip.ipynb" rel="nofollow">How to fine-tune an image captioning model</a></td> <td align="left">Show how to fine-tune BLIP for image captioning on a custom dataset</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_captioning_blip.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/image_captioning_blip.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/image_similarity.ipynb" rel="nofollow">How to build an image similarity system with Transformers</a></td> <td align="left">Show how to build an image similarity system</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_similarity.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/image_similarity.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/semantic_segmentation.ipynb" rel="nofollow">How to fine-tune a SegFormer model on semantic segmentation</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained SegFormer model on Semantic Segmentation</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/semantic_segmentation.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/semantic_segmentation.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/video_classification.ipynb" rel="nofollow">How to fine-tune a VideoMAE model on video classification</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained VideoMAE model on Video Classification</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/video_classification.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/video_classification.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr></tbody></table> <h4 class="relative group"><a id="pytorch-audio" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pytorch-audio"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Audio</span></h4> <table><thead><tr><th align="left">Notebook</th> <th align="left">Description</th> <th align="left"></th> <th align="right"></th></tr></thead> <tbody><tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/speech_recognition.ipynb" rel="nofollow">How to fine-tune a speech recognition model in English</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained Speech model on TIMIT</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/speech_recognition.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/speech_recognition.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/multi_lingual_speech_recognition.ipynb" rel="nofollow">How to fine-tune a speech recognition model in any language</a></td> <td align="left">Show how to preprocess the data and fine-tune a multi-lingually pretrained speech model on Common Voice</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multi_lingual_speech_recognition.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/multi_lingual_speech_recognition.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/audio_classification.ipynb" rel="nofollow">How to fine-tune a model on audio classification</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained Speech model on Keyword Spotting</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/audio_classification.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/audio_classification.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr></tbody></table> <h4 class="relative group"><a id="pytorch-other" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pytorch-other"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Other modalities</span></h4> <table><thead><tr><th align="left">Notebook</th> <th align="left">Description</th> <th align="left"></th> <th align="right"></th></tr></thead> <tbody><tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/protein_language_modeling.ipynb" rel="nofollow">How to fine-tune a pre-trained protein model</a></td> <td align="left">See how to tokenize proteins and fine-tune a large pre-trained protein “language” model</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/protein_language_modeling.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/protein_language_modeling.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/protein_folding.ipynb" rel="nofollow">How to generate protein folds</a></td> <td align="left">See how to go from protein sequence to a full protein model and PDB file</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/protein_folding.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/protein_folding.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/time-series-transformers.ipynb" rel="nofollow">Probabilistic Time Series Forecasting</a></td> <td align="left">See how to train Time Series Transformer on a custom dataset</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/time-series-transformers.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/time-series-transformers.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr></tbody></table> <h4 class="relative group"><a id="pytorch-utility" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pytorch-utility"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Utility notebooks</span></h4> <table><thead><tr><th align="left">Notebook</th> <th align="left">Description</th> <th align="left"></th> <th align="right"></th></tr></thead> <tbody><tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/onnx-export.ipynb" rel="nofollow">How to export model to ONNX</a></td> <td align="left">Highlight how to export and run inference workloads through ONNX</td> <td align="left"></td> <td align="right"></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/benchmark.ipynb" rel="nofollow">How to use Benchmarks</a></td> <td align="left">How to benchmark models with transformers</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/benchmark.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/benchmark.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr></tbody></table> <h3 class="relative group"><a id="tensorflow-examples" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tensorflow-examples"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>TensorFlow Examples</span></h3> <h4 class="relative group"><a id="tensorflow-nlp" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tensorflow-nlp"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Natural Language Processing</span></h4> <table><thead><tr><th align="left">Notebook</th> <th align="left">Description</th> <th align="left"></th> <th align="right"></th></tr></thead> <tbody><tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/tokenizer_training.ipynb" rel="nofollow">Train your tokenizer</a></td> <td align="left">How to train and use your very own tokenizer</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/tokenizer_training.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/tokenizer_training.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/language_modeling_from_scratch-tf.ipynb" rel="nofollow">Train your language model</a></td> <td align="left">How to easily start using transformers</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling_from_scratch-tf.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/language_modeling_from_scratch-tf.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb" rel="nofollow">How to fine-tune a model on text classification</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on any GLUE task.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb" rel="nofollow">How to fine-tune a model on language modeling</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on a causal or masked LM task.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/language_modeling-tf.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb" rel="nofollow">How to fine-tune a model on token classification</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on a token classification task (NER, PoS).</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/token_classification-tf.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb" rel="nofollow">How to fine-tune a model on question answering</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on SQUAD.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/question_answering-tf.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb" rel="nofollow">How to fine-tune a model on multiple choice</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on SWAG.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/multiple_choice-tf.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/translation-tf.ipynb" rel="nofollow">How to fine-tune a model on translation</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on WMT.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/translation-tf.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/translation-tf.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb" rel="nofollow">How to fine-tune a model on summarization</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained model on XSUM.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr></tbody></table> <h4 class="relative group"><a id="tensorflow-cv" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tensorflow-cv"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Computer Vision</span></h4> <table><thead><tr><th align="left">Notebook</th> <th align="left">Description</th> <th align="left"></th> <th align="right"></th></tr></thead> <tbody><tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/image_classification-tf.ipynb" rel="nofollow">How to fine-tune a model on image classification</a></td> <td align="left">Show how to preprocess the data and fine-tune any pretrained Vision model on Image Classification</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification-tf.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/image_classification-tf.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/semantic_segmentation-tf.ipynb" rel="nofollow">How to fine-tune a SegFormer model on semantic segmentation</a></td> <td align="left">Show how to preprocess the data and fine-tune a pretrained SegFormer model on Semantic Segmentation</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/semantic_segmentation-tf.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/semantic_segmentation-tf.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr></tbody></table> <h4 class="relative group"><a id="tensorflow-other" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tensorflow-other"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Other modalities</span></h4> <table><thead><tr><th align="left">Notebook</th> <th align="left">Description</th> <th align="left"></th> <th align="right"></th></tr></thead> <tbody><tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/protein_language_modeling-tf.ipynb" rel="nofollow">How to fine-tune a pre-trained protein model</a></td> <td align="left">See how to tokenize proteins and fine-tune a large pre-trained protein “language” model</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/protein_language_modeling-tf.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/protein_language_modeling-tf.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr></tbody></table> <h4 class="relative group"><a id="tensorflow-utility" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tensorflow-utility"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Utility notebooks</span></h4> <table><thead><tr><th align="left">Notebook</th> <th align="left">Description</th> <th align="left"></th> <th align="right"></th></tr></thead> <tbody><tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/tpu_training-tf.ipynb" rel="nofollow">How to train TF/Keras models on TPU</a></td> <td align="left">See how to train at high speed on Google’s TPU hardware</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/tpu_training-tf.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/tpu_training-tf.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr></tbody></table> <h3 class="relative group"><a id="optimum-notebooks" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#optimum-notebooks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Optimum notebooks</span></h3> <p>🤗 <a href="https://github.com/huggingface/optimum" rel="nofollow">Optimum</a> is an extension of 🤗 Transformers, providing a set of performance optimization tools enabling maximum efficiency to train and run models on targeted hardwares.</p> <table><thead><tr><th align="left">Notebook</th> <th align="left">Description</th> <th align="left"></th> <th align="right"></th></tr></thead> <tbody><tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/text_classification_quantization_ort.ipynb" rel="nofollow">How to quantize a model with ONNX Runtime for text classification</a></td> <td align="left">Show how to apply static and dynamic quantization on a model using <a href="https://github.com/microsoft/onnxruntime" rel="nofollow">ONNX Runtime</a> for any GLUE task.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_quantization_ort.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/text_classification_quantization_ort.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/text_classification_quantization_inc.ipynb" rel="nofollow">How to quantize a model with Intel Neural Compressor for text classification</a></td> <td align="left">Show how to apply static, dynamic and aware training quantization on a model using <a href="https://github.com/intel/neural-compressor" rel="nofollow">Intel Neural Compressor (INC)</a> for any GLUE task.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_quantization_inc.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/text_classification_quantization_inc.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/text_classification_ort.ipynb" rel="nofollow">How to fine-tune a model on text classification with ONNX Runtime</a></td> <td align="left">Show how to preprocess the data and fine-tune a model on any GLUE task using <a href="https://github.com/microsoft/onnxruntime" rel="nofollow">ONNX Runtime</a>.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_ort.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/text_classification_ort.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr> <tr><td align="left"><a href="https://github.com/huggingface/notebooks/blob/main/examples/summarization_ort.ipynb" rel="nofollow">How to fine-tune a model on summarization with ONNX Runtime</a></td> <td align="left">Show how to preprocess the data and fine-tune a model on XSUM using <a href="https://github.com/microsoft/onnxruntime" rel="nofollow">ONNX Runtime</a>.</td> <td align="left"><a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization_ort.ipynb" rel="nofollow"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open in Colab"></a></td> <td align="right"><a href="https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/examples/summarization_ort.ipynb" rel="nofollow"><img src="https://studiolab.sagemaker.aws/studiolab.svg" alt="Open in AWS Studio"></a></td></tr></tbody></table> <h2 class="relative group"><a id="community-notebooks" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#community-notebooks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Community notebooks:</span></h2> <p>More notebooks developed by the community are available <a href="https://hf.co/docs/transformers/community#community-notebooks" rel="nofollow">here</a>.</p> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/benchmarks" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Benchmarks</a> <a href="/docs/transformers/community" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Community resources<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;🤗 Transformers Notebooks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers-notebooks&quot;,&quot;url&quot;:&quot;#transformers-notebooks&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Hugging Face's notebooks 🤗&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hugging-faces-notebooks&quot;,&quot;url&quot;:&quot;#hugging-faces-notebooks&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Documentation notebooks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;documentation-notebooks&quot;,&quot;url&quot;:&quot;#documentation-notebooks&quot;},{&quot;title&quot;:&quot;PyTorch Examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pytorch-examples&quot;,&quot;url&quot;:&quot;#pytorch-examples&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pytorch-nlp&quot;,&quot;url&quot;:&quot;#pytorch-nlp&quot;},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pytorch-cv&quot;,&quot;url&quot;:&quot;#pytorch-cv&quot;},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pytorch-audio&quot;,&quot;url&quot;:&quot;#pytorch-audio&quot;},{&quot;title&quot;:&quot;Other modalities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pytorch-other&quot;,&quot;url&quot;:&quot;#pytorch-other&quot;},{&quot;title&quot;:&quot;Utility notebooks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pytorch-utility&quot;,&quot;url&quot;:&quot;#pytorch-utility&quot;}]},{&quot;title&quot;:&quot;TensorFlow Examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tensorflow-examples&quot;,&quot;url&quot;:&quot;#tensorflow-examples&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tensorflow-nlp&quot;,&quot;url&quot;:&quot;#tensorflow-nlp&quot;},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tensorflow-cv&quot;,&quot;url&quot;:&quot;#tensorflow-cv&quot;},{&quot;title&quot;:&quot;Other modalities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tensorflow-other&quot;,&quot;url&quot;:&quot;#tensorflow-other&quot;},{&quot;title&quot;:&quot;Utility notebooks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tensorflow-utility&quot;,&quot;url&quot;:&quot;#tensorflow-utility&quot;}]},{&quot;title&quot;:&quot;Optimum notebooks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;optimum-notebooks&quot;,&quot;url&quot;:&quot;#optimum-notebooks&quot;}]},{&quot;title&quot;:&quot;Community notebooks:&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community-notebooks&quot;,&quot;url&quot;:&quot;#community-notebooks&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#transformers-notebooks" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-transformers-notebooks">🤗 <wbr>Transformers <wbr>Notebooks</a> <a href="#hugging-faces-notebooks" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-hugging-faces-notebooks"><wbr>Hugging <wbr>Face's notebooks 🤗</a> <a href="#documentation-notebooks" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-documentation-notebooks"><wbr>Documentation notebooks</a> <a href="#pytorch-examples" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-pytorch-examples"><wbr>Py<wbr>Torch <wbr>Examples</a> <a href="#pytorch-nlp" class="pl-12 text-xs text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-pytorch-nlp"><wbr>Natural <wbr>Language <wbr>Processing</a><a href="#pytorch-cv" class="pl-12 text-xs text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-pytorch-cv"><wbr>Computer <wbr>Vision</a><a href="#pytorch-audio" class="pl-12 text-xs text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-pytorch-audio"><wbr>Audio</a><a href="#pytorch-other" class="pl-12 text-xs text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-pytorch-other"><wbr>Other modalities</a><a href="#pytorch-utility" class="pl-12 text-xs text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-pytorch-utility"><wbr>Utility notebooks</a><a href="#tensorflow-examples" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tensorflow-examples"><wbr>Tensor<wbr>Flow <wbr>Examples</a> <a href="#tensorflow-nlp" class="pl-12 text-xs text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tensorflow-nlp"><wbr>Natural <wbr>Language <wbr>Processing</a><a href="#tensorflow-cv" class="pl-12 text-xs text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tensorflow-cv"><wbr>Computer <wbr>Vision</a><a href="#tensorflow-other" class="pl-12 text-xs text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tensorflow-other"><wbr>Other modalities</a><a href="#tensorflow-utility" class="pl-12 text-xs text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-tensorflow-utility"><wbr>Utility notebooks</a><a href="#optimum-notebooks" class="pl-8 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-optimum-notebooks"><wbr>Optimum notebooks</a> <a href="#community-notebooks" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-community-notebooks"><wbr>Community notebooks:</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/notebooks" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/notebooks"); } </script> <iframe name="__privateStripeMetricsController1600" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fnotebooks&amp;title=%F0%9F%A4%97%20Transformers%20Notebooks&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:13.820Z
Benchmarks
https://huggingface.co/docs/transformers/benchmarks
Hugging Face’s Benchmarking tools are deprecated and it is advised to use external Benchmarking libraries to measure the speed and memory complexity of Transformer models. Let’s take a look at how 🤗 Transformers models can be benchmarked, best practices, and already available benchmarks. A notebook explaining in more detail how to benchmark 🤗 Transformers models can be found [here](https://github.com/huggingface/notebooks/tree/main/examples/benchmark.ipynb). ## [](#how-to-benchmark-transformers-models)How to benchmark 🤗 Transformers models The classes `PyTorchBenchmark` and `TensorFlowBenchmark` allow to flexibly benchmark 🤗 Transformers models. The benchmark classes allow us to measure the _peak memory usage_ and _required time_ for both _inference_ and _training_. Hereby, _inference_ is defined by a single forward pass, and _training_ is defined by a single forward pass and backward pass. The benchmark classes `PyTorchBenchmark` and `TensorFlowBenchmark` expect an object of type `PyTorchBenchmarkArguments` and `TensorFlowBenchmarkArguments`, respectively, for instantiation. `PyTorchBenchmarkArguments` and `TensorFlowBenchmarkArguments` are data classes and contain all relevant configurations for their corresponding benchmark class. In the following example, it is shown how a BERT model of type _bert-base-cased_ can be benchmarked. ``` >>> from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments >>> args = PyTorchBenchmarkArguments(models=["bert-base-uncased"], batch_sizes=[8], sequence_lengths=[8, 32, 128, 512]) >>> benchmark = PyTorchBenchmark(args)``` ``` >>> from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments >>> args = TensorFlowBenchmarkArguments( ... models=["bert-base-uncased"], batch_sizes=[8], sequence_lengths=[8, 32, 128, 512] ... ) >>> benchmark = TensorFlowBenchmark(args)``` Here, three arguments are given to the benchmark argument data classes, namely `models`, `batch_sizes`, and `sequence_lengths`. The argument `models` is required and expects a `list` of model identifiers from the [model hub](https://huggingface.co/models) The `list` arguments `batch_sizes` and `sequence_lengths` define the size of the `input_ids` on which the model is benchmarked. There are many more parameters that can be configured via the benchmark argument data classes. For more detail on these one can either directly consult the files `src/transformers/benchmark/benchmark_args_utils.py`, `src/transformers/benchmark/benchmark_args.py` (for PyTorch) and `src/transformers/benchmark/benchmark_args_tf.py` (for Tensorflow). Alternatively, running the following shell commands from root will print out a descriptive list of all configurable parameters for PyTorch and Tensorflow respectively. ``` python examples/pytorch/benchmarking/run_benchmark.py --help``` An instantiated benchmark object can then simply be run by calling `benchmark.run()`. ``` >>> results = benchmark.run() >>> print(results) ==================== INFERENCE - SPEED - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Time in s -------------------------------------------------------------------------------- bert-base-uncased 8 8 0.006 bert-base-uncased 8 32 0.006 bert-base-uncased 8 128 0.018 bert-base-uncased 8 512 0.088 -------------------------------------------------------------------------------- ==================== INFERENCE - MEMORY - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Memory in MB -------------------------------------------------------------------------------- bert-base-uncased 8 8 1227 bert-base-uncased 8 32 1281 bert-base-uncased 8 128 1307 bert-base-uncased 8 512 1539 -------------------------------------------------------------------------------- ==================== ENVIRONMENT INFORMATION ==================== - transformers_version: 2.11.0 - framework: PyTorch - use_torchscript: False - framework_version: 1.4.0 - python_version: 3.6.10 - system: Linux - cpu: x86_64 - architecture: 64bit - date: 2020-06-29 - time: 08:58:43.371351 - fp16: False - use_multiprocessing: True - only_pretrain_model: False - cpu_ram_mb: 32088 - use_gpu: True - num_gpus: 1 - gpu: TITAN RTX - gpu_ram_mb: 24217 - gpu_power_watts: 280.0 - gpu_performance_state: 2 - use_tpu: False``` ``` python examples/tensorflow/benchmarking/run_benchmark_tf.py --help``` An instantiated benchmark object can then simply be run by calling `benchmark.run()`. ``` >>> results = benchmark.run() >>> print(results) >>> results = benchmark.run() >>> print(results) ==================== INFERENCE - SPEED - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Time in s -------------------------------------------------------------------------------- bert-base-uncased 8 8 0.005 bert-base-uncased 8 32 0.008 bert-base-uncased 8 128 0.022 bert-base-uncased 8 512 0.105 -------------------------------------------------------------------------------- ==================== INFERENCE - MEMORY - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Memory in MB -------------------------------------------------------------------------------- bert-base-uncased 8 8 1330 bert-base-uncased 8 32 1330 bert-base-uncased 8 128 1330 bert-base-uncased 8 512 1770 -------------------------------------------------------------------------------- ==================== ENVIRONMENT INFORMATION ==================== - transformers_version: 2.11.0 - framework: Tensorflow - use_xla: False - framework_version: 2.2.0 - python_version: 3.6.10 - system: Linux - cpu: x86_64 - architecture: 64bit - date: 2020-06-29 - time: 09:26:35.617317 - fp16: False - use_multiprocessing: True - only_pretrain_model: False - cpu_ram_mb: 32088 - use_gpu: True - num_gpus: 1 - gpu: TITAN RTX - gpu_ram_mb: 24217 - gpu_power_watts: 280.0 - gpu_performance_state: 2 - use_tpu: False``` By default, the _time_ and the _required memory_ for _inference_ are benchmarked. In the example output above the first two sections show the result corresponding to _inference time_ and _inference memory_. In addition, all relevant information about the computing environment, _e.g._ the GPU type, the system, the library versions, etc… are printed out in the third section under _ENVIRONMENT INFORMATION_. This information can optionally be saved in a _.csv_ file when adding the argument `save_to_csv=True` to `PyTorchBenchmarkArguments` and `TensorFlowBenchmarkArguments` respectively. In this case, every section is saved in a separate _.csv_ file. The path to each _.csv_ file can optionally be defined via the argument data classes. Instead of benchmarking pre-trained models via their model identifier, _e.g._ `bert-base-uncased`, the user can alternatively benchmark an arbitrary configuration of any available model class. In this case, a `list` of configurations must be inserted with the benchmark args as follows. ``` >>> from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments, BertConfig >>> args = PyTorchBenchmarkArguments( ... models=["bert-base", "bert-384-hid", "bert-6-lay"], batch_sizes=[8], sequence_lengths=[8, 32, 128, 512] ... ) >>> config_base = BertConfig() >>> config_384_hid = BertConfig(hidden_size=384) >>> config_6_lay = BertConfig(num_hidden_layers=6) >>> benchmark = PyTorchBenchmark(args, configs=[config_base, config_384_hid, config_6_lay]) >>> benchmark.run() ==================== INFERENCE - SPEED - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Time in s -------------------------------------------------------------------------------- bert-base 8 128 0.006 bert-base 8 512 0.006 bert-base 8 128 0.018 bert-base 8 512 0.088 bert-384-hid 8 8 0.006 bert-384-hid 8 32 0.006 bert-384-hid 8 128 0.011 bert-384-hid 8 512 0.054 bert-6-lay 8 8 0.003 bert-6-lay 8 32 0.004 bert-6-lay 8 128 0.009 bert-6-lay 8 512 0.044 -------------------------------------------------------------------------------- ==================== INFERENCE - MEMORY - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Memory in MB -------------------------------------------------------------------------------- bert-base 8 8 1277 bert-base 8 32 1281 bert-base 8 128 1307 bert-base 8 512 1539 bert-384-hid 8 8 1005 bert-384-hid 8 32 1027 bert-384-hid 8 128 1035 bert-384-hid 8 512 1255 bert-6-lay 8 8 1097 bert-6-lay 8 32 1101 bert-6-lay 8 128 1127 bert-6-lay 8 512 1359 -------------------------------------------------------------------------------- ==================== ENVIRONMENT INFORMATION ==================== - transformers_version: 2.11.0 - framework: PyTorch - use_torchscript: False - framework_version: 1.4.0 - python_version: 3.6.10 - system: Linux - cpu: x86_64 - architecture: 64bit - date: 2020-06-29 - time: 09:35:25.143267 - fp16: False - use_multiprocessing: True - only_pretrain_model: False - cpu_ram_mb: 32088 - use_gpu: True - num_gpus: 1 - gpu: TITAN RTX - gpu_ram_mb: 24217 - gpu_power_watts: 280.0 - gpu_performance_state: 2 - use_tpu: False``` ``` >>> from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments, BertConfig >>> args = TensorFlowBenchmarkArguments( ... models=["bert-base", "bert-384-hid", "bert-6-lay"], batch_sizes=[8], sequence_lengths=[8, 32, 128, 512] ... ) >>> config_base = BertConfig() >>> config_384_hid = BertConfig(hidden_size=384) >>> config_6_lay = BertConfig(num_hidden_layers=6) >>> benchmark = TensorFlowBenchmark(args, configs=[config_base, config_384_hid, config_6_lay]) >>> benchmark.run() ==================== INFERENCE - SPEED - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Time in s -------------------------------------------------------------------------------- bert-base 8 8 0.005 bert-base 8 32 0.008 bert-base 8 128 0.022 bert-base 8 512 0.106 bert-384-hid 8 8 0.005 bert-384-hid 8 32 0.007 bert-384-hid 8 128 0.018 bert-384-hid 8 512 0.064 bert-6-lay 8 8 0.002 bert-6-lay 8 32 0.003 bert-6-lay 8 128 0.0011 bert-6-lay 8 512 0.074 -------------------------------------------------------------------------------- ==================== INFERENCE - MEMORY - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Memory in MB -------------------------------------------------------------------------------- bert-base 8 8 1330 bert-base 8 32 1330 bert-base 8 128 1330 bert-base 8 512 1770 bert-384-hid 8 8 1330 bert-384-hid 8 32 1330 bert-384-hid 8 128 1330 bert-384-hid 8 512 1540 bert-6-lay 8 8 1330 bert-6-lay 8 32 1330 bert-6-lay 8 128 1330 bert-6-lay 8 512 1540 -------------------------------------------------------------------------------- ==================== ENVIRONMENT INFORMATION ==================== - transformers_version: 2.11.0 - framework: Tensorflow - use_xla: False - framework_version: 2.2.0 - python_version: 3.6.10 - system: Linux - cpu: x86_64 - architecture: 64bit - date: 2020-06-29 - time: 09:38:15.487125 - fp16: False - use_multiprocessing: True - only_pretrain_model: False - cpu_ram_mb: 32088 - use_gpu: True - num_gpus: 1 - gpu: TITAN RTX - gpu_ram_mb: 24217 - gpu_power_watts: 280.0 - gpu_performance_state: 2 - use_tpu: False``` Again, _inference time_ and _required memory_ for _inference_ are measured, but this time for customized configurations of the `BertModel` class. This feature can especially be helpful when deciding for which configuration the model should be trained. ## [](#benchmark-best-practices)Benchmark best practices This section lists a couple of best practices one should be aware of when benchmarking a model. - Currently, only single device benchmarking is supported. When benchmarking on GPU, it is recommended that the user specifies on which device the code should be run by setting the `CUDA_VISIBLE_DEVICES` environment variable in the shell, _e.g._ `export CUDA_VISIBLE_DEVICES=0` before running the code. - The option `no_multi_processing` should only be set to `True` for testing and debugging. To ensure accurate memory measurement it is recommended to run each memory benchmark in a separate process by making sure `no_multi_processing` is set to `True`. - One should always state the environment information when sharing the results of a model benchmark. Results can vary heavily between different GPU devices, library versions, etc., so that benchmark results on their own are not very useful for the community. ## [](#sharing-your-benchmark)Sharing your benchmark Previously all available core models (10 at the time) have been benchmarked for _inference time_, across many different settings: using PyTorch, with and without TorchScript, using TensorFlow, with and without XLA. All of those tests were done across CPUs (except for TensorFlow XLA) and GPUs. The approach is detailed in the [following blogpost](https://medium.com/huggingface/benchmarking-transformers-pytorch-and-tensorflow-e2917fb891c2) and the results are available [here](https://docs.google.com/spreadsheets/d/1sryqufw2D0XlUH4sq3e9Wnxu5EAQkaohzrJbd5HdQ_w/edit?usp=sharing). With the new _benchmark_ tools, it is easier than ever to share your benchmark results with the community - [PyTorch Benchmarking Results](https://github.com/huggingface/transformers/tree/main/examples/pytorch/benchmarking/README.md). - [TensorFlow Benchmarking Results](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/benchmarking/README.md).
<!DOCTYPE html><html class=""><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <meta name="description" content="We’re on a journey to advance and democratize artificial intelligence through open source and open science."> <meta property="fb:app_id" content="1321688464574422"> <meta name="twitter:card" content="summary_large_image"> <meta name="twitter:site" content="@huggingface"> <meta property="og:title" content="Benchmarks"> <meta property="og:type" content="website"> <meta property="og:url" content="https://huggingface.co/docs/transformers/benchmarks"> <meta property="og:image" content="https://huggingface.co/front/thumbnails/docs/transformers.png"> <link rel="stylesheet" href="/front/build/kube-c0d76de/style.css"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&amp;display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&amp;display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"> <title>Benchmarks</title> <script async="" src="https://www.google-analytics.com/analytics.js"></script><script defer="" data-domain="huggingface.co" src="/js/script.js"></script> <script src="https://js.stripe.com/v3/" async=""></script><script src="https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL" async=""></script><link rel="stylesheet" href="/docs/transformers/v4.30.0/en/_app/assets/pages/__layout.svelte-hf-doc-builder.css"><link rel="modulepreload" as="script" crossorigin="" href="/docs/transformers/v4.30.0/en/_app/error.svelte-hf-doc-builder.js"><meta name="hf:doc:metadata" content="{&quot;local&quot;:&quot;benchmarks&quot;,&quot;sections&quot;:[{&quot;local&quot;:&quot;how-to-benchmark-transformers-models&quot;,&quot;title&quot;:&quot;How to benchmark 🤗 Transformers models&quot;},{&quot;local&quot;:&quot;benchmark-best-practices&quot;,&quot;title&quot;:&quot;Benchmark best practices&quot;},{&quot;local&quot;:&quot;sharing-your-benchmark&quot;,&quot;title&quot;:&quot;Sharing your benchmark&quot;}],&quot;title&quot;:&quot;Benchmarks&quot;}"></head> <body class="flex flex-col min-h-screen bg-white dark:bg-gray-950 text-black DocBuilderPage"> <div class="flex min-h-screen flex-col"> <div class="SVELTE_HYDRATER contents" data-props="{&quot;isWide&quot;:true,&quot;isZh&quot;:false}" data-target="MainHeader"><header class="border-b border-gray-100"><div class="w-full px-4 flex h-16 items-center"><div class="flex flex-1 items-center"><a class="mr-5 flex flex-none items-center lg:mr-6" href="/"><img alt="Hugging Face's logo" class="w-7 md:mr-2" src="/front/assets/huggingface_logo-noborder.svg"> <span class="hidden whitespace-nowrap text-lg font-bold md:block">Hugging Face</span></a> <div class="relative flex-1 lg:max-w-sm mr-2 sm:mr-4 lg:mr-6"><input autocomplete="off" class="w-full dark:bg-gray-950 pl-8 form-input-alt h-9 pr-3 focus:shadow-xl" name="" placeholder="Search models, datasets, users..." spellcheck="false" type="text"> <svg class="absolute left-2.5 text-gray-400 top-1/2 transform -translate-y-1/2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> </div> <button class="relative flex w-8 flex-none items-center justify-center place-self-stretch lg:hidden" type="button"><svg width="1em" height="1em" viewBox="0 0 10 10" class="text-xl" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" preserveAspectRatio="xMidYMid meet" fill="currentColor"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.65039 2.9999C1.65039 2.8066 1.80709 2.6499 2.00039 2.6499H8.00039C8.19369 2.6499 8.35039 2.8066 8.35039 2.9999C8.35039 3.1932 8.19369 3.3499 8.00039 3.3499H2.00039C1.80709 3.3499 1.65039 3.1932 1.65039 2.9999ZM1.65039 4.9999C1.65039 4.8066 1.80709 4.6499 2.00039 4.6499H8.00039C8.19369 4.6499 8.35039 4.8066 8.35039 4.9999C8.35039 5.1932 8.19369 5.3499 8.00039 5.3499H2.00039C1.80709 5.3499 1.65039 5.1932 1.65039 4.9999ZM2.00039 6.6499C1.80709 6.6499 1.65039 6.8066 1.65039 6.9999C1.65039 7.1932 1.80709 7.3499 2.00039 7.3499H8.00039C8.19369 7.3499 8.35039 7.1932 8.35039 6.9999C8.35039 6.8066 8.19369 6.6499 8.00039 6.6499H2.00039Z"></path></svg> </button> </div> <nav aria-label="Main" class="ml-auto hidden lg:block"><ul class="flex items-center space-x-2"><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-indigo-700" href="/models"><svg class="mr-1.5 text-gray-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg> Models</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-red-700" href="/datasets"><svg class="mr-1.5 text-gray-400 group-hover:text-red-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 25 25"><ellipse cx="12.5" cy="5" fill="currentColor" fill-opacity="0.25" rx="7.5" ry="2"></ellipse><path d="M12.5 15C16.6421 15 20 14.1046 20 13V20C20 21.1046 16.6421 22 12.5 22C8.35786 22 5 21.1046 5 20V13C5 14.1046 8.35786 15 12.5 15Z" fill="currentColor" opacity="0.5"></path><path d="M12.5 7C16.6421 7 20 6.10457 20 5V11.5C20 12.6046 16.6421 13.5 12.5 13.5C8.35786 13.5 5 12.6046 5 11.5V5C5 6.10457 8.35786 7 12.5 7Z" fill="currentColor" opacity="0.5"></path><path d="M5.23628 12C5.08204 12.1598 5 12.8273 5 13C5 14.1046 8.35786 15 12.5 15C16.6421 15 20 14.1046 20 13C20 12.8273 19.918 12.1598 19.7637 12C18.9311 12.8626 15.9947 13.5 12.5 13.5C9.0053 13.5 6.06886 12.8626 5.23628 12Z" fill="currentColor"></path></svg> Datasets</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-blue-700" href="/spaces"><svg class="mr-1.5 text-gray-400 group-hover:text-blue-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 25 25"><path opacity=".5" d="M6.016 14.674v4.31h4.31v-4.31h-4.31ZM14.674 14.674v4.31h4.31v-4.31h-4.31ZM6.016 6.016v4.31h4.31v-4.31h-4.31Z" fill="currentColor"></path><path opacity=".75" fill-rule="evenodd" clip-rule="evenodd" d="M3 4.914C3 3.857 3.857 3 4.914 3h6.514c.884 0 1.628.6 1.848 1.414a5.171 5.171 0 0 1 7.31 7.31c.815.22 1.414.964 1.414 1.848v6.514A1.914 1.914 0 0 1 20.086 22H4.914A1.914 1.914 0 0 1 3 20.086V4.914Zm3.016 1.102v4.31h4.31v-4.31h-4.31Zm0 12.968v-4.31h4.31v4.31h-4.31Zm8.658 0v-4.31h4.31v4.31h-4.31Zm0-10.813a2.155 2.155 0 1 1 4.31 0 2.155 2.155 0 0 1-4.31 0Z" fill="currentColor"></path><path opacity=".25" d="M16.829 6.016a2.155 2.155 0 1 0 0 4.31 2.155 2.155 0 0 0 0-4.31Z" fill="currentColor"></path></svg> Spaces</a></li><li><a class="group flex items-center px-2 py-0.5 dark:hover:text-gray-400 hover:text-yellow-700" href="/docs"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="mr-1.5 text-gray-400 group-hover:text-yellow-500" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path opacity="0.5" d="M20.9022 5.10334L10.8012 10.8791L7.76318 9.11193C8.07741 8.56791 8.5256 8.11332 9.06512 7.7914L15.9336 3.73907C17.0868 3.08811 18.5002 3.26422 19.6534 3.91519L19.3859 3.73911C19.9253 4.06087 20.5879 4.56025 20.9022 5.10334Z" fill="currentColor"></path><path d="M10.7999 10.8792V28.5483C10.2136 28.5475 9.63494 28.4139 9.10745 28.1578C8.5429 27.8312 8.074 27.3621 7.74761 26.7975C7.42122 26.2327 7.24878 25.5923 7.24756 24.9402V10.9908C7.25062 10.3319 7.42358 9.68487 7.74973 9.1123L10.7999 10.8792Z" fill="currentColor" fill-opacity="0.75"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M21.3368 10.8499V6.918C21.3331 6.25959 21.16 5.61234 20.8346 5.03949L10.7971 10.8727L10.8046 10.874L21.3368 10.8499Z" fill="currentColor"></path><path opacity="0.5" d="M21.7937 10.8488L10.7825 10.8741V28.5486L21.7937 28.5234C23.3344 28.5234 24.5835 27.2743 24.5835 25.7335V13.6387C24.5835 12.0979 23.4365 11.1233 21.7937 10.8488Z" fill="currentColor"></path></svg> Docs</a></li> <li><div class="relative "><button class="px-2 py-0.5 group hover:text-green-700 dark:hover:text-gray-400 flex items-center " type="button"><svg class="mr-1.5 text-gray-400 group-hover:text-green-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-tertiary" d="M19 6H5a3 3 0 0 0-3 3v2.72L8.837 14h6.326L22 11.72V9a3 3 0 0 0-3-3z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M10 6V5h4v1h2V5a2.002 2.002 0 0 0-2-2h-4a2.002 2.002 0 0 0-2 2v1h2zm-1.163 8L2 11.72V18a3.003 3.003 0 0 0 3 3h14a3.003 3.003 0 0 0 3-3v-6.28L15.163 14H8.837z" fill="currentColor"></path></svg> Solutions </button> </div></li> <li><a class="group flex items-center px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/pricing">Pricing</a></li> <li><div class="relative group"><button class="px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-600 flex items-center " type="button"><svg class="mr-1.5 text-gray-500 w-5 group-hover:text-gray-400 dark:text-gray-300 dark:group-hover:text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" viewBox="0 0 32 18" preserveAspectRatio="xMidYMid meet"><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 3.30221C14.4504 2.836 14.8284 2.45807 15.2946 2.45807H28.4933C28.9595 2.45807 29.3374 2.836 29.3374 3.30221C29.3374 3.76842 28.9595 4.14635 28.4933 4.14635H15.2946C14.8284 4.14635 14.4504 3.76842 14.4504 3.30221Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 9.00002C14.4504 8.53382 14.8284 8.15588 15.2946 8.15588H28.4933C28.9595 8.15588 29.3374 8.53382 29.3374 9.00002C29.3374 9.46623 28.9595 9.84417 28.4933 9.84417H15.2946C14.8284 9.84417 14.4504 9.46623 14.4504 9.00002Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M14.4504 14.6978C14.4504 14.2316 14.8284 13.8537 15.2946 13.8537H28.4933C28.9595 13.8537 29.3374 14.2316 29.3374 14.6978C29.3374 15.164 28.9595 15.542 28.4933 15.542H15.2946C14.8284 15.542 14.4504 15.164 14.4504 14.6978Z" fill="currentColor"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M1.94549 6.87377C2.27514 6.54411 2.80962 6.54411 3.13928 6.87377L6.23458 9.96907L9.32988 6.87377C9.65954 6.54411 10.194 6.54411 10.5237 6.87377C10.8533 7.20343 10.8533 7.73791 10.5237 8.06756L6.23458 12.3567L1.94549 8.06756C1.61583 7.73791 1.61583 7.20343 1.94549 6.87377Z" fill="currentColor"></path></svg> </button> </div></li> <li><hr class="h-5 w-0.5 border-none bg-gray-100 dark:bg-gray-800"></li> <li><a class="block cursor-pointer px-2 py-0.5 hover:text-gray-500 dark:hover:text-gray-400" href="/login">Log In</a></li> <li><a class="btn ml-2" href="/join">Sign Up</a></li></ul></nav></div></header></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="GoogleAnalyticsTracker"></div> <div class="SVELTE_HYDRATER contents" data-props="{}" data-target="SSOBanner"></div> <main class="flex flex-1 flex-col "><div class="relative lg:flex"><div class="sticky top-0 z-20 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapters&quot;:[{&quot;title&quot;:&quot;Get started&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;🤗 Transformers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;index&quot;,&quot;url&quot;:&quot;/docs/transformers/index&quot;},{&quot;title&quot;:&quot;Quick tour&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;quicktour&quot;,&quot;url&quot;:&quot;/docs/transformers/quicktour&quot;},{&quot;title&quot;:&quot;Installation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;installation&quot;,&quot;url&quot;:&quot;/docs/transformers/installation&quot;}]},{&quot;title&quot;:&quot;Tutorials&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run inference with pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_tutorial&quot;},{&quot;title&quot;:&quot;Write portable code with AutoClass&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;autoclass_tutorial&quot;,&quot;url&quot;:&quot;/docs/transformers/autoclass_tutorial&quot;},{&quot;title&quot;:&quot;Preprocess data&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;preprocessing&quot;,&quot;url&quot;:&quot;/docs/transformers/preprocessing&quot;},{&quot;title&quot;:&quot;Fine-tune a pretrained model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;training&quot;,&quot;url&quot;:&quot;/docs/transformers/training&quot;},{&quot;title&quot;:&quot;Train with a script&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;run_scripts&quot;,&quot;url&quot;:&quot;/docs/transformers/run_scripts&quot;},{&quot;title&quot;:&quot;Set up distributed training with 🤗 Accelerate&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;accelerate&quot;,&quot;url&quot;:&quot;/docs/transformers/accelerate&quot;},{&quot;title&quot;:&quot;Share your model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_sharing&quot;,&quot;url&quot;:&quot;/docs/transformers/model_sharing&quot;},{&quot;title&quot;:&quot;Agents&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;transformers_agents&quot;,&quot;url&quot;:&quot;/docs/transformers/transformers_agents&quot;}]},{&quot;title&quot;:&quot;Task Guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Natural Language Processing&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text classification&quot;,&quot;id&quot;:&quot;tasks/sequence_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/sequence_classification&quot;},{&quot;title&quot;:&quot;Token classification&quot;,&quot;id&quot;:&quot;tasks/token_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/token_classification&quot;},{&quot;title&quot;:&quot;Question answering&quot;,&quot;id&quot;:&quot;tasks/question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/question_answering&quot;},{&quot;title&quot;:&quot;Causal language modeling&quot;,&quot;id&quot;:&quot;tasks/language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/language_modeling&quot;},{&quot;title&quot;:&quot;Masked language modeling&quot;,&quot;id&quot;:&quot;tasks/masked_language_modeling&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/masked_language_modeling&quot;},{&quot;title&quot;:&quot;Translation&quot;,&quot;id&quot;:&quot;tasks/translation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/translation&quot;},{&quot;title&quot;:&quot;Summarization&quot;,&quot;id&quot;:&quot;tasks/summarization&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/summarization&quot;},{&quot;title&quot;:&quot;Multiple choice&quot;,&quot;id&quot;:&quot;tasks/multiple_choice&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/multiple_choice&quot;}]},{&quot;title&quot;:&quot;Audio&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio classification&quot;,&quot;id&quot;:&quot;tasks/audio_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/audio_classification&quot;},{&quot;title&quot;:&quot;Automatic speech recognition&quot;,&quot;id&quot;:&quot;tasks/asr&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/asr&quot;}]},{&quot;title&quot;:&quot;Computer Vision&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image classification&quot;,&quot;id&quot;:&quot;tasks/image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_classification&quot;},{&quot;title&quot;:&quot;Semantic segmentation&quot;,&quot;id&quot;:&quot;tasks/semantic_segmentation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/semantic_segmentation&quot;},{&quot;title&quot;:&quot;Video classification&quot;,&quot;id&quot;:&quot;tasks/video_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/video_classification&quot;},{&quot;title&quot;:&quot;Object detection&quot;,&quot;id&quot;:&quot;tasks/object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot object detection&quot;,&quot;id&quot;:&quot;tasks/zero_shot_object_detection&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_object_detection&quot;},{&quot;title&quot;:&quot;Zero-shot image classification&quot;,&quot;id&quot;:&quot;tasks/zero_shot_image_classification&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/zero_shot_image_classification&quot;},{&quot;title&quot;:&quot;Depth estimation&quot;,&quot;id&quot;:&quot;tasks/monocular_depth_estimation&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/monocular_depth_estimation&quot;}]},{&quot;title&quot;:&quot;Multimodal&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image captioning&quot;,&quot;id&quot;:&quot;tasks/image_captioning&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/image_captioning&quot;},{&quot;title&quot;:&quot;Document Question Answering&quot;,&quot;id&quot;:&quot;tasks/document_question_answering&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/document_question_answering&quot;},{&quot;title&quot;:&quot;Text to speech&quot;,&quot;id&quot;:&quot;tasks/text-to-speech&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks/text-to-speech&quot;}]}]},{&quot;title&quot;:&quot;Developer guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Use fast tokenizers from 🤗 Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;fast_tokenizers&quot;,&quot;url&quot;:&quot;/docs/transformers/fast_tokenizers&quot;},{&quot;title&quot;:&quot;Run inference with multilingual models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;multilingual&quot;,&quot;url&quot;:&quot;/docs/transformers/multilingual&quot;},{&quot;title&quot;:&quot;Customize text generation strategy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;generation_strategies&quot;,&quot;url&quot;:&quot;/docs/transformers/generation_strategies&quot;},{&quot;title&quot;:&quot;Use model-specific APIs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;create_a_model&quot;,&quot;url&quot;:&quot;/docs/transformers/create_a_model&quot;},{&quot;title&quot;:&quot;Share a custom model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_models&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_models&quot;},{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sagemaker&quot;,&quot;url&quot;:&quot;/docs/transformers/sagemaker&quot;},{&quot;title&quot;:&quot;Export to ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;serialization&quot;,&quot;url&quot;:&quot;/docs/transformers/serialization&quot;},{&quot;title&quot;:&quot;Export to TFLite&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tflite&quot;,&quot;url&quot;:&quot;/docs/transformers/tflite&quot;},{&quot;title&quot;:&quot;Export to TorchScript&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;torchscript&quot;,&quot;url&quot;:&quot;/docs/transformers/torchscript&quot;},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;/docs/transformers/benchmarks&quot;},{&quot;title&quot;:&quot;Notebooks with examples&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;notebooks&quot;,&quot;url&quot;:&quot;/docs/transformers/notebooks&quot;},{&quot;title&quot;:&quot;Community resources&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;community&quot;,&quot;url&quot;:&quot;/docs/transformers/community&quot;},{&quot;title&quot;:&quot;Custom Tools and Prompts&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;custom_tools&quot;,&quot;url&quot;:&quot;/docs/transformers/custom_tools&quot;},{&quot;title&quot;:&quot;Troubleshoot&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;troubleshooting&quot;,&quot;url&quot;:&quot;/docs/transformers/troubleshooting&quot;}]},{&quot;title&quot;:&quot;Performance and scalability&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overview&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;performance&quot;,&quot;url&quot;:&quot;/docs/transformers/performance&quot;},{&quot;title&quot;:&quot;Training on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_one&quot;},{&quot;title&quot;:&quot;Training on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_gpu_many&quot;},{&quot;title&quot;:&quot;Training on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu&quot;},{&quot;title&quot;:&quot;Training on many CPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_cpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_cpu_many&quot;},{&quot;title&quot;:&quot;Training on TPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu&quot;},{&quot;title&quot;:&quot;Training on TPU with TensorFlow&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_tpu_tf&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_tpu_tf&quot;},{&quot;title&quot;:&quot;Training on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_train_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_train_special&quot;},{&quot;title&quot;:&quot;Inference on CPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_cpu&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_cpu&quot;},{&quot;title&quot;:&quot;Inference on one GPU&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_one&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_one&quot;},{&quot;title&quot;:&quot;Inference on many GPUs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_gpu_many&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_gpu_many&quot;},{&quot;title&quot;:&quot;Inference on Specialized Hardware&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_infer_special&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_infer_special&quot;},{&quot;title&quot;:&quot;Custom hardware for training&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perf_hardware&quot;,&quot;url&quot;:&quot;/docs/transformers/perf_hardware&quot;},{&quot;title&quot;:&quot;Instantiating a big model&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;big_models&quot;,&quot;url&quot;:&quot;/docs/transformers/big_models&quot;},{&quot;title&quot;:&quot;Debugging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;debugging&quot;,&quot;url&quot;:&quot;/docs/transformers/debugging&quot;},{&quot;title&quot;:&quot;Hyperparameter Search using Trainer API&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;hpo_train&quot;,&quot;url&quot;:&quot;/docs/transformers/hpo_train&quot;},{&quot;title&quot;:&quot;XLA Integration for TensorFlow Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tf_xla&quot;,&quot;url&quot;:&quot;/docs/transformers/tf_xla&quot;}]},{&quot;title&quot;:&quot;Contribute&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to contribute to transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;contributing&quot;,&quot;url&quot;:&quot;/docs/transformers/contributing&quot;},{&quot;title&quot;:&quot;How to add a model to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_model&quot;},{&quot;title&quot;:&quot;How to convert a 🤗 Transformers model to TensorFlow?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_tensorflow_model&quot;,&quot;url&quot;:&quot;/docs/transformers/add_tensorflow_model&quot;},{&quot;title&quot;:&quot;How to add a pipeline to 🤗 Transformers?&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;add_new_pipeline&quot;,&quot;url&quot;:&quot;/docs/transformers/add_new_pipeline&quot;},{&quot;title&quot;:&quot;Testing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;testing&quot;,&quot;url&quot;:&quot;/docs/transformers/testing&quot;},{&quot;title&quot;:&quot;Checks on a Pull Request&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pr_checks&quot;,&quot;url&quot;:&quot;/docs/transformers/pr_checks&quot;}]},{&quot;title&quot;:&quot;Conceptual guides&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Philosophy&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;philosophy&quot;,&quot;url&quot;:&quot;/docs/transformers/philosophy&quot;},{&quot;title&quot;:&quot;Glossary&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;glossary&quot;,&quot;url&quot;:&quot;/docs/transformers/glossary&quot;},{&quot;title&quot;:&quot;What 🤗 Transformers can do&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;task_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/task_summary&quot;},{&quot;title&quot;:&quot;How 🤗 Transformers solve tasks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tasks_explained&quot;,&quot;url&quot;:&quot;/docs/transformers/tasks_explained&quot;},{&quot;title&quot;:&quot;The Transformer model family&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/model_summary&quot;},{&quot;title&quot;:&quot;Summary of the tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;tokenizer_summary&quot;,&quot;url&quot;:&quot;/docs/transformers/tokenizer_summary&quot;},{&quot;title&quot;:&quot;Attention mechanisms&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;attention&quot;,&quot;url&quot;:&quot;/docs/transformers/attention&quot;},{&quot;title&quot;:&quot;Padding and truncation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pad_truncation&quot;,&quot;url&quot;:&quot;/docs/transformers/pad_truncation&quot;},{&quot;title&quot;:&quot;BERTology&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;bertology&quot;,&quot;url&quot;:&quot;/docs/transformers/bertology&quot;},{&quot;title&quot;:&quot;Perplexity of fixed-length models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;perplexity&quot;,&quot;url&quot;:&quot;/docs/transformers/perplexity&quot;},{&quot;title&quot;:&quot;Pipelines for webserver inference&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;pipeline_webserver&quot;,&quot;url&quot;:&quot;/docs/transformers/pipeline_webserver&quot;}]},{&quot;title&quot;:&quot;API&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Main Classes&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Agents and Tools&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/agent&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/agent&quot;},{&quot;title&quot;:&quot;Auto Classes&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;model_doc/auto&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/auto&quot;},{&quot;title&quot;:&quot;Callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/callback&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/callback&quot;},{&quot;title&quot;:&quot;Configuration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/configuration&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/configuration&quot;},{&quot;title&quot;:&quot;Data Collator&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/data_collator&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/data_collator&quot;},{&quot;title&quot;:&quot;Keras callbacks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/keras_callbacks&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/keras_callbacks&quot;},{&quot;title&quot;:&quot;Logging&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/logging&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/logging&quot;},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/model&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/model&quot;},{&quot;title&quot;:&quot;Text Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/text_generation&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/text_generation&quot;},{&quot;title&quot;:&quot;ONNX&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/onnx&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/onnx&quot;},{&quot;title&quot;:&quot;Optimization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/optimizer_schedules&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/optimizer_schedules&quot;},{&quot;title&quot;:&quot;Model outputs&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/output&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/output&quot;},{&quot;title&quot;:&quot;Pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/pipelines&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/pipelines&quot;},{&quot;title&quot;:&quot;Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/processors&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/processors&quot;},{&quot;title&quot;:&quot;Quantization&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/quantization&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/quantization&quot;},{&quot;title&quot;:&quot;Tokenizer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/tokenizer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/tokenizer&quot;},{&quot;title&quot;:&quot;Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/trainer&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/trainer&quot;},{&quot;title&quot;:&quot;DeepSpeed Integration&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/deepspeed&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/deepspeed&quot;},{&quot;title&quot;:&quot;Feature Extractor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/feature_extractor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/feature_extractor&quot;},{&quot;title&quot;:&quot;Image Processor&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;main_classes/image_processor&quot;,&quot;url&quot;:&quot;/docs/transformers/main_classes/image_processor&quot;}]},{&quot;title&quot;:&quot;Models&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Text models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALBERT&quot;,&quot;id&quot;:&quot;model_doc/albert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/albert&quot;},{&quot;title&quot;:&quot;BART&quot;,&quot;id&quot;:&quot;model_doc/bart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bart&quot;},{&quot;title&quot;:&quot;BARThez&quot;,&quot;id&quot;:&quot;model_doc/barthez&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/barthez&quot;},{&quot;title&quot;:&quot;BARTpho&quot;,&quot;id&quot;:&quot;model_doc/bartpho&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bartpho&quot;},{&quot;title&quot;:&quot;BERT&quot;,&quot;id&quot;:&quot;model_doc/bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert&quot;},{&quot;title&quot;:&quot;BertGeneration&quot;,&quot;id&quot;:&quot;model_doc/bert-generation&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-generation&quot;},{&quot;title&quot;:&quot;BertJapanese&quot;,&quot;id&quot;:&quot;model_doc/bert-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bert-japanese&quot;},{&quot;title&quot;:&quot;Bertweet&quot;,&quot;id&quot;:&quot;model_doc/bertweet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bertweet&quot;},{&quot;title&quot;:&quot;BigBird&quot;,&quot;id&quot;:&quot;model_doc/big_bird&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/big_bird&quot;},{&quot;title&quot;:&quot;BigBirdPegasus&quot;,&quot;id&quot;:&quot;model_doc/bigbird_pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bigbird_pegasus&quot;},{&quot;title&quot;:&quot;BioGpt&quot;,&quot;id&quot;:&quot;model_doc/biogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/biogpt&quot;},{&quot;title&quot;:&quot;Blenderbot&quot;,&quot;id&quot;:&quot;model_doc/blenderbot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot&quot;},{&quot;title&quot;:&quot;Blenderbot Small&quot;,&quot;id&quot;:&quot;model_doc/blenderbot-small&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blenderbot-small&quot;},{&quot;title&quot;:&quot;BLOOM&quot;,&quot;id&quot;:&quot;model_doc/bloom&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bloom&quot;},{&quot;title&quot;:&quot;BORT&quot;,&quot;id&quot;:&quot;model_doc/bort&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bort&quot;},{&quot;title&quot;:&quot;ByT5&quot;,&quot;id&quot;:&quot;model_doc/byt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/byt5&quot;},{&quot;title&quot;:&quot;CamemBERT&quot;,&quot;id&quot;:&quot;model_doc/camembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/camembert&quot;},{&quot;title&quot;:&quot;CANINE&quot;,&quot;id&quot;:&quot;model_doc/canine&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/canine&quot;},{&quot;title&quot;:&quot;CodeGen&quot;,&quot;id&quot;:&quot;model_doc/codegen&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/codegen&quot;},{&quot;title&quot;:&quot;ConvBERT&quot;,&quot;id&quot;:&quot;model_doc/convbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convbert&quot;},{&quot;title&quot;:&quot;CPM&quot;,&quot;id&quot;:&quot;model_doc/cpm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpm&quot;},{&quot;title&quot;:&quot;CPMANT&quot;,&quot;id&quot;:&quot;model_doc/cpmant&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cpmant&quot;},{&quot;title&quot;:&quot;CTRL&quot;,&quot;id&quot;:&quot;model_doc/ctrl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ctrl&quot;},{&quot;title&quot;:&quot;DeBERTa&quot;,&quot;id&quot;:&quot;model_doc/deberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta&quot;},{&quot;title&quot;:&quot;DeBERTa-v2&quot;,&quot;id&quot;:&quot;model_doc/deberta-v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deberta-v2&quot;},{&quot;title&quot;:&quot;DialoGPT&quot;,&quot;id&quot;:&quot;model_doc/dialogpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dialogpt&quot;},{&quot;title&quot;:&quot;DistilBERT&quot;,&quot;id&quot;:&quot;model_doc/distilbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/distilbert&quot;},{&quot;title&quot;:&quot;DPR&quot;,&quot;id&quot;:&quot;model_doc/dpr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpr&quot;},{&quot;title&quot;:&quot;ELECTRA&quot;,&quot;id&quot;:&quot;model_doc/electra&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/electra&quot;},{&quot;title&quot;:&quot;Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/encoder-decoder&quot;},{&quot;title&quot;:&quot;ERNIE&quot;,&quot;id&quot;:&quot;model_doc/ernie&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie&quot;},{&quot;title&quot;:&quot;ErnieM&quot;,&quot;id&quot;:&quot;model_doc/ernie_m&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ernie_m&quot;},{&quot;title&quot;:&quot;ESM&quot;,&quot;id&quot;:&quot;model_doc/esm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/esm&quot;},{&quot;title&quot;:&quot;FLAN-T5&quot;,&quot;id&quot;:&quot;model_doc/flan-t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-t5&quot;},{&quot;title&quot;:&quot;FLAN-UL2&quot;,&quot;id&quot;:&quot;model_doc/flan-ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flan-ul2&quot;},{&quot;title&quot;:&quot;FlauBERT&quot;,&quot;id&quot;:&quot;model_doc/flaubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flaubert&quot;},{&quot;title&quot;:&quot;FNet&quot;,&quot;id&quot;:&quot;model_doc/fnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fnet&quot;},{&quot;title&quot;:&quot;FSMT&quot;,&quot;id&quot;:&quot;model_doc/fsmt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/fsmt&quot;},{&quot;title&quot;:&quot;Funnel Transformer&quot;,&quot;id&quot;:&quot;model_doc/funnel&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/funnel&quot;},{&quot;title&quot;:&quot;GPT&quot;,&quot;id&quot;:&quot;model_doc/openai-gpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/openai-gpt&quot;},{&quot;title&quot;:&quot;GPT Neo&quot;,&quot;id&quot;:&quot;model_doc/gpt_neo&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neo&quot;},{&quot;title&quot;:&quot;GPT NeoX&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox&quot;},{&quot;title&quot;:&quot;GPT NeoX Japanese&quot;,&quot;id&quot;:&quot;model_doc/gpt_neox_japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_neox_japanese&quot;},{&quot;title&quot;:&quot;GPT-J&quot;,&quot;id&quot;:&quot;model_doc/gptj&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptj&quot;},{&quot;title&quot;:&quot;GPT2&quot;,&quot;id&quot;:&quot;model_doc/gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt2&quot;},{&quot;title&quot;:&quot;GPTBigCode&quot;,&quot;id&quot;:&quot;model_doc/gpt_bigcode&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt_bigcode&quot;},{&quot;title&quot;:&quot;GPTSAN Japanese&quot;,&quot;id&quot;:&quot;model_doc/gptsan-japanese&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gptsan-japanese&quot;},{&quot;title&quot;:&quot;GPTSw3&quot;,&quot;id&quot;:&quot;model_doc/gpt-sw3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/gpt-sw3&quot;},{&quot;title&quot;:&quot;HerBERT&quot;,&quot;id&quot;:&quot;model_doc/herbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/herbert&quot;},{&quot;title&quot;:&quot;I-BERT&quot;,&quot;id&quot;:&quot;model_doc/ibert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ibert&quot;},{&quot;title&quot;:&quot;Jukebox&quot;,&quot;id&quot;:&quot;model_doc/jukebox&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/jukebox&quot;},{&quot;title&quot;:&quot;LED&quot;,&quot;id&quot;:&quot;model_doc/led&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/led&quot;},{&quot;title&quot;:&quot;LLaMA&quot;,&quot;id&quot;:&quot;model_doc/llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/llama&quot;},{&quot;title&quot;:&quot;Longformer&quot;,&quot;id&quot;:&quot;model_doc/longformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longformer&quot;},{&quot;title&quot;:&quot;LongT5&quot;,&quot;id&quot;:&quot;model_doc/longt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/longt5&quot;},{&quot;title&quot;:&quot;LUKE&quot;,&quot;id&quot;:&quot;model_doc/luke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/luke&quot;},{&quot;title&quot;:&quot;M2M100&quot;,&quot;id&quot;:&quot;model_doc/m2m_100&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/m2m_100&quot;},{&quot;title&quot;:&quot;MarianMT&quot;,&quot;id&quot;:&quot;model_doc/marian&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/marian&quot;},{&quot;title&quot;:&quot;MarkupLM&quot;,&quot;id&quot;:&quot;model_doc/markuplm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/markuplm&quot;},{&quot;title&quot;:&quot;MBart and MBart-50&quot;,&quot;id&quot;:&quot;model_doc/mbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mbart&quot;},{&quot;title&quot;:&quot;MEGA&quot;,&quot;id&quot;:&quot;model_doc/mega&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mega&quot;},{&quot;title&quot;:&quot;MegatronBERT&quot;,&quot;id&quot;:&quot;model_doc/megatron-bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron-bert&quot;},{&quot;title&quot;:&quot;MegatronGPT2&quot;,&quot;id&quot;:&quot;model_doc/megatron_gpt2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/megatron_gpt2&quot;},{&quot;title&quot;:&quot;mLUKE&quot;,&quot;id&quot;:&quot;model_doc/mluke&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mluke&quot;},{&quot;title&quot;:&quot;MobileBERT&quot;,&quot;id&quot;:&quot;model_doc/mobilebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilebert&quot;},{&quot;title&quot;:&quot;MPNet&quot;,&quot;id&quot;:&quot;model_doc/mpnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mpnet&quot;},{&quot;title&quot;:&quot;MT5&quot;,&quot;id&quot;:&quot;model_doc/mt5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mt5&quot;},{&quot;title&quot;:&quot;MVP&quot;,&quot;id&quot;:&quot;model_doc/mvp&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mvp&quot;},{&quot;title&quot;:&quot;NEZHA&quot;,&quot;id&quot;:&quot;model_doc/nezha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nezha&quot;},{&quot;title&quot;:&quot;NLLB&quot;,&quot;id&quot;:&quot;model_doc/nllb&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb&quot;},{&quot;title&quot;:&quot;NLLB-MoE&quot;,&quot;id&quot;:&quot;model_doc/nllb-moe&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nllb-moe&quot;},{&quot;title&quot;:&quot;Nyströmformer&quot;,&quot;id&quot;:&quot;model_doc/nystromformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nystromformer&quot;},{&quot;title&quot;:&quot;Open-Llama&quot;,&quot;id&quot;:&quot;model_doc/open-llama&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/open-llama&quot;},{&quot;title&quot;:&quot;OPT&quot;,&quot;id&quot;:&quot;model_doc/opt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/opt&quot;},{&quot;title&quot;:&quot;Pegasus&quot;,&quot;id&quot;:&quot;model_doc/pegasus&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus&quot;},{&quot;title&quot;:&quot;PEGASUS-X&quot;,&quot;id&quot;:&quot;model_doc/pegasus_x&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pegasus_x&quot;},{&quot;title&quot;:&quot;PhoBERT&quot;,&quot;id&quot;:&quot;model_doc/phobert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/phobert&quot;},{&quot;title&quot;:&quot;PLBart&quot;,&quot;id&quot;:&quot;model_doc/plbart&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/plbart&quot;},{&quot;title&quot;:&quot;ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/prophetnet&quot;},{&quot;title&quot;:&quot;QDQBert&quot;,&quot;id&quot;:&quot;model_doc/qdqbert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/qdqbert&quot;},{&quot;title&quot;:&quot;RAG&quot;,&quot;id&quot;:&quot;model_doc/rag&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rag&quot;},{&quot;title&quot;:&quot;REALM&quot;,&quot;id&quot;:&quot;model_doc/realm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/realm&quot;},{&quot;title&quot;:&quot;Reformer&quot;,&quot;id&quot;:&quot;model_doc/reformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/reformer&quot;},{&quot;title&quot;:&quot;RemBERT&quot;,&quot;id&quot;:&quot;model_doc/rembert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rembert&quot;},{&quot;title&quot;:&quot;RetriBERT&quot;,&quot;id&quot;:&quot;model_doc/retribert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/retribert&quot;},{&quot;title&quot;:&quot;RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta&quot;},{&quot;title&quot;:&quot;RoBERTa-PreLayerNorm&quot;,&quot;id&quot;:&quot;model_doc/roberta-prelayernorm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roberta-prelayernorm&quot;},{&quot;title&quot;:&quot;RoCBert&quot;,&quot;id&quot;:&quot;model_doc/roc_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roc_bert&quot;},{&quot;title&quot;:&quot;RoFormer&quot;,&quot;id&quot;:&quot;model_doc/roformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/roformer&quot;},{&quot;title&quot;:&quot;RWKV&quot;,&quot;id&quot;:&quot;model_doc/rwkv&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/rwkv&quot;},{&quot;title&quot;:&quot;Splinter&quot;,&quot;id&quot;:&quot;model_doc/splinter&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/splinter&quot;},{&quot;title&quot;:&quot;SqueezeBERT&quot;,&quot;id&quot;:&quot;model_doc/squeezebert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/squeezebert&quot;},{&quot;title&quot;:&quot;SwitchTransformers&quot;,&quot;id&quot;:&quot;model_doc/switch_transformers&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/switch_transformers&quot;},{&quot;title&quot;:&quot;T5&quot;,&quot;id&quot;:&quot;model_doc/t5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5&quot;},{&quot;title&quot;:&quot;T5v1.1&quot;,&quot;id&quot;:&quot;model_doc/t5v1.1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/t5v1.1&quot;},{&quot;title&quot;:&quot;TAPEX&quot;,&quot;id&quot;:&quot;model_doc/tapex&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapex&quot;},{&quot;title&quot;:&quot;Transformer XL&quot;,&quot;id&quot;:&quot;model_doc/transfo-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/transfo-xl&quot;},{&quot;title&quot;:&quot;UL2&quot;,&quot;id&quot;:&quot;model_doc/ul2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/ul2&quot;},{&quot;title&quot;:&quot;X-MOD&quot;,&quot;id&quot;:&quot;model_doc/xmod&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xmod&quot;},{&quot;title&quot;:&quot;XGLM&quot;,&quot;id&quot;:&quot;model_doc/xglm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xglm&quot;},{&quot;title&quot;:&quot;XLM&quot;,&quot;id&quot;:&quot;model_doc/xlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm&quot;},{&quot;title&quot;:&quot;XLM-ProphetNet&quot;,&quot;id&quot;:&quot;model_doc/xlm-prophetnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-prophetnet&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta&quot;},{&quot;title&quot;:&quot;XLM-RoBERTa-XL&quot;,&quot;id&quot;:&quot;model_doc/xlm-roberta-xl&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-roberta-xl&quot;},{&quot;title&quot;:&quot;XLM-V&quot;,&quot;id&quot;:&quot;model_doc/xlm-v&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlm-v&quot;},{&quot;title&quot;:&quot;XLNet&quot;,&quot;id&quot;:&quot;model_doc/xlnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlnet&quot;},{&quot;title&quot;:&quot;YOSO&quot;,&quot;id&quot;:&quot;model_doc/yoso&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yoso&quot;}]},{&quot;title&quot;:&quot;Vision models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;BEiT&quot;,&quot;id&quot;:&quot;model_doc/beit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/beit&quot;},{&quot;title&quot;:&quot;BiT&quot;,&quot;id&quot;:&quot;model_doc/bit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bit&quot;},{&quot;title&quot;:&quot;Conditional DETR&quot;,&quot;id&quot;:&quot;model_doc/conditional_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/conditional_detr&quot;},{&quot;title&quot;:&quot;ConvNeXT&quot;,&quot;id&quot;:&quot;model_doc/convnext&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnext&quot;},{&quot;title&quot;:&quot;ConvNeXTV2&quot;,&quot;id&quot;:&quot;model_doc/convnextv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/convnextv2&quot;},{&quot;title&quot;:&quot;CvT&quot;,&quot;id&quot;:&quot;model_doc/cvt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/cvt&quot;},{&quot;title&quot;:&quot;Deformable DETR&quot;,&quot;id&quot;:&quot;model_doc/deformable_detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deformable_detr&quot;},{&quot;title&quot;:&quot;DeiT&quot;,&quot;id&quot;:&quot;model_doc/deit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deit&quot;},{&quot;title&quot;:&quot;DETA&quot;,&quot;id&quot;:&quot;model_doc/deta&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deta&quot;},{&quot;title&quot;:&quot;DETR&quot;,&quot;id&quot;:&quot;model_doc/detr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/detr&quot;},{&quot;title&quot;:&quot;DiNAT&quot;,&quot;id&quot;:&quot;model_doc/dinat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dinat&quot;},{&quot;title&quot;:&quot;DiT&quot;,&quot;id&quot;:&quot;model_doc/dit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dit&quot;},{&quot;title&quot;:&quot;DPT&quot;,&quot;id&quot;:&quot;model_doc/dpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/dpt&quot;},{&quot;title&quot;:&quot;EfficientFormer&quot;,&quot;id&quot;:&quot;model_doc/efficientformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientformer&quot;},{&quot;title&quot;:&quot;EfficientNet&quot;,&quot;id&quot;:&quot;model_doc/efficientnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/efficientnet&quot;},{&quot;title&quot;:&quot;FocalNet&quot;,&quot;id&quot;:&quot;model_doc/focalnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/focalnet&quot;},{&quot;title&quot;:&quot;GLPN&quot;,&quot;id&quot;:&quot;model_doc/glpn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/glpn&quot;},{&quot;title&quot;:&quot;ImageGPT&quot;,&quot;id&quot;:&quot;model_doc/imagegpt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/imagegpt&quot;},{&quot;title&quot;:&quot;LeViT&quot;,&quot;id&quot;:&quot;model_doc/levit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/levit&quot;},{&quot;title&quot;:&quot;Mask2Former&quot;,&quot;id&quot;:&quot;model_doc/mask2former&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mask2former&quot;},{&quot;title&quot;:&quot;MaskFormer&quot;,&quot;id&quot;:&quot;model_doc/maskformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/maskformer&quot;},{&quot;title&quot;:&quot;MobileNetV1&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v1&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v1&quot;},{&quot;title&quot;:&quot;MobileNetV2&quot;,&quot;id&quot;:&quot;model_doc/mobilenet_v2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilenet_v2&quot;},{&quot;title&quot;:&quot;MobileViT&quot;,&quot;id&quot;:&quot;model_doc/mobilevit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevit&quot;},{&quot;title&quot;:&quot;MobileViTV2&quot;,&quot;id&quot;:&quot;model_doc/mobilevitv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mobilevitv2&quot;},{&quot;title&quot;:&quot;NAT&quot;,&quot;id&quot;:&quot;model_doc/nat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/nat&quot;},{&quot;title&quot;:&quot;PoolFormer&quot;,&quot;id&quot;:&quot;model_doc/poolformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/poolformer&quot;},{&quot;title&quot;:&quot;RegNet&quot;,&quot;id&quot;:&quot;model_doc/regnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/regnet&quot;},{&quot;title&quot;:&quot;ResNet&quot;,&quot;id&quot;:&quot;model_doc/resnet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/resnet&quot;},{&quot;title&quot;:&quot;SegFormer&quot;,&quot;id&quot;:&quot;model_doc/segformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/segformer&quot;},{&quot;title&quot;:&quot;SwiftFormer&quot;,&quot;id&quot;:&quot;model_doc/swiftformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swiftformer&quot;},{&quot;title&quot;:&quot;Swin Transformer&quot;,&quot;id&quot;:&quot;model_doc/swin&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin&quot;},{&quot;title&quot;:&quot;Swin Transformer V2&quot;,&quot;id&quot;:&quot;model_doc/swinv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swinv2&quot;},{&quot;title&quot;:&quot;Swin2SR&quot;,&quot;id&quot;:&quot;model_doc/swin2sr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/swin2sr&quot;},{&quot;title&quot;:&quot;Table Transformer&quot;,&quot;id&quot;:&quot;model_doc/table-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/table-transformer&quot;},{&quot;title&quot;:&quot;TimeSformer&quot;,&quot;id&quot;:&quot;model_doc/timesformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/timesformer&quot;},{&quot;title&quot;:&quot;UperNet&quot;,&quot;id&quot;:&quot;model_doc/upernet&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/upernet&quot;},{&quot;title&quot;:&quot;VAN&quot;,&quot;id&quot;:&quot;model_doc/van&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/van&quot;},{&quot;title&quot;:&quot;VideoMAE&quot;,&quot;id&quot;:&quot;model_doc/videomae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/videomae&quot;},{&quot;title&quot;:&quot;Vision Transformer (ViT)&quot;,&quot;id&quot;:&quot;model_doc/vit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit&quot;},{&quot;title&quot;:&quot;ViT Hybrid&quot;,&quot;id&quot;:&quot;model_doc/vit_hybrid&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_hybrid&quot;},{&quot;title&quot;:&quot;ViTMAE&quot;,&quot;id&quot;:&quot;model_doc/vit_mae&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_mae&quot;},{&quot;title&quot;:&quot;ViTMSN&quot;,&quot;id&quot;:&quot;model_doc/vit_msn&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vit_msn&quot;},{&quot;title&quot;:&quot;YOLOS&quot;,&quot;id&quot;:&quot;model_doc/yolos&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/yolos&quot;}]},{&quot;title&quot;:&quot;Audio models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Audio Spectrogram Transformer&quot;,&quot;id&quot;:&quot;model_doc/audio-spectrogram-transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/audio-spectrogram-transformer&quot;},{&quot;title&quot;:&quot;CLAP&quot;,&quot;id&quot;:&quot;model_doc/clap&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clap&quot;},{&quot;title&quot;:&quot;Hubert&quot;,&quot;id&quot;:&quot;model_doc/hubert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/hubert&quot;},{&quot;title&quot;:&quot;MCTCT&quot;,&quot;id&quot;:&quot;model_doc/mctct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mctct&quot;},{&quot;title&quot;:&quot;MMS&quot;,&quot;id&quot;:&quot;model_doc/mms&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mms&quot;},{&quot;title&quot;:&quot;SEW&quot;,&quot;id&quot;:&quot;model_doc/sew&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew&quot;},{&quot;title&quot;:&quot;SEW-D&quot;,&quot;id&quot;:&quot;model_doc/sew-d&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sew-d&quot;},{&quot;title&quot;:&quot;Speech2Text&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text&quot;},{&quot;title&quot;:&quot;Speech2Text2&quot;,&quot;id&quot;:&quot;model_doc/speech_to_text_2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech_to_text_2&quot;},{&quot;title&quot;:&quot;SpeechT5&quot;,&quot;id&quot;:&quot;model_doc/speecht5&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speecht5&quot;},{&quot;title&quot;:&quot;UniSpeech&quot;,&quot;id&quot;:&quot;model_doc/unispeech&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech&quot;},{&quot;title&quot;:&quot;UniSpeech-SAT&quot;,&quot;id&quot;:&quot;model_doc/unispeech-sat&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/unispeech-sat&quot;},{&quot;title&quot;:&quot;Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2&quot;},{&quot;title&quot;:&quot;Wav2Vec2-Conformer&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2-conformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2-conformer&quot;},{&quot;title&quot;:&quot;Wav2Vec2Phoneme&quot;,&quot;id&quot;:&quot;model_doc/wav2vec2_phoneme&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wav2vec2_phoneme&quot;},{&quot;title&quot;:&quot;WavLM&quot;,&quot;id&quot;:&quot;model_doc/wavlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/wavlm&quot;},{&quot;title&quot;:&quot;Whisper&quot;,&quot;id&quot;:&quot;model_doc/whisper&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/whisper&quot;},{&quot;title&quot;:&quot;XLS-R&quot;,&quot;id&quot;:&quot;model_doc/xls_r&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xls_r&quot;},{&quot;title&quot;:&quot;XLSR-Wav2Vec2&quot;,&quot;id&quot;:&quot;model_doc/xlsr_wav2vec2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xlsr_wav2vec2&quot;}]},{&quot;title&quot;:&quot;Multimodal models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;ALIGN&quot;,&quot;id&quot;:&quot;model_doc/align&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/align&quot;},{&quot;title&quot;:&quot;AltCLIP&quot;,&quot;id&quot;:&quot;model_doc/altclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/altclip&quot;},{&quot;title&quot;:&quot;BLIP&quot;,&quot;id&quot;:&quot;model_doc/blip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip&quot;},{&quot;title&quot;:&quot;BLIP-2&quot;,&quot;id&quot;:&quot;model_doc/blip-2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/blip-2&quot;},{&quot;title&quot;:&quot;BridgeTower&quot;,&quot;id&quot;:&quot;model_doc/bridgetower&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/bridgetower&quot;},{&quot;title&quot;:&quot;Chinese-CLIP&quot;,&quot;id&quot;:&quot;model_doc/chinese_clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/chinese_clip&quot;},{&quot;title&quot;:&quot;CLIP&quot;,&quot;id&quot;:&quot;model_doc/clip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clip&quot;},{&quot;title&quot;:&quot;CLIPSeg&quot;,&quot;id&quot;:&quot;model_doc/clipseg&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/clipseg&quot;},{&quot;title&quot;:&quot;Data2Vec&quot;,&quot;id&quot;:&quot;model_doc/data2vec&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/data2vec&quot;},{&quot;title&quot;:&quot;DePlot&quot;,&quot;id&quot;:&quot;model_doc/deplot&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/deplot&quot;},{&quot;title&quot;:&quot;Donut&quot;,&quot;id&quot;:&quot;model_doc/donut&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/donut&quot;},{&quot;title&quot;:&quot;FLAVA&quot;,&quot;id&quot;:&quot;model_doc/flava&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/flava&quot;},{&quot;title&quot;:&quot;GIT&quot;,&quot;id&quot;:&quot;model_doc/git&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/git&quot;},{&quot;title&quot;:&quot;GroupViT&quot;,&quot;id&quot;:&quot;model_doc/groupvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/groupvit&quot;},{&quot;title&quot;:&quot;LayoutLM&quot;,&quot;id&quot;:&quot;model_doc/layoutlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlm&quot;},{&quot;title&quot;:&quot;LayoutLMV2&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv2&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv2&quot;},{&quot;title&quot;:&quot;LayoutLMV3&quot;,&quot;id&quot;:&quot;model_doc/layoutlmv3&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutlmv3&quot;},{&quot;title&quot;:&quot;LayoutXLM&quot;,&quot;id&quot;:&quot;model_doc/layoutxlm&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/layoutxlm&quot;},{&quot;title&quot;:&quot;LiLT&quot;,&quot;id&quot;:&quot;model_doc/lilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lilt&quot;},{&quot;title&quot;:&quot;LXMERT&quot;,&quot;id&quot;:&quot;model_doc/lxmert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/lxmert&quot;},{&quot;title&quot;:&quot;MatCha&quot;,&quot;id&quot;:&quot;model_doc/matcha&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/matcha&quot;},{&quot;title&quot;:&quot;MGP-STR&quot;,&quot;id&quot;:&quot;model_doc/mgp-str&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/mgp-str&quot;},{&quot;title&quot;:&quot;OneFormer&quot;,&quot;id&quot;:&quot;model_doc/oneformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/oneformer&quot;},{&quot;title&quot;:&quot;OWL-ViT&quot;,&quot;id&quot;:&quot;model_doc/owlvit&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/owlvit&quot;},{&quot;title&quot;:&quot;Perceiver&quot;,&quot;id&quot;:&quot;model_doc/perceiver&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/perceiver&quot;},{&quot;title&quot;:&quot;Pix2Struct&quot;,&quot;id&quot;:&quot;model_doc/pix2struct&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/pix2struct&quot;},{&quot;title&quot;:&quot;Segment Anything&quot;,&quot;id&quot;:&quot;model_doc/sam&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/sam&quot;},{&quot;title&quot;:&quot;Speech Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/speech-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/speech-encoder-decoder&quot;},{&quot;title&quot;:&quot;TAPAS&quot;,&quot;id&quot;:&quot;model_doc/tapas&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tapas&quot;},{&quot;title&quot;:&quot;TrOCR&quot;,&quot;id&quot;:&quot;model_doc/trocr&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trocr&quot;},{&quot;title&quot;:&quot;TVLT&quot;,&quot;id&quot;:&quot;model_doc/tvlt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/tvlt&quot;},{&quot;title&quot;:&quot;ViLT&quot;,&quot;id&quot;:&quot;model_doc/vilt&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vilt&quot;},{&quot;title&quot;:&quot;Vision Encoder Decoder Models&quot;,&quot;id&quot;:&quot;model_doc/vision-encoder-decoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-encoder-decoder&quot;},{&quot;title&quot;:&quot;Vision Text Dual Encoder&quot;,&quot;id&quot;:&quot;model_doc/vision-text-dual-encoder&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/vision-text-dual-encoder&quot;},{&quot;title&quot;:&quot;VisualBERT&quot;,&quot;id&quot;:&quot;model_doc/visual_bert&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/visual_bert&quot;},{&quot;title&quot;:&quot;X-CLIP&quot;,&quot;id&quot;:&quot;model_doc/xclip&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/xclip&quot;}]},{&quot;title&quot;:&quot;Reinforcement learning models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Decision Transformer&quot;,&quot;id&quot;:&quot;model_doc/decision_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/decision_transformer&quot;},{&quot;title&quot;:&quot;Trajectory Transformer&quot;,&quot;id&quot;:&quot;model_doc/trajectory_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/trajectory_transformer&quot;}]},{&quot;title&quot;:&quot;Time series models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Autoformer&quot;,&quot;id&quot;:&quot;model_doc/autoformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/autoformer&quot;},{&quot;title&quot;:&quot;Informer&quot;,&quot;id&quot;:&quot;model_doc/informer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/informer&quot;},{&quot;title&quot;:&quot;Time Series Transformer&quot;,&quot;id&quot;:&quot;model_doc/time_series_transformer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/time_series_transformer&quot;}]},{&quot;title&quot;:&quot;Graph models&quot;,&quot;isExpanded&quot;:false,&quot;sections&quot;:[{&quot;title&quot;:&quot;Graphormer&quot;,&quot;id&quot;:&quot;model_doc/graphormer&quot;,&quot;url&quot;:&quot;/docs/transformers/model_doc/graphormer&quot;}]}]},{&quot;title&quot;:&quot;Internal Helpers&quot;,&quot;isExpanded&quot;:true,&quot;sections&quot;:[{&quot;title&quot;:&quot;Custom Layers and Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/modeling_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/modeling_utils&quot;},{&quot;title&quot;:&quot;Utilities for pipelines&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/pipelines_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/pipelines_utils&quot;},{&quot;title&quot;:&quot;Utilities for Tokenizers&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/tokenization_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/tokenization_utils&quot;},{&quot;title&quot;:&quot;Utilities for Trainer&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/trainer_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/trainer_utils&quot;},{&quot;title&quot;:&quot;Utilities for Generation&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/generation_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/generation_utils&quot;},{&quot;title&quot;:&quot;Utilities for Image Processors&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/image_processing_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/image_processing_utils&quot;},{&quot;title&quot;:&quot;Utilities for Audio processing&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/audio_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/audio_utils&quot;},{&quot;title&quot;:&quot;General Utilities&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/file_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/file_utils&quot;},{&quot;title&quot;:&quot;Utilities for Time Series&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;internal/time_series_utils&quot;,&quot;url&quot;:&quot;/docs/transformers/internal/time_series_utils&quot;}]}]}],&quot;chapterId&quot;:&quot;benchmarks&quot;,&quot;docType&quot;:&quot;docs&quot;,&quot;isLoggedIn&quot;:false,&quot;lang&quot;:&quot;en&quot;,&quot;langs&quot;:[&quot;de&quot;,&quot;en&quot;,&quot;es&quot;,&quot;fr&quot;,&quot;it&quot;,&quot;ko&quot;,&quot;pt&quot;,&quot;zh&quot;],&quot;library&quot;:&quot;transformers&quot;,&quot;theme&quot;:&quot;light&quot;,&quot;version&quot;:&quot;v4.30.0&quot;,&quot;versions&quot;:[{&quot;version&quot;:&quot;main&quot;},{&quot;version&quot;:&quot;v4.30.0&quot;},{&quot;version&quot;:&quot;v4.29.1&quot;},{&quot;version&quot;:&quot;v4.29.0&quot;},{&quot;version&quot;:&quot;v4.28.1&quot;},{&quot;version&quot;:&quot;v4.28.0&quot;},{&quot;version&quot;:&quot;v4.27.2&quot;},{&quot;version&quot;:&quot;v4.27.1&quot;},{&quot;version&quot;:&quot;v4.27.0&quot;},{&quot;version&quot;:&quot;v4.26.1&quot;},{&quot;version&quot;:&quot;v4.26.0&quot;},{&quot;version&quot;:&quot;v4.25.1&quot;},{&quot;version&quot;:&quot;v4.24.0&quot;},{&quot;version&quot;:&quot;v4.23.1&quot;},{&quot;version&quot;:&quot;v4.23.0&quot;},{&quot;version&quot;:&quot;v4.22.2&quot;},{&quot;version&quot;:&quot;v4.22.1&quot;},{&quot;version&quot;:&quot;v4.22.0&quot;},{&quot;version&quot;:&quot;v4.21.3&quot;},{&quot;version&quot;:&quot;v4.21.2&quot;},{&quot;version&quot;:&quot;v4.21.1&quot;},{&quot;version&quot;:&quot;v4.21.0&quot;},{&quot;version&quot;:&quot;v4.20.1&quot;},{&quot;version&quot;:&quot;v4.20.0&quot;},{&quot;version&quot;:&quot;v4.19.4&quot;},{&quot;version&quot;:&quot;v4.19.3&quot;},{&quot;version&quot;:&quot;v4.19.2&quot;},{&quot;version&quot;:&quot;v4.19.0&quot;},{&quot;version&quot;:&quot;v4.18.0&quot;},{&quot;version&quot;:&quot;v4.17.0&quot;},{&quot;version&quot;:&quot;v4.16.2&quot;},{&quot;version&quot;:&quot;v4.16.1&quot;},{&quot;version&quot;:&quot;v4.16.0&quot;},{&quot;version&quot;:&quot;v4.15.0&quot;},{&quot;version&quot;:&quot;v4.14.1&quot;},{&quot;version&quot;:&quot;v4.13.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.5&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.4&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.12.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.3&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v4.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v3.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.11.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.10.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.9.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.8.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.7.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.6.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.5.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.4.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.3.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.2&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.1.1&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v2.0.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.2.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.1.0&quot;},{&quot;sphinx&quot;:true,&quot;version&quot;:&quot;v1.0.0&quot;},{&quot;version&quot;:&quot;doc-builder-html&quot;}],&quot;title&quot;:&quot;Benchmarks&quot;}" data-target="SideMenu"> <div class="z-2 w-full flex-none lg:block lg:h-screen lg:w-[270px] 2xl:w-[300px] false"><div class="shadow-alternate flex h-16 w-full items-center rounded-b-xl border-b bg-white text-lg leading-tight lg:hidden"><div class="flex flex-1 cursor-pointer flex-col justify-center self-stretch pl-6"><p class="text-sm text-gray-400 first-letter:capitalize">Transformers documentation</p> <div class="flex items-center"><p class="font-semibold">Benchmarks</p> <svg class="text-xl false" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></div></div> <button class="hover:shadow-alternate group ml-auto mr-6 inline-flex flex-none cursor-pointer rounded-xl border p-2"><svg class="text-gray-500 group-hover:text-gray-700" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg></button></div> <div class="hidden h-32 flex-col justify-between border-r border-b bg-white bg-gradient-to-r p-4 lg:flex from-orange-50 to-white dark:from-gray-900 dark:to-gray-950"><div class="relative "><button class=" " type="button"><h1 class="flex items-center text-lg font-bold leading-tight first-letter:capitalize"><div class="mr-1.5 h-1.5 w-1.5 rounded-full bg-orange-500 flex-none"></div> Transformers <span><svg class="opacity-70 " xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M16.293 9.293L12 13.586L7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z" fill="currentColor"></path></svg></span></h1> </button> </div> <button class="shadow-alternate flex w-full items-center rounded-full border bg-white px-2 py-1 text-left text-sm text-gray-400 ring-indigo-200 hover:bg-indigo-50 hover:ring-2 dark:border-gray-700 dark:ring-yellow-600 dark:hover:bg-gray-900 dark:hover:text-yellow-500"><svg class="flex-none mr-1.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M30 28.59L22.45 21A11 11 0 1 0 21 22.45L28.59 30zM5 14a9 9 0 1 1 9 9a9 9 0 0 1-9-9z" fill="currentColor"></path></svg> <div>Search documentation</div> <span class="ml-auto rounded border border-gray-200 bg-gray-100 px-0.5 text-xs dark:border-gray-800 dark:bg-gray-800"><kbd class="font-sans">⌘K</kbd></span></button> <div class="flex items-center"><select class="form-input mr-1 !mt-0 !w-20 rounded !border border-gray-200 p-1 text-xs uppercase dark:!text-gray-400"><option value="0">main</option><option value="1">v4.30.0</option><option value="2">v4.29.1</option><option value="3">v4.28.1</option><option value="4">v4.27.2</option><option value="5">v4.26.1</option><option value="6">v4.25.1</option><option value="7">v4.24.0</option><option value="8">v4.23.1</option><option value="9">v4.22.2</option><option value="10">v4.21.3</option><option value="11">v4.20.1</option><option value="12">v4.19.4</option><option value="13">v4.18.0</option><option value="14">v4.17.0</option><option value="15">v4.16.2</option><option value="16">v4.15.0</option><option value="17">v4.14.1</option><option value="18">v4.13.0</option><option value="19">v4.12.5</option><option value="20">v4.11.3</option><option value="21">v4.10.1</option><option value="22">v4.9.2</option><option value="23">v4.8.2</option><option value="24">v4.7.0</option><option value="25">v4.6.0</option><option value="26">v4.5.1</option><option value="27">v4.4.2</option><option value="28">v4.3.3</option><option value="29">v4.2.2</option><option value="30">v4.1.1</option><option value="31">v4.0.1</option><option value="32">v3.5.1</option><option value="33">v3.4.0</option><option value="34">v3.3.1</option><option value="35">v3.2.0</option><option value="36">v3.1.0</option><option value="37">v3.0.2</option><option value="38">v2.11.0</option><option value="39">v2.10.0</option><option value="40">v2.9.1</option><option value="41">v2.8.0</option><option value="42">v2.7.0</option><option value="43">v2.6.0</option><option value="44">v2.5.1</option><option value="45">v2.4.1</option><option value="46">v2.3.0</option><option value="47">v2.2.2</option><option value="48">v2.1.1</option><option value="49">v2.0.0</option><option value="50">v1.2.0</option><option value="51">v1.1.0</option><option value="52">v1.0.0</option><option value="53">doc-builder-html</option></select> <select class="form-input mr-1 rounded border-gray-200 p-1 text-xs dark:!text-gray-400 !w-12 !mt-0 !border"><option value="de">DE</option><option value="en">EN</option><option value="es">ES</option><option value="fr">FR</option><option value="it">IT</option><option value="ko">KO</option><option value="pt">PT</option><option value="zh">ZH</option></select> <div class="relative inline-block"><button class="rounded-full border border-gray-100 py-1 pl-2 pr-0.5 flex items-center text-sm text-gray-500 bg-white hover:bg-yellow-50 hover:border-yellow-200 dark:hover:bg-gray-800 dark:hover:border-gray-950 " type="button"><svg class="mr-1.5 text-yellow-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" fill="currentColor"><path d="M6.05 4.14l-.39-.39a.993.993 0 0 0-1.4 0l-.01.01a.984.984 0 0 0 0 1.4l.39.39c.39.39 1.01.39 1.4 0l.01-.01a.984.984 0 0 0 0-1.4zM3.01 10.5H1.99c-.55 0-.99.44-.99.99v.01c0 .55.44.99.99.99H3c.56.01 1-.43 1-.98v-.01c0-.56-.44-1-.99-1zm9-9.95H12c-.56 0-1 .44-1 .99v.96c0 .55.44.99.99.99H12c.56.01 1-.43 1-.98v-.97c0-.55-.44-.99-.99-.99zm7.74 3.21c-.39-.39-1.02-.39-1.41-.01l-.39.39a.984.984 0 0 0 0 1.4l.01.01c.39.39 1.02.39 1.4 0l.39-.39a.984.984 0 0 0 0-1.4zm-1.81 15.1l.39.39a.996.996 0 1 0 1.41-1.41l-.39-.39a.993.993 0 0 0-1.4 0c-.4.4-.4 1.02-.01 1.41zM20 11.49v.01c0 .55.44.99.99.99H22c.55 0 .99-.44.99-.99v-.01c0-.55-.44-.99-.99-.99h-1.01c-.55 0-.99.44-.99.99zM12 5.5c-3.31 0-6 2.69-6 6s2.69 6 6 6s6-2.69 6-6s-2.69-6-6-6zm-.01 16.95H12c.55 0 .99-.44.99-.99v-.96c0-.55-.44-.99-.99-.99h-.01c-.55 0-.99.44-.99.99v.96c0 .55.44.99.99.99zm-7.74-3.21c.39.39 1.02.39 1.41 0l.39-.39a.993.993 0 0 0 0-1.4l-.01-.01a.996.996 0 0 0-1.41 0l-.39.39c-.38.4-.38 1.02.01 1.41z"></path></svg> </button> </div> <a href="https://github.com/huggingface/transformers" class="group ml-auto text-xs text-gray-500 hover:text-gray-700 hover:underline dark:hover:text-gray-300"><svg class="inline-block text-gray-500 group-hover:text-gray-700 dark:group-hover:text-gray-300 mr-1.5 -mt-1 w-4 h-4" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1.03em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 250"><path d="M128.001 0C57.317 0 0 57.307 0 128.001c0 56.554 36.676 104.535 87.535 121.46c6.397 1.185 8.746-2.777 8.746-6.158c0-3.052-.12-13.135-.174-23.83c-35.61 7.742-43.124-15.103-43.124-15.103c-5.823-14.795-14.213-18.73-14.213-18.73c-11.613-7.944.876-7.78.876-7.78c12.853.902 19.621 13.19 19.621 13.19c11.417 19.568 29.945 13.911 37.249 10.64c1.149-8.272 4.466-13.92 8.127-17.116c-28.431-3.236-58.318-14.212-58.318-63.258c0-13.975 5-25.394 13.188-34.358c-1.329-3.224-5.71-16.242 1.24-33.874c0 0 10.749-3.44 35.21 13.121c10.21-2.836 21.16-4.258 32.038-4.307c10.878.049 21.837 1.47 32.066 4.307c24.431-16.56 35.165-13.12 35.165-13.12c6.967 17.63 2.584 30.65 1.255 33.873c8.207 8.964 13.173 20.383 13.173 34.358c0 49.163-29.944 59.988-58.447 63.157c4.591 3.972 8.682 11.762 8.682 23.704c0 17.126-.148 30.91-.148 35.126c0 3.407 2.304 7.398 8.792 6.14C219.37 232.5 256 184.537 256 128.002C256 57.307 198.691 0 128.001 0zm-80.06 182.34c-.282.636-1.283.827-2.194.39c-.929-.417-1.45-1.284-1.15-1.922c.276-.655 1.279-.838 2.205-.399c.93.418 1.46 1.293 1.139 1.931zm6.296 5.618c-.61.566-1.804.303-2.614-.591c-.837-.892-.994-2.086-.375-2.66c.63-.566 1.787-.301 2.626.591c.838.903 1 2.088.363 2.66zm4.32 7.188c-.785.545-2.067.034-2.86-1.104c-.784-1.138-.784-2.503.017-3.05c.795-.547 2.058-.055 2.861 1.075c.782 1.157.782 2.522-.019 3.08zm7.304 8.325c-.701.774-2.196.566-3.29-.49c-1.119-1.032-1.43-2.496-.726-3.27c.71-.776 2.213-.558 3.315.49c1.11 1.03 1.45 2.505.701 3.27zm9.442 2.81c-.31 1.003-1.75 1.459-3.199 1.033c-1.448-.439-2.395-1.613-2.103-2.626c.301-1.01 1.747-1.484 3.207-1.028c1.446.436 2.396 1.602 2.095 2.622zm10.744 1.193c.036 1.055-1.193 1.93-2.715 1.95c-1.53.034-2.769-.82-2.786-1.86c0-1.065 1.202-1.932 2.733-1.958c1.522-.03 2.768.818 2.768 1.868zm10.555-.405c.182 1.03-.875 2.088-2.387 2.37c-1.485.271-2.861-.365-3.05-1.386c-.184-1.056.893-2.114 2.376-2.387c1.514-.263 2.868.356 3.061 1.403z" fill="currentColor"></path></svg> 105,251</a></div></div> <nav class="top-32 hidden lg:flex absolute bottom-0 left-0 w-full flex-col overflow-y-auto border-r px-4 pt-3 pb-16 text-[0.95rem] lg:w-[270px] 2xl:w-[300px]"> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Get started</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/index">🤗 Transformers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/quicktour">Quick tour </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/installation">Installation </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Tutorials</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_tutorial">Run inference with pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/autoclass_tutorial">Write portable code with AutoClass </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/preprocessing">Preprocess data </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/training">Fine-tune a pretrained model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/run_scripts">Train with a script </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/accelerate">Set up distributed training with 🤗 Accelerate </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_sharing">Share your model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/transformers_agents">Agents </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Task Guides</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Natural Language Processing</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Computer Vision</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Developer guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/fast_tokenizers">Use fast tokenizers from 🤗 Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/multilingual">Run inference with multilingual models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/generation_strategies">Customize text generation strategy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/create_a_model">Use model-specific APIs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_models">Share a custom model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/sagemaker">Run training on Amazon SageMaker </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/serialization">Export to ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tflite">Export to TFLite </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/torchscript">Export to TorchScript </a><a class="rounded-xl bg-gradient-to-br from-black to-gray-900 py-1 pr-2 pl-2 text-white first:mt-1 last:mb-4 dark:from-gray-800 dark:to-gray-900 ml-2" href="/docs/transformers/benchmarks">Benchmarks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/notebooks">Notebooks with examples </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/community">Community resources </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/custom_tools">Custom Tools and Prompts </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/troubleshooting">Troubleshoot </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Performance and scalability</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/performance">Overview </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_one">Training on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_gpu_many">Training on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu">Training on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_cpu_many">Training on many CPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu">Training on TPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_tpu_tf">Training on TPU with TensorFlow </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_train_special">Training on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_cpu">Inference on CPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_one">Inference on one GPU </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_gpu_many">Inference on many GPUs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_infer_special">Inference on Specialized Hardware </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perf_hardware">Custom hardware for training </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/big_models">Instantiating a big model </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/debugging">Debugging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/hpo_train">Hyperparameter Search using Trainer API </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tf_xla">XLA Integration for TensorFlow Models </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Contribute</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/contributing">How to contribute to transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_model">How to add a model to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_tensorflow_model">How to convert a 🤗 Transformers model to TensorFlow? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/add_new_pipeline">How to add a pipeline to 🤗 Transformers? </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/testing">Testing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pr_checks">Checks on a Pull Request </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Conceptual guides</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/philosophy">Philosophy </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/glossary">Glossary </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/task_summary">What 🤗 Transformers can do </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tasks_explained">How 🤗 Transformers solve tasks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/model_summary">The Transformer model family </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/tokenizer_summary">Summary of the tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/attention">Attention mechanisms </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pad_truncation">Padding and truncation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/bertology">BERTology </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/perplexity">Perplexity of fixed-length models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-2" href="/docs/transformers/pipeline_webserver">Pipelines for webserver inference </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-0"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>API</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Main Classes</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/agent">Agents and Tools </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/model_doc/auto">Auto Classes </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/callback">Callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/configuration">Configuration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/data_collator">Data Collator </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/keras_callbacks">Keras callbacks </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/logging">Logging </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/model">Models </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/text_generation">Text Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/onnx">ONNX </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/optimizer_schedules">Optimization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/output">Model outputs </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/pipelines">Pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/processors">Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/quantization">Quantization </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/tokenizer">Tokenizer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/trainer">Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/deepspeed">DeepSpeed Integration </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/feature_extractor">Feature Extractor </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/main_classes/image_processor">Image Processor </a> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Models</span> </span></span></div></div> <div class="flex flex-col"><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Text models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Vision models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Audio models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Multimodal models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Reinforcement learning models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Time series models</span> </span></span></div></div> <div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-4"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] false"><span><span class="inline-block space-x-1 leading-5"><span>Graph models</span> </span></span></div></div> </div><div class="group flex cursor-pointer items-center pl-2 text-[0.8rem] font-semibold uppercase leading-9 hover:text-gray-700 dark:hover:text-gray-300 ml-2"><div class="flex after:absolute after:right-4 after:text-gray-500 group-hover:after:content-['▶'] after:rotate-90 after:transform"><span><span class="inline-block space-x-1 leading-5"><span>Internal Helpers</span> </span></span></div></div> <div class="flex flex-col"><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/modeling_utils">Custom Layers and Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/pipelines_utils">Utilities for pipelines </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/tokenization_utils">Utilities for Tokenizers </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/trainer_utils">Utilities for Trainer </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/generation_utils">Utilities for Generation </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/image_processing_utils">Utilities for Image Processors </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/audio_utils">Utilities for Audio processing </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/file_utils">General Utilities </a><a class="transform py-1 pr-2 pl-2 text-gray-500 first:mt-1 last:mb-4 hover:translate-x-px hover:text-black dark:hover:text-gray-300 ml-4" href="/docs/transformers/internal/time_series_utils">Utilities for Time Series </a> </div> </div></nav></div></div></div> <div class="z-1 min-w-0 flex-1"> <div class="px-6 pt-6 md:px-12 md:pt-16 md:pb-16"><div class="max-w-4xl mx-auto mb-10"><div class="relative overflow-hidden rounded-xl bg-gradient-to-br from-orange-300/10 py-5 px-4 ring-1 ring-orange-100/70 md:px-6 md:py-8"><img alt="Hugging Face's logo" class="absolute -right-6 -bottom-6 w-28 -rotate-45 md:hidden" src="/front/assets/huggingface_logo-noborder.svg"> <div class="mb-2 text-2xl font-bold dark:text-gray-200 md:mb-0">Join the Hugging Face community</div> <p class="mb-4 text-lg text-gray-400 dark:text-gray-300 md:mb-8">and get access to the augmented documentation experience </p> <div class="mb-8 hidden space-y-4 md:block xl:flex xl:space-y-0 xl:space-x-6"><div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-indigo-100 to-indigo-100/20 dark:to-indigo-100"><svg class="text-indigo-400 group-hover:text-indigo-500" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Collaborate on models, datasets and Spaces </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-orange-100 to-orange-100/20 dark:to-orange-50"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" class="text-xl text-yellow-400" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="M11 15H6l7-14v8h5l-7 14v-8z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Faster examples with accelerated inference </div></div> <div class="flex items-center"><div class="mr-3 flex h-9 w-9 flex-none items-center justify-center rounded-lg bg-gradient-to-br from-gray-500/10 to-gray-500/5"><svg class="text-gray-400" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M14.9804 3C14.9217 3.0002 14.8631 3.00555 14.8054 3.016C11.622 3.58252 8.76073 5.30669 6.77248 7.85653C4.78422 10.4064 3.80955 13.6016 4.03612 16.8271C4.26268 20.0525 5.67447 23.0801 7.99967 25.327C10.3249 27.5738 13.3991 28.8811 16.6304 28.997C16.7944 29.003 16.9584 28.997 17.1204 28.997C19.2193 28.9984 21.2877 28.4943 23.1507 27.5274C25.0137 26.5605 26.6164 25.1592 27.8234 23.442C27.9212 23.294 27.9783 23.1229 27.9889 22.9458C27.9995 22.7687 27.9633 22.592 27.884 22.4333C27.8046 22.2747 27.6848 22.1397 27.5367 22.0421C27.3887 21.9444 27.2175 21.8875 27.0404 21.877C25.0426 21.7017 23.112 21.0693 21.3976 20.0288C19.6832 18.9884 18.231 17.5676 17.1533 15.8764C16.0756 14.1852 15.4011 12.2688 15.1822 10.2754C14.9632 8.28193 15.2055 6.26484 15.8904 4.38C15.9486 4.22913 15.97 4.06652 15.9527 3.90572C15.9354 3.74492 15.8799 3.59059 15.7909 3.45557C15.7019 3.32055 15.5819 3.20877 15.4409 3.12952C15.2999 3.05028 15.142 3.00587 14.9804 3Z" fill="currentColor"></path></svg></div> <div class="text-smd leading-tight text-gray-500 dark:text-gray-300 xl:max-w-[200px] 2xl:text-base">Switch between documentation themes </div></div></div> <div class="flex items-center space-x-2.5"><a href="/join"><button class="rounded-lg bg-white bg-gradient-to-br from-gray-100/20 to-gray-200/60 py-1.5 px-5 font-semibold text-gray-700 shadow-sm ring-1 ring-gray-300/60 hover:to-gray-100/70 hover:ring-gray-300/30 active:shadow-inner">Sign Up</button></a> <p class="text-gray-500 dark:text-gray-300">to get started</p></div></div></div> <div class="prose-doc prose relative mx-auto max-w-4xl break-words"> <h1 class="relative group"><a id="benchmarks" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#benchmarks"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Benchmarks</span></h1> <div class="course-tip course-tip-orange bg-gradient-to-br dark:bg-gradient-to-r before:border-orange-500 dark:before:border-orange-800 from-orange-50 dark:from-gray-900 to-white dark:to-gray-950 border border-orange-50 text-orange-700 dark:text-gray-400"><p>Hugging Face’s Benchmarking tools are deprecated and it is advised to use external Benchmarking libraries to measure the speed and memory complexity of Transformer models.</p></div> <div class="flex space-x-1 absolute z-10 right-0 top-0"> <div class="relative colab-dropdown "><button class=" " type="button"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"></button> </div> <div class="relative colab-dropdown "><button class=" " type="button"><img alt="Open In Studio Lab" class="!m-0" src="https://studiolab.sagemaker.aws/studiolab.svg"></button> </div></div> <p>Let’s take a look at how 🤗 Transformers models can be benchmarked, best practices, and already available benchmarks.</p> <p>A notebook explaining in more detail how to benchmark 🤗 Transformers models can be found <a href="https://github.com/huggingface/notebooks/tree/main/examples/benchmark.ipynb" rel="nofollow">here</a>.</p> <h2 class="relative group"><a id="how-to-benchmark-transformers-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#how-to-benchmark-transformers-models"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>How to benchmark 🤗 Transformers models</span></h2> <p>The classes <code>PyTorchBenchmark</code> and <code>TensorFlowBenchmark</code> allow to flexibly benchmark 🤗 Transformers models. The benchmark classes allow us to measure the <em>peak memory usage</em> and <em>required time</em> for both <em>inference</em> and <em>training</em>.</p> <div class="course-tip bg-gradient-to-br dark:bg-gradient-to-r before:border-green-500 dark:before:border-green-800 from-green-50 dark:from-gray-900 to-white dark:to-gray-950 border border-green-50 text-green-700 dark:text-gray-400"><p>Hereby, <em>inference</em> is defined by a single forward pass, and <em>training</em> is defined by a single forward pass and backward pass.</p></div> <p>The benchmark classes <code>PyTorchBenchmark</code> and <code>TensorFlowBenchmark</code> expect an object of type <code>PyTorchBenchmarkArguments</code> and <code>TensorFlowBenchmarkArguments</code>, respectively, for instantiation. <code>PyTorchBenchmarkArguments</code> and <code>TensorFlowBenchmarkArguments</code> are data classes and contain all relevant configurations for their corresponding benchmark class. In the following example, it is shown how a BERT model of type <em>bert-base-cased</em> can be benchmarked.</p> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PyTorchBenchmark, PyTorchBenchmarkArguments <span class="hljs-meta">&gt;&gt;&gt; </span>args = PyTorchBenchmarkArguments(models=[<span class="hljs-string">"bert-base-uncased"</span>], batch_sizes=[<span class="hljs-number">8</span>], sequence_lengths=[<span class="hljs-number">8</span>, <span class="hljs-number">32</span>, <span class="hljs-number">128</span>, <span class="hljs-number">512</span>]) <span class="hljs-meta">&gt;&gt;&gt; </span>benchmark = PyTorchBenchmark(args)</pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TensorFlowBenchmark, TensorFlowBenchmarkArguments <span class="hljs-meta">&gt;&gt;&gt; </span>args = TensorFlowBenchmarkArguments( <span class="hljs-meta">... </span> models=[<span class="hljs-string">"bert-base-uncased"</span>], batch_sizes=[<span class="hljs-number">8</span>], sequence_lengths=[<span class="hljs-number">8</span>, <span class="hljs-number">32</span>, <span class="hljs-number">128</span>, <span class="hljs-number">512</span>] <span class="hljs-meta">... </span>) <span class="hljs-meta">&gt;&gt;&gt; </span>benchmark = TensorFlowBenchmark(args)</pre></div></div></div> </div> <p>Here, three arguments are given to the benchmark argument data classes, namely <code>models</code>, <code>batch_sizes</code>, and <code>sequence_lengths</code>. The argument <code>models</code> is required and expects a <code>list</code> of model identifiers from the <a href="https://huggingface.co/models" rel="nofollow">model hub</a> The <code>list</code> arguments <code>batch_sizes</code> and <code>sequence_lengths</code> define the size of the <code>input_ids</code> on which the model is benchmarked. There are many more parameters that can be configured via the benchmark argument data classes. For more detail on these one can either directly consult the files <code>src/transformers/benchmark/benchmark_args_utils.py</code>, <code>src/transformers/benchmark/benchmark_args.py</code> (for PyTorch) and <code>src/transformers/benchmark/benchmark_args_tf.py</code> (for Tensorflow). Alternatively, running the following shell commands from root will print out a descriptive list of all configurable parameters for PyTorch and Tensorflow respectively.</p> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python examples/pytorch/benchmarking/run_benchmark.py --<span class="hljs-built_in">help</span></pre></div> <p>An instantiated benchmark object can then simply be run by calling <code>benchmark.run()</code>.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>results = benchmark.run() <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(results) ==================== INFERENCE - SPEED - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Time <span class="hljs-keyword">in</span> s -------------------------------------------------------------------------------- bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">0.006</span> bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">0.006</span> bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">0.018</span> bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">0.088</span> -------------------------------------------------------------------------------- ==================== INFERENCE - MEMORY - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Memory <span class="hljs-keyword">in</span> MB -------------------------------------------------------------------------------- bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">1227</span> bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">1281</span> bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">1307</span> bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">1539</span> -------------------------------------------------------------------------------- ==================== ENVIRONMENT INFORMATION ==================== - transformers_version: <span class="hljs-number">2.11</span><span class="hljs-number">.0</span> - framework: PyTorch - use_torchscript: <span class="hljs-literal">False</span> - framework_version: <span class="hljs-number">1.4</span><span class="hljs-number">.0</span> - python_version: <span class="hljs-number">3.6</span><span class="hljs-number">.10</span> - system: Linux - cpu: x86_64 - architecture: 64bit - date: <span class="hljs-number">2020</span>-06-<span class="hljs-number">29</span> - time: 08:<span class="hljs-number">58</span>:<span class="hljs-number">43.371351</span> - fp16: <span class="hljs-literal">False</span> - use_multiprocessing: <span class="hljs-literal">True</span> - only_pretrain_model: <span class="hljs-literal">False</span> - cpu_ram_mb: <span class="hljs-number">32088</span> - use_gpu: <span class="hljs-literal">True</span> - num_gpus: <span class="hljs-number">1</span> - gpu: TITAN RTX - gpu_ram_mb: <span class="hljs-number">24217</span> - gpu_power_watts: <span class="hljs-number">280.0</span> - gpu_performance_state: <span class="hljs-number">2</span> - use_tpu: <span class="hljs-literal">False</span></pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre>python examples/tensorflow/benchmarking/run_benchmark_tf.py --<span class="hljs-built_in">help</span></pre></div> <p>An instantiated benchmark object can then simply be run by calling <code>benchmark.run()</code>.</p> <div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span>results = benchmark.run() <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(results) <span class="hljs-meta">&gt;&gt;&gt; </span>results = benchmark.run() <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(results) ==================== INFERENCE - SPEED - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Time <span class="hljs-keyword">in</span> s -------------------------------------------------------------------------------- bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">0.005</span> bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">0.008</span> bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">0.022</span> bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">0.105</span> -------------------------------------------------------------------------------- ==================== INFERENCE - MEMORY - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Memory <span class="hljs-keyword">in</span> MB -------------------------------------------------------------------------------- bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">1330</span> bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">1330</span> bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">1330</span> bert-base-uncased <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">1770</span> -------------------------------------------------------------------------------- ==================== ENVIRONMENT INFORMATION ==================== - transformers_version: <span class="hljs-number">2.11</span><span class="hljs-number">.0</span> - framework: Tensorflow - use_xla: <span class="hljs-literal">False</span> - framework_version: <span class="hljs-number">2.2</span><span class="hljs-number">.0</span> - python_version: <span class="hljs-number">3.6</span><span class="hljs-number">.10</span> - system: Linux - cpu: x86_64 - architecture: 64bit - date: <span class="hljs-number">2020</span>-06-<span class="hljs-number">29</span> - time: 09:<span class="hljs-number">26</span>:<span class="hljs-number">35.617317</span> - fp16: <span class="hljs-literal">False</span> - use_multiprocessing: <span class="hljs-literal">True</span> - only_pretrain_model: <span class="hljs-literal">False</span> - cpu_ram_mb: <span class="hljs-number">32088</span> - use_gpu: <span class="hljs-literal">True</span> - num_gpus: <span class="hljs-number">1</span> - gpu: TITAN RTX - gpu_ram_mb: <span class="hljs-number">24217</span> - gpu_power_watts: <span class="hljs-number">280.0</span> - gpu_performance_state: <span class="hljs-number">2</span> - use_tpu: <span class="hljs-literal">False</span></pre></div></div></div> </div> <p>By default, the <em>time</em> and the <em>required memory</em> for <em>inference</em> are benchmarked. In the example output above the first two sections show the result corresponding to <em>inference time</em> and <em>inference memory</em>. In addition, all relevant information about the computing environment, <em>e.g.</em> the GPU type, the system, the library versions, etc… are printed out in the third section under <em>ENVIRONMENT INFORMATION</em>. This information can optionally be saved in a <em>.csv</em> file when adding the argument <code>save_to_csv=True</code> to <code>PyTorchBenchmarkArguments</code> and <code>TensorFlowBenchmarkArguments</code> respectively. In this case, every section is saved in a separate <em>.csv</em> file. The path to each <em>.csv</em> file can optionally be defined via the argument data classes.</p> <p>Instead of benchmarking pre-trained models via their model identifier, <em>e.g.</em> <code>bert-base-uncased</code>, the user can alternatively benchmark an arbitrary configuration of any available model class. In this case, a <code>list</code> of configurations must be inserted with the benchmark args as follows.</p> <div class="space-y-10 py-6 2xl:py-8 2xl:-mx-4"><div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><defs><clipPath id="a"><rect x="3.05" y="0.5" width="25.73" height="31" fill="none"></rect></clipPath></defs><g clip-path="url(#a)"><path d="M24.94,9.51a12.81,12.81,0,0,1,0,18.16,12.68,12.68,0,0,1-18,0,12.81,12.81,0,0,1,0-18.16l9-9V5l-.84.83-6,6a9.58,9.58,0,1,0,13.55,0ZM20.44,9a1.68,1.68,0,1,1,1.67-1.67A1.68,1.68,0,0,1,20.44,9Z" fill="#ee4c2c"></path></g></svg> <span>Pytorch</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide Pytorch content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> PyTorchBenchmark, PyTorchBenchmarkArguments, BertConfig <span class="hljs-meta">&gt;&gt;&gt; </span>args = PyTorchBenchmarkArguments( <span class="hljs-meta">... </span> models=[<span class="hljs-string">"bert-base"</span>, <span class="hljs-string">"bert-384-hid"</span>, <span class="hljs-string">"bert-6-lay"</span>], batch_sizes=[<span class="hljs-number">8</span>], sequence_lengths=[<span class="hljs-number">8</span>, <span class="hljs-number">32</span>, <span class="hljs-number">128</span>, <span class="hljs-number">512</span>] <span class="hljs-meta">... </span>) <span class="hljs-meta">&gt;&gt;&gt; </span>config_base = BertConfig() <span class="hljs-meta">&gt;&gt;&gt; </span>config_384_hid = BertConfig(hidden_size=<span class="hljs-number">384</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>config_6_lay = BertConfig(num_hidden_layers=<span class="hljs-number">6</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>benchmark = PyTorchBenchmark(args, configs=[config_base, config_384_hid, config_6_lay]) <span class="hljs-meta">&gt;&gt;&gt; </span>benchmark.run() ==================== INFERENCE - SPEED - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Time <span class="hljs-keyword">in</span> s -------------------------------------------------------------------------------- bert-base <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">0.006</span> bert-base <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">0.006</span> bert-base <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">0.018</span> bert-base <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">0.088</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">0.006</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">0.006</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">0.011</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">0.054</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">0.003</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">0.004</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">0.009</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">0.044</span> -------------------------------------------------------------------------------- ==================== INFERENCE - MEMORY - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Memory <span class="hljs-keyword">in</span> MB -------------------------------------------------------------------------------- bert-base <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">1277</span> bert-base <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">1281</span> bert-base <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">1307</span> bert-base <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">1539</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">1005</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">1027</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">1035</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">1255</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">1097</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">1101</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">1127</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">1359</span> -------------------------------------------------------------------------------- ==================== ENVIRONMENT INFORMATION ==================== - transformers_version: <span class="hljs-number">2.11</span><span class="hljs-number">.0</span> - framework: PyTorch - use_torchscript: <span class="hljs-literal">False</span> - framework_version: <span class="hljs-number">1.4</span><span class="hljs-number">.0</span> - python_version: <span class="hljs-number">3.6</span><span class="hljs-number">.10</span> - system: Linux - cpu: x86_64 - architecture: 64bit - date: <span class="hljs-number">2020</span>-06-<span class="hljs-number">29</span> - time: 09:<span class="hljs-number">35</span>:<span class="hljs-number">25.143267</span> - fp16: <span class="hljs-literal">False</span> - use_multiprocessing: <span class="hljs-literal">True</span> - only_pretrain_model: <span class="hljs-literal">False</span> - cpu_ram_mb: <span class="hljs-number">32088</span> - use_gpu: <span class="hljs-literal">True</span> - num_gpus: <span class="hljs-number">1</span> - gpu: TITAN RTX - gpu_ram_mb: <span class="hljs-number">24217</span> - gpu_power_watts: <span class="hljs-number">280.0</span> - gpu_performance_state: <span class="hljs-number">2</span> - use_tpu: <span class="hljs-literal">False</span></pre></div></div></div> <div class="border border-gray-200 rounded-xl px-4 relative"><div class="flex h-[22px] mt-[-12.5px] justify-between leading-none"><div class="flex px-1 items-center space-x-1 bg-white dark:bg-gray-950"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="0.94em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 274"><path d="M145.726 42.065v42.07l72.861 42.07v-42.07l-72.86-42.07zM0 84.135v42.07l36.43 21.03V105.17L0 84.135zm109.291 21.035l-36.43 21.034v126.2l36.43 21.035v-84.135l36.435 21.035v-42.07l-36.435-21.034V105.17z" fill="#E55B2D"></path><path d="M145.726 42.065L36.43 105.17v42.065l72.861-42.065v42.065l36.435-21.03v-84.14zM255.022 63.1l-36.435 21.035v42.07l36.435-21.035V63.1zm-72.865 84.135l-36.43 21.035v42.07l36.43-21.036v-42.07zm-36.43 63.104l-36.436-21.035v84.135l36.435-21.035V210.34z" fill="#ED8E24"></path><path d="M145.726 0L0 84.135l36.43 21.035l109.296-63.105l72.861 42.07L255.022 63.1L145.726 0zm0 126.204l-36.435 21.03l36.435 21.036l36.43-21.035l-36.43-21.03z" fill="#F8BF3C"></path></svg> <span>TensorFlow</span></div> <div class="cursor-pointer flex items-center justify-center space-x-1 text-sm px-2 bg-white dark:bg-gray-950 hover:underline leading-none"><svg class="" width="0.9em" height="0.9em" viewBox="0 0 10 9" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M1.39125 1.9725L0.0883333 0.669997L0.677917 0.0804138L8.9275 8.33041L8.33792 8.91958L6.95875 7.54041C6.22592 8.00523 5.37572 8.25138 4.50792 8.25C2.26125 8.25 0.392083 6.63333 0 4.5C0.179179 3.52946 0.667345 2.64287 1.39167 1.9725H1.39125ZM5.65667 6.23833L5.04667 5.62833C4.81335 5.73996 4.55116 5.77647 4.29622 5.73282C4.04129 5.68918 3.80617 5.56752 3.62328 5.38463C3.44039 5.20175 3.31874 4.96663 3.27509 4.71169C3.23144 4.45676 3.26795 4.19456 3.37958 3.96125L2.76958 3.35125C2.50447 3.75187 2.38595 4.2318 2.4341 4.70978C2.48225 5.18777 2.6941 5.63442 3.0338 5.97411C3.37349 6.31381 3.82015 6.52567 4.29813 6.57382C4.77611 6.62197 5.25605 6.50345 5.65667 6.23833ZM2.83042 1.06666C3.35 0.862497 3.91625 0.749997 4.50792 0.749997C6.75458 0.749997 8.62375 2.36666 9.01583 4.5C8.88816 5.19404 8.60119 5.84899 8.1775 6.41333L6.56917 4.805C6.61694 4.48317 6.58868 4.15463 6.48664 3.84569C6.3846 3.53675 6.21162 3.256 5.98156 3.02594C5.7515 2.79588 5.47075 2.6229 5.16181 2.52086C4.85287 2.41882 4.52433 2.39056 4.2025 2.43833L2.83042 1.06708V1.06666Z" fill="currentColor"></path></svg> <span>Hide TensorFlow content</span></div></div> <div class="framework-content"><div class="code-block relative"><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TensorFlowBenchmark, TensorFlowBenchmarkArguments, BertConfig <span class="hljs-meta">&gt;&gt;&gt; </span>args = TensorFlowBenchmarkArguments( <span class="hljs-meta">... </span> models=[<span class="hljs-string">"bert-base"</span>, <span class="hljs-string">"bert-384-hid"</span>, <span class="hljs-string">"bert-6-lay"</span>], batch_sizes=[<span class="hljs-number">8</span>], sequence_lengths=[<span class="hljs-number">8</span>, <span class="hljs-number">32</span>, <span class="hljs-number">128</span>, <span class="hljs-number">512</span>] <span class="hljs-meta">... </span>) <span class="hljs-meta">&gt;&gt;&gt; </span>config_base = BertConfig() <span class="hljs-meta">&gt;&gt;&gt; </span>config_384_hid = BertConfig(hidden_size=<span class="hljs-number">384</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>config_6_lay = BertConfig(num_hidden_layers=<span class="hljs-number">6</span>) <span class="hljs-meta">&gt;&gt;&gt; </span>benchmark = TensorFlowBenchmark(args, configs=[config_base, config_384_hid, config_6_lay]) <span class="hljs-meta">&gt;&gt;&gt; </span>benchmark.run() ==================== INFERENCE - SPEED - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Time <span class="hljs-keyword">in</span> s -------------------------------------------------------------------------------- bert-base <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">0.005</span> bert-base <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">0.008</span> bert-base <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">0.022</span> bert-base <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">0.106</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">0.005</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">0.007</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">0.018</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">0.064</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">0.002</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">0.003</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">0.0011</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">0.074</span> -------------------------------------------------------------------------------- ==================== INFERENCE - MEMORY - RESULT ==================== -------------------------------------------------------------------------------- Model Name Batch Size Seq Length Memory <span class="hljs-keyword">in</span> MB -------------------------------------------------------------------------------- bert-base <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">1330</span> bert-base <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">1330</span> bert-base <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">1330</span> bert-base <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">1770</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">1330</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">1330</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">1330</span> bert-<span class="hljs-number">384</span>-hid <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">1540</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">8</span> <span class="hljs-number">1330</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">32</span> <span class="hljs-number">1330</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">128</span> <span class="hljs-number">1330</span> bert-<span class="hljs-number">6</span>-lay <span class="hljs-number">8</span> <span class="hljs-number">512</span> <span class="hljs-number">1540</span> -------------------------------------------------------------------------------- ==================== ENVIRONMENT INFORMATION ==================== - transformers_version: <span class="hljs-number">2.11</span><span class="hljs-number">.0</span> - framework: Tensorflow - use_xla: <span class="hljs-literal">False</span> - framework_version: <span class="hljs-number">2.2</span><span class="hljs-number">.0</span> - python_version: <span class="hljs-number">3.6</span><span class="hljs-number">.10</span> - system: Linux - cpu: x86_64 - architecture: 64bit - date: <span class="hljs-number">2020</span>-06-<span class="hljs-number">29</span> - time: 09:<span class="hljs-number">38</span>:<span class="hljs-number">15.487125</span> - fp16: <span class="hljs-literal">False</span> - use_multiprocessing: <span class="hljs-literal">True</span> - only_pretrain_model: <span class="hljs-literal">False</span> - cpu_ram_mb: <span class="hljs-number">32088</span> - use_gpu: <span class="hljs-literal">True</span> - num_gpus: <span class="hljs-number">1</span> - gpu: TITAN RTX - gpu_ram_mb: <span class="hljs-number">24217</span> - gpu_power_watts: <span class="hljs-number">280.0</span> - gpu_performance_state: <span class="hljs-number">2</span> - use_tpu: <span class="hljs-literal">False</span></pre></div></div></div> </div> <p>Again, <em>inference time</em> and <em>required memory</em> for <em>inference</em> are measured, but this time for customized configurations of the <code>BertModel</code> class. This feature can especially be helpful when deciding for which configuration the model should be trained.</p> <h2 class="relative group"><a id="benchmark-best-practices" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#benchmark-best-practices"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Benchmark best practices</span></h2> <p>This section lists a couple of best practices one should be aware of when benchmarking a model.</p> <ul><li>Currently, only single device benchmarking is supported. When benchmarking on GPU, it is recommended that the user specifies on which device the code should be run by setting the <code>CUDA_VISIBLE_DEVICES</code> environment variable in the shell, <em>e.g.</em> <code>export CUDA_VISIBLE_DEVICES=0</code> before running the code.</li> <li>The option <code>no_multi_processing</code> should only be set to <code>True</code> for testing and debugging. To ensure accurate memory measurement it is recommended to run each memory benchmark in a separate process by making sure <code>no_multi_processing</code> is set to <code>True</code>.</li> <li>One should always state the environment information when sharing the results of a model benchmark. Results can vary heavily between different GPU devices, library versions, etc., so that benchmark results on their own are not very useful for the community.</li></ul> <h2 class="relative group"><a id="sharing-your-benchmark" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#sharing-your-benchmark"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Sharing your benchmark</span></h2> <p>Previously all available core models (10 at the time) have been benchmarked for <em>inference time</em>, across many different settings: using PyTorch, with and without TorchScript, using TensorFlow, with and without XLA. All of those tests were done across CPUs (except for TensorFlow XLA) and GPUs.</p> <p>The approach is detailed in the <a href="https://medium.com/huggingface/benchmarking-transformers-pytorch-and-tensorflow-e2917fb891c2" rel="nofollow">following blogpost</a> and the results are available <a href="https://docs.google.com/spreadsheets/d/1sryqufw2D0XlUH4sq3e9Wnxu5EAQkaohzrJbd5HdQ_w/edit?usp=sharing" rel="nofollow">here</a>.</p> <p>With the new <em>benchmark</em> tools, it is easier than ever to share your benchmark results with the community</p> <ul><li><a href="https://github.com/huggingface/transformers/tree/main/examples/pytorch/benchmarking/README.md" rel="nofollow">PyTorch Benchmarking Results</a>.</li> <li><a href="https://github.com/huggingface/transformers/tree/main/examples/tensorflow/benchmarking/README.md" rel="nofollow">TensorFlow Benchmarking Results</a>.</li></ul> <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0px; top: 0px; clip: rect(0px, 0px, 0px, 0px); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px;"></div></div> <div class="mx-auto mt-16 flex max-w-4xl items-center pb-8 font-sans font-medium leading-6 xl:mt-32"><a href="/docs/transformers/torchscript" class="mr-8 flex transform items-center text-gray-600 transition-all hover:-translate-x-px hover:text-gray-900 dark:hover:text-gray-300"><span class="mr-2 translate-y-px">←</span>Export to TorchScript</a> <a href="/docs/transformers/notebooks" class="ml-auto flex transform items-center text-right text-gray-600 transition-all hover:translate-x-px hover:text-gray-900 dark:hover:text-gray-300">Notebooks with examples<span class="ml-2 translate-y-px">→</span></a></div></div></div> <div class="sticky top-0 self-start"><div class="SVELTE_HYDRATER contents" data-props="{&quot;chapter&quot;:{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmarks&quot;,&quot;url&quot;:&quot;#benchmarks&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;How to benchmark 🤗 Transformers models&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;how-to-benchmark-transformers-models&quot;,&quot;url&quot;:&quot;#how-to-benchmark-transformers-models&quot;},{&quot;title&quot;:&quot;Benchmark best practices&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;benchmark-best-practices&quot;,&quot;url&quot;:&quot;#benchmark-best-practices&quot;},{&quot;title&quot;:&quot;Sharing your benchmark&quot;,&quot;isExpanded&quot;:true,&quot;id&quot;:&quot;sharing-your-benchmark&quot;,&quot;url&quot;:&quot;#sharing-your-benchmark&quot;}]}}" data-target="SubSideMenu"><nav class="hidden h-screen w-[270px] flex-none flex-col space-y-3 overflow-y-auto break-words border-l pt-24 pl-6 pr-10 pb-16 text-sm lg:flex 2xl:w-[305px]"><a href="#benchmarks" class=" text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-benchmarks"><wbr>Benchmarks</a> <a href="#how-to-benchmark-transformers-models" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-how-to-benchmark-transformers-models"><wbr>How to benchmark 🤗 <wbr>Transformers models</a> <a href="#benchmark-best-practices" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-benchmark-best-practices"><wbr>Benchmark best practices</a> <a href="#sharing-your-benchmark" class="pl-4 text-gray-400 transform hover:translate-x-px hover:text-gray-700 dark:hover:text-gray-300" id="nav-sharing-your-benchmark"><wbr>Sharing your benchmark</a> </nav></div></div></div> <div id="doc-footer"></div></main> </div> <script> import("/front/build/kube-c0d76de/index.js"); window.moonSha = "kube-c0d76de/"; window.hubConfig = JSON.parse(`{"features":{"signupDisabled":false},"sshGitUrl":"git@hf.co","moonHttpUrl":"https://huggingface.co","captchaApiKey":"bd5f2066-93dc-4bdd-a64b-a24646ca3859","stripePublicKey":"pk_live_x2tdjFXBCvXo2FFmMybezpeM00J6gPCAAc"}`); </script> <!-- Stripe --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://js.stripe.com/v3/"; script.async = true; document.head.appendChild(script); } </script> <!-- Google analytics v4 --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { const script = document.createElement("script"); script.src = "https://www.googletagmanager.com/gtag/js?id=G-8Q63TH4CSL"; script.async = true; document.head.appendChild(script); window.dataLayer = window.dataLayer || []; function gtag() { if (window.dataLayer !== undefined) { window.dataLayer.push(arguments); } } gtag("js", new Date()); gtag("config", "G-8Q63TH4CSL", { page_path: "/docs/transformers/benchmarks" }); /// ^ See https://developers.google.com/analytics/devguides/collection/gtagjs/pages gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" }); /// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent /// TODO: ask the user for their consent and update this with gtag('consent', 'update') } </script> <!-- Google Analytics v3 (deprecated) --> <script> if (["hf.co", "huggingface.co"].includes(window.location.hostname)) { (function (i, s, o, g, r, a, m) { i["GoogleAnalyticsObject"] = r; (i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments); }), (i[r].l = 1 * new Date()); (a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]); a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, "script", "https://www.google-analytics.com/analytics.js", "ganalytics"); ganalytics("create", "UA-83738774-2", "auto"); ganalytics("send", "pageview", "/docs/transformers/benchmarks"); } </script> <iframe name="__privateStripeMetricsController3110" frameborder="0" allowtransparency="true" scrolling="no" role="presentation" allow="payment *" src="https://js.stripe.com/v3/m-outer-93afeeb17bc37e711759584dbfc50d47.html#url=https%3A%2F%2Fhuggingface.co%2Fdocs%2Ftransformers%2Fbenchmarks&amp;title=Benchmarks&amp;referrer=&amp;muid=e9b16def-0136-44b8-bb10-a3c524603e60e8f6fb&amp;sid=1d1c7959-97a3-4c99-95e7-b4b6fcd486384ff5ae&amp;version=6&amp;preview=false" aria-hidden="true" tabindex="-1" style="border: none !important; margin: 0px !important; padding: 0px !important; width: 1px !important; min-width: 100% !important; overflow: hidden !important; display: block !important; visibility: hidden !important; position: fixed !important; height: 1px !important; pointer-events: none !important; user-select: none !important;"></iframe></body></html>
2023-06-27T19:52:13.977Z